Skip to content
Myr

arguments.rst

This file

Path

docs/arguments.rst

242 lines · 7.7 KiB

Last changed by

Merge pull request #125 from iKevinY/doc-fixesArmin Ronacherbefore adoption1d25afd

History of this file

Take it

The walk stopped after 200 commits: lines older than 5dbdfef point at it.

Reversed logic, pick the long ones before the short onesArmin Ronacherbefore adoption5dbdfef

lines 1–5why, one step back
1Arguments
2=========34.. currentmodule:: click5

Merge pull request #125 from iKevinY/doc-fixesArmin Ronacherbefore adoption1d25afd

line 6why, one step back
6Arguments work similarly to options but are positional. They also only

Reversed logic, pick the long ones before the short onesArmin Ronacherbefore adoption5dbdfef

line 7why, one step back
7support a subset of the features of options due to their syntactical nature.

Documentation fixesKevin Yapbefore adoption67523af

lines 8–9why, one step back
8Click will also not attempt to document arguments for you and wants you to
9document them manually in order to avoid ugly help pages.

Reversed logic, pick the long ones before the short onesArmin Ronacherbefore adoption5dbdfef

lines 10–14why, one step back
10
11Basic Arguments12---------------1314The most basic option is a simple string argument of one value. If no

Documentation fixesKevin Yapbefore adoption67523af

lines 15–16why, one step back
15type is provided, the type of the default value is used, and if no default
16value is provided, the type is assumed to be :data:`STRING`.

Reversed logic, pick the long ones before the short onesArmin Ronacherbefore adoption5dbdfef

lines 17–37why, one step back
17
18Example:1920.. click:example::2122 @click.command()23 @click.argument('filename')24 def touch(filename):25 click.echo(filename)2627And what it looks like:2829.. click:run::3031 invoke(touch, args=['foo.txt'])3233Variadic Arguments34------------------3536The second most common version is variadic arguments where a specific (or37unlimited) number of arguments is accepted. This can be controlled with

Documentation fixesKevin Yapbefore adoption67523af

line 38why, one step back
38the ``nargs`` parameter. If it is set to ``-1``, then an unlimited number

Reversed logic, pick the long ones before the short onesArmin Ronacherbefore adoption5dbdfef

lines 39–41why, one step back
39of arguments is accepted.
4041The value is then passed as a tuple. Note that only one argument can be

Documentation fixesKevin Yapbefore adoption67523af

line 42why, one step back
42set to ``nargs=-1``, as it will eat up all arguments.

Reversed logic, pick the long ones before the short onesArmin Ronacherbefore adoption5dbdfef

lines 43–60why, one step back
43
44Example:4546.. click:example::4748 @click.command()49 @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))5455And what it looks like:5657.. click:run::5859 invoke(copy, args=['foo.txt', 'bar.txt', 'my_folder'])60

Added docs for path typeArmin Ronacherbefore adoptionc13d723

lines 61–62why, one step back
61Note that this is not how you would write this application. The reason
62for this is that in this particular example the arguments are defined as

Documentation fixesKevin Yapbefore adoption67523af

lines 63–65why, one step back
63strings. Filenames, however, are not strings! They might be on certain
64operating systems, but not necessarily on all. For better ways to write65this, see the next sections.

Added docs for path typeArmin Ronacherbefore adoptionc13d723

line 66why, one step back
66

Reversed logic, pick the long ones before the short onesArmin Ronacherbefore adoption5dbdfef

lines 67–68why, one step back
67.. admonition:: Note on Non-Empty Variadic Arguments
68

Merge pull request #125 from iKevinY/doc-fixesArmin Ronacherbefore adoption1d25afd

line 69why, one step back
69 If you come from ``argparse``, you might be missing support for setting

Reversed logic, pick the long ones before the short onesArmin Ronacherbefore adoption5dbdfef

lines 70–71why, one step back
70 ``nargs`` to ``+`` to indicate that at least one argument is required.
71

Merge pull request #109 from iKevinY/doc-fixesArmin Ronacherbefore adoptiond428e69

line 72why, one step back
72 This is supported by setting ``required=True``. However, this should

setting a default for arguments now implicitly makes it non requiredArmin Ronacherbefore adoption71da0b4

lines 73–74why, one step back
73 not be used if you can avoid it as we believe scripts should gracefully
74 degrade into becoming noops if a variadic argument is empty. The

Merge pull request #109 from iKevinY/doc-fixesArmin Ronacherbefore adoptiond428e69

line 75why, one step back
75 reason for this is that very often, scripts are invoked with wildcard

setting a default for arguments now implicitly makes it non requiredArmin Ronacherbefore adoption71da0b4

lines 76–77why, one step back
76 inputs from the command line and they should not error out if the
77 wildcard is empty.

Reversed logic, pick the long ones before the short onesArmin Ronacherbefore adoption5dbdfef

lines 78–83why, one step back
78
79.. _file-args:8081File Arguments82--------------83

