Added docs for path type
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Armin Ronacher · committed by Armin Ronacher · parent a6af25d · Browse files · 2 files·+43 added−1 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+43 added−1 removed
- other:+43 added−1 removed
Provenance
- Commit
c13d723- Tree
48ab82c- Author
Armin Ronacher <armin.ronacher@active-4.com>- Committer
Armin Ronacher <armin.ronacher@active-4.com>- Parent
a6af25d
2 files changed+43 added−1 removed
modified:docs/arguments.rst+42 added
56 unmodified lines57 .. click:run::58 59 invoke(copy, args=['foo.txt', 'bar.txt', 'my_folder'])60 61+Note that this is not how you would write this application. The reason62+for this is that in this particular example the arguments are defined as63+strings. File names however are not strings! They might be (on certain64+operating systems) but not necessarily on others. For better ways see the65+next sections.66+67 .. admonition:: Note on Non-Empty Variadic Arguments68 69 If you come from ``argparse`` you might be missing support for setting70 ``nargs`` to ``+`` to indicate that at least one argument is required.41 unmodified lines112 invoke(inout, args=['-', 'hello.txt'], input=['hello'],113 terminate_input=True)114 invoke(inout, args=['hello.txt', '-'])115 116+File Path Arguments117+-------------------118+119+In the previous example the files were opened immediately. But what if120+we just want the filename? The naive way is to use the default string121+argument type. However remember that click is unicode based so the string122+will be a unicode value always. Filenames unfortunately however can be123+unicode or bytes depending on which operating system you're on. As such124+the type is insufficient.125+126+Instead you should be using the :class:`Path` type which handles this127+complexity for you. Not only will it return either bytes or unicode128+depending on what makes more sense, but it will also be able to do some129+basic checks for you such as existence checks.130+131+Example:132+133+.. click:example::134+135+ @click.command()136+ @click.argument('f', type=click.Path(exists=True))137+ def touch(f):138+ click.echo(click.format_filename(f))139+140+And what it does:141+142+.. click:run::143+144+ with isolated_filesystem():145+ with open('hello.txt', 'w') as f:146+ f.write('Hello World!\n')147+ invoke(touch, args=['hello.txt'])148+ println()149+ invoke(touch, args=['missing.txt'])150+151+152 File Opening Safety153 -------------------154 155 The :class:`FileType` type has one problem it needs to deal with and that87 unmodified linesmodified:examples/repo/repo.py+1 added-1 removed
105 unmodified lines106 @cli.command()107 @click.option('--message', '-m', multiple=True,108 help='The commit message. If provided multiple times each '109 'argument gets converted into a new line.')110-@click.argument('files', nargs=-1)110+@click.argument('files', nargs=-1, type=click.Path())111 @pass_repo112 def commit(repo, files, message):113 """Commits outstanding changes.114 37 unmodified lines