arguments.rst
This file
Path
docs/arguments.rst
165 lines · 5.1 KiB
Last changed by
Added notes on missing "+" from argparseArmin Ronacherbefore adoption1ad7df7
History of this fileTake it
1Arguments2=========34.. currentmodule:: click56Arguments work similar to options but are positional. They also onlyMerge pull request #27 from epequeno/docs-editsArmin Ronacherbefore adoptionb2475cd
line 7why, one step back7support a subset of the features of options due to their syntactical nature.Initial commitArmin Ronacherbefore adoption4101de3
lines 8–138Click also will not attempt to document arguments for you and wants you to9document them manually to avoid ugly looking help pages.1011Basic Arguments12---------------13Merge pull request #27 from epequeno/docs-editsArmin Ronacherbefore adoptionb2475cd
line 14why, one step back14The most basic option is a simple string argument of one value. If noInitial commitArmin Ronacherbefore adoption4101de3
lines 15–1715type is provided the type of the default value is used. If no default16value is provided the type is assumed to be :data:`STRING`.17Make documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670
line 18why, one step back18Example:19Make documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670
lines 20–21why, one step back20.. click:example::21Initial commitArmin Ronacherbefore adoption4101de3
lines 22–2422 @click.command()23 @click.argument('filename')24 def touch(filename):Implemented and exposed click.echoArmin Ronacherbefore adoption95c24b3
line 25why, one step back25 click.echo(filename)26Make documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670
line 27why, one step back27And what it looks like:28Make documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670
line 29why, one step back29.. click:run::30Make documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670
lines 31–32why, one step back31 invoke(touch, args=['foo.txt'])32Initial commitArmin Ronacherbefore adoption4101de3
lines 33–4033Variadic Arguments34------------------3536The second most common version is variadic arguments where a specific (or37unlimited) number of arguments is accepted. This can be controlled with38the ``nargs`` parameter. If it's set to ``-1`` then an unlimited number39of arguments is accepted.40Added support for nargs in other placesArmin Ronacherbefore adoption2e8354a
lines 41–42why, one step back41The value is then passed as a tuple. Note that only one argument can be42set to ``nargs=-1`` as this will eat up all arguments.43Make documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670
line 44why, one step back44Example:45Make documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670
lines 46–47why, one step back46.. click:example::4748 @click.command()Added support for nargs in other placesArmin Ronacherbefore adoption2e8354a
lines 49–53why, one step back49 @click.argument('src', nargs=-1)50 @click.argument('dst', nargs=1)51 def copy(src, dst):52 for fn in src:53 click.echo('move %s to folder %s' % (fn, dst))54Make documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670
line 55why, one step back55And what it looks like:56Make documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670
line 57why, one step back57.. click:run::58Fixed a broken example in the docsArmin Ronacherbefore adoption20e8ffa
line 59why, one step back59 invoke(copy, args=['foo.txt', 'bar.txt', 'my_folder'])Make documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670
line 60why, one step back60Added notes on missing "+" from argparseArmin Ronacherbefore adoption1ad7df7
lines 61–71why, one step back61.. admonition:: Note on Non-Empty Variadic Arguments6263 If you come from ``argparse`` you might be missing support for setting64 ``nargs`` to ``+`` to indicate that at least one argument is required.6566 Support for this is very intentionally not included as we believe67 scripts should gracefully degrate into becomming noops if a variadic68 argument is empty. The reason for this is that very often scripts are69 invoked with wildcard inputs from the command line and they should not70 error out if the wildcard is empty.71Added choice supportArmin Ronacherbefore adoption9eacc90
lines 72–73why, one step back72.. _file-args:73Initial commitArmin Ronacherbefore adoption4101de3
lines 74–7874File Arguments75--------------7677Since all the examples have already worked with file names it makes sense78to explain how to deal with files properly. Command line tools are moreMerge pull request #27 from epequeno/docs-editsArmin Ronacherbefore adoptionb2475cd
line 79why, one step back79fun if they work with files the unix way which is to accept ``-`` as aInitial commitArmin Ronacherbefore adoption4101de3
lines 80–8580special file that refers to stdin/stdout.8182Click supports this through the :class:`click.File` type which83intelligently handles files for you. It also deals with unicode and bytes84correctly for all versions of Python so your script stays very portable.85Make documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670
line 86why, one step back86Example:87Make documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670
lines 88–89why, one step back88.. click:example::89Initial commitArmin Ronacherbefore adoption4101de3
lines 90–9990 @click.command()91 @click.argument('input', type=click.File('rb'))92 @click.argument('output', type=click.File('wb'))93 def inout(input, output):94 while True:95 chunk = input.read(1024)96 if not chunk:97 break98 output.write(chunk)99Make documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670
line 100why, one step back100And what it does:101Make documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670
line 102why, one step back102.. click:run::103Make documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670
line 104why, one step back104 with isolated_filesystem():Split up click into a packageArmin Ronacherbefore adoption0ed40c8
lines 105–106why, one step back105 invoke(inout, args=['-', 'hello.txt'], input=['hello'],106 terminate_input=True)Make documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670
lines 107–108why, one step back107 invoke(inout, args=['hello.txt', '-'])108Added lazy file openingArmin Ronacherbefore adoptionf270dd2
lines 109–127why, one step back109File Opening Safety110-------------------111112The :class:`FileType` type has one problem it needs to deal with and that113is to decide when to open a file. The default behavior is to be114"intelligent" about it. What this means is that it will open stdin/stdout115and files opened for reading immediately. This will directly give the116user feedback when a file cannot be opened. But it will only open files117for writing the first time an IO operation is performed by wrapping the118file automatically in a special wrapper.119120This behavior can be forced by passing ``lazy=True`` or ``lazy=False`` to121the constructor. If the file is openened lazily it will fail on first IO122operation by raising an :exc:`FileError`.123124Since files opened for writing will typically immediatley empty the file,125the lazy mode should really only be disabled if the developer is 100% sure126that this is intended behavior.127Documentation updates for file handlingArmin Ronacherbefore adoption72ae8a2
lines 128–135why, one step back128Forcing on lazy mode is also very useful to avoid resource handling129confusion. If a file is opened in lazy mode it will get a130``close_intelligently`` method that can help you figuring out if the file131needs closing or not. This is not needed for parameters, but it is132necessary for manually prompting with the :func:`prompt` function as you133do not know if a stream like stdout was openend (which was already open134before) or a real file that needs closing.135Initial commitArmin Ronacherbefore adoption4101de3
lines 136–142136Environment Variables137---------------------138139Like options, arguments can also get values from an environment variable.140Unlike options however this is only supported for explicitly named141environment variables.142Make documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670
line 143why, one step back143Example usage:144Make documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670
lines 145–146why, one step back145.. click:example::146147 @click.command()Make documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670
line 148why, one step back148 @click.argument('src', envvar='SRC', type=click.File('r'))149 def echo(src):Implemented and exposed click.echoArmin Ronacherbefore adoption95c24b3
line 150why, one step back150 click.echo(src.read())151Make documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670
line 152why, one step back152And from the command line:153Make documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670
line 154why, one step back154.. click:run::155Make documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670
lines 156–159why, one step back156 with isolated_filesystem():157 with open('hello.txt', 'w') as f:158 f.write('Hello World!')159 invoke(echo, env={'SRC': 'hello.txt'})Initial commitArmin Ronacherbefore adoption4101de3
lines 160–165160161In that case it can also be a list of different environment variables162where the first one is picked.163164Generally this feature is not recommended because it can cause a lot of165confusion to the user.