Merge pull request #109 from iKevinY/doc-fixesArmin Ronacherbefore adoptiond428e69

line 84why, one step back
84Since all the examples have already worked with filenames, it makes sense

Reversed logic, pick the long ones before the short onesArmin Ronacherbefore adoption5dbdfef

line 85why, one step back
85to explain how to deal with files properly. Command line tools are more

Documentation fixesKevin Yapbefore adoption67523af

line 86why, one step back
86fun if they work with files the Unix way, which is to accept ``-`` as a

Reversed logic, pick the long ones before the short onesArmin Ronacherbefore adoption5dbdfef

lines 87–89why, one step back
87special file that refers to stdin/stdout.
8889Click supports this through the :class:`click.File` type which

Merge pull request #109 from iKevinY/doc-fixesArmin Ronacherbefore adoptiond428e69

line 90why, one step back
90intelligently handles files for you. It also deals with Unicode and bytes

Reversed logic, pick the long ones before the short onesArmin Ronacherbefore adoption5dbdfef

lines 91–114why, one step back
91correctly for all versions of Python so your script stays very portable.
9293Example:9495.. click:example::9697 @click.command()98 @click.argument('input', type=click.File('rb'))99 @click.argument('output', type=click.File('wb'))100 def inout(input, output):101 while True:102 chunk = input.read(1024)103 if not chunk:104 break105 output.write(chunk)106107And what it does:108109.. click:run::110111 with isolated_filesystem():112 invoke(inout, args=['-', 'hello.txt'], input=['hello'],113 terminate_input=True)114 invoke(inout, args=['hello.txt', '-'])

Added docs for path typeArmin Ronacherbefore adoptionc13d723

lines 115–118why, one step back
115
116File Path Arguments117-------------------118

Merge pull request #125 from iKevinY/doc-fixesArmin Ronacherbefore adoption1d25afd

lines 119–120why, one step back
119In the previous example, the files were opened immediately. But what if
120we just want the filename? The naïve way is to use the default string

Documentation fixesKevin Yapbefore adoption67523af

lines 121–124why, one step back
121argument type. However, remember that click is Unicode-based, so the string
122will always be a Unicode value. Unfortunately, filenames can be Unicode or123bytes depending on which operating system is being used. As such, the type124is insufficient.

Added docs for path typeArmin Ronacherbefore adoptionc13d723

line 125why, one step back
125

Merge pull request #125 from iKevinY/doc-fixesArmin Ronacherbefore adoption1d25afd

lines 126–127why, one step back
126Instead, you should be using the :class:`Path` type, which automatically
127handles this ambiguity. Not only will it return either bytes or Unicode

Added docs for path typeArmin Ronacherbefore adoptionc13d723

lines 128–150why, one step back
128depending on what makes more sense, but it will also be able to do some
129basic checks for you such as existence checks.130131Example:132133.. click:example::134135 @click.command()136 @click.argument('f', type=click.Path(exists=True))137 def touch(f):138 click.echo(click.format_filename(f))139140And what it does:141142.. click:run::143144 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

Reversed logic, pick the long ones before the short onesArmin Ronacherbefore adoption5dbdfef

lines 151–154why, one step back
151
152File Opening Safety153-------------------154

Documentation fixesKevin Yapbefore adoption67523af

line 155why, one step back
155The :class:`FileType` type has one problem it needs to deal with, and that

Reversed logic, pick the long ones before the short onesArmin Ronacherbefore adoption5dbdfef

lines 156–157why, one step back
156is to decide when to open a file. The default behavior is to be
157"intelligent" about it. What this means is that it will open stdin/stdout

Documentation fixesKevin Yapbefore adoption67523af

lines 158–161why, one step back
158and files opened for reading immediately. This will give the user direct
159feedback when a file cannot be opened, but it will only open files160for writing the first time an IO operation is performed by automatically161wrapping the file in a special wrapper.

Reversed logic, pick the long ones before the short onesArmin Ronacherbefore adoption5dbdfef

lines 162–163why, one step back
162
163This behavior can be forced by passing ``lazy=True`` or ``lazy=False`` to

Documentation fixesKevin Yapbefore adoption67523af

line 164why, one step back
164the constructor. If the file is opened lazily, it will fail its first IO

Reversed logic, pick the long ones before the short onesArmin Ronacherbefore adoption5dbdfef

lines 165–166why, one step back
165operation by raising an :exc:`FileError`.
166

Merge pull request #109 from iKevinY/doc-fixesArmin Ronacherbefore adoptiond428e69

lines 167–168why, one step back
167Since files opened for writing will typically immediately empty the file,
168the lazy mode should only be disabled if the developer is absolutely sure

Reversed logic, pick the long ones before the short onesArmin Ronacherbefore adoption5dbdfef

lines 169–170why, one step back
169that this is intended behavior.
170

Merge pull request #109 from iKevinY/doc-fixesArmin Ronacherbefore adoptiond428e69

lines 171–174why, one step back
171Forcing lazy mode is also very useful to avoid resource handling
172confusion. If a file is opened in lazy mode, it will receive a173``close_intelligently`` method that can help figure out if the file174needs closing or not. This is not needed for parameters, but is

Reversed logic, pick the long ones before the short onesArmin Ronacherbefore adoption5dbdfef

line 175why, one step back
175necessary for manually prompting with the :func:`prompt` function as you

Merge pull request #109 from iKevinY/doc-fixesArmin Ronacherbefore adoptiond428e69

line 176why, one step back
176do not know if a stream like stdout was opened (which was already open

Reversed logic, pick the long ones before the short onesArmin Ronacherbefore adoption5dbdfef

lines 177–178why, one step back
177before) or a real file that needs closing.
178

Documentation fixesKevin Yapbefore adoption67523af

line 179why, one step back
179Starting with click 2.0, it is also possible to open files in atomic mode by

Merge pull request #109 from iKevinY/doc-fixesArmin Ronacherbefore adoptiond428e69

line 180why, one step back
180passing ``atomic=True``. In atomic mode, all writes go into a separate

Documentation fixesKevin Yapbefore adoption67523af

line 181why, one step back
181file in the same folder, and upon completion, the file will be moved over to

Merge pull request #93 from julen/doc-fixesArmin Ronacherbefore adoption82973d0

lines 182–183why, one step back
182the original location. This is useful if a file regularly read by other
183users is modified.

Implemented atomic file opening support.Armin Ronacherbefore adoption469ba25

line 184why, one step back
184

Reversed logic, pick the long ones before the short onesArmin Ronacherbefore adoption5dbdfef

lines 185–187why, one step back
185Environment Variables
186---------------------187

Documentation fixesKevin Yapbefore adoption67523af

line 188why, one step back
188Like options, arguments can also grab values from an environment variable.

Merge pull request #109 from iKevinY/doc-fixesArmin Ronacherbefore adoptiond428e69

line 189why, one step back
189Unlike options, however, this is only supported for explicitly named

Reversed logic, pick the long ones before the short onesArmin Ronacherbefore adoption5dbdfef

lines 190–209why, one step back
190environment variables.
191192Example usage:193194.. click:example::195196 @click.command()197 @click.argument('src', envvar='SRC', type=click.File('r'))198 def echo(src):199 click.echo(src.read())200201And from the command line:202203.. click:run::204205 with isolated_filesystem():206 with open('hello.txt', 'w') as f:207 f.write('Hello World!')208 invoke(echo, env={'SRC': 'hello.txt'})209

Merge pull request #109 from iKevinY/doc-fixesArmin Ronacherbefore adoptiond428e69

line 210why, one step back
210In that case, it can also be a list of different environment variables

Reversed logic, pick the long ones before the short onesArmin Ronacherbefore adoption5dbdfef

lines 211–212why, one step back
211where the first one is picked.
212

Documentation fixesKevin Yapbefore adoption67523af

lines 213–214why, one step back
213Generally, this feature is not recommended because it can cause the user
214a lot of confusion.

Mentioned "--" in the docs.Armin Ronacherbefore adoption5f8c04f

line 215why, one step back
215

Documentation fixesKevin Yapbefore adoption67523af

line 216why, one step back
216Argument-Like Options

Merge pull request #125 from iKevinY/doc-fixesArmin Ronacherbefore adoption1d25afd

line 217why, one step back
217---------------------

Mentioned "--" in the docs.Armin Ronacherbefore adoption5f8c04f

line 218why, one step back
218

Documentation fixesKevin Yapbefore adoption67523af

line 219why, one step back
219Sometimes, you want to process arguments that look like options. For

Merge pull request #109 from iKevinY/doc-fixesArmin Ronacherbefore adoptiond428e69

lines 220–221why, one step back
220instance, imagine you have a file named ``-foo.txt``. If you pass this as
221an argument in this manner, click will treat it as an option.

Mentioned "--" in the docs.Armin Ronacherbefore adoption5f8c04f

line 222why, one step back
222

Merge pull request #109 from iKevinY/doc-fixesArmin Ronacherbefore adoptiond428e69

line 223why, one step back
223To solve this, click does what any POSIX style command line script does,

Mentioned "--" in the docs.Armin Ronacherbefore adoption5f8c04f

line 224why, one step back
224and that is to accept the string ``--`` as a separator for options and

Merge pull request #109 from iKevinY/doc-fixesArmin Ronacherbefore adoptiond428e69

line 225why, one step back
225arguments. After the ``--`` marker, all further parameters are accepted as

Mentioned "--" in the docs.Armin Ronacherbefore adoption5f8c04f

lines 226–242why, one step back
226arguments.
227228Example usage:229230.. click:example::231232 @click.command()233 @click.argument('files', nargs=-1, type=click.Path())234 def touch(files):235 for filename in files:236 click.echo(filename)237238And from the command line:239240.. click:run::241242 invoke(touch, ['--', '-foo.txt', 'bar.txt'])