Skip to content
Myr

arguments.rst

This file

Path

docs/arguments.rst

245 lines · 7.7 KiB

Last changed by

Merge branch 'master' of https://github.com/mitsuhiko/clickArmin Ronacherbefore adoption4ee540d

History of this file

Take it

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

Merge branch 'master' of https://github.com/mitsuhiko/clickArmin Ronacherbefore adoption4ee540d

lines 1–2why, one step back
1.. _arguments:
2

Added license to docs and changelog.Armin Ronacherbefore adoptionddf58b2

lines 3–7why, one step back
3Arguments
4=========56.. currentmodule:: click7

Merge branch 'master' of https://github.com/mitsuhiko/clickArmin Ronacherbefore adoption4ee540d

lines 8–12why, one step back
8Arguments work similarly to :ref:`options <options>` but are positional.
9They also only support a subset of the features of options due to their10syntactical nature. Click will also not attempt to document arguments for11you and wants you to document them manually in order to avoid ugly help12pages.

Added license to docs and changelog.Armin Ronacherbefore adoptionddf58b2

lines 13–17why, one step back
13
14Basic Arguments15---------------1617The most basic option is a simple string argument of one value. If no

Documentation fixesKevin Yapbefore adoption67523af

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

Added license to docs and changelog.Armin Ronacherbefore adoptionddf58b2

lines 20–40why, one step back
20
21Example:2223.. click:example::2425 @click.command()26 @click.argument('filename')27 def touch(filename):28 click.echo(filename)2930And what it looks like:3132.. click:run::3334 invoke(touch, args=['foo.txt'])3536Variadic Arguments37------------------3839The second most common version is variadic arguments where a specific (or40unlimited) number of arguments is accepted. This can be controlled with

Documentation fixesKevin Yapbefore adoption67523af

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

Added license to docs and changelog.Armin Ronacherbefore adoptionddf58b2

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

Documentation fixesKevin Yapbefore adoption67523af

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

Added license to docs and changelog.Armin Ronacherbefore adoptionddf58b2

lines 46–63why, one step back
46
47Example:4849.. click:example::5051 @click.command()52 @click.argument('src', nargs=-1)53 @click.argument('dst', nargs=1)54 def copy(src, dst):55 for fn in src:56 click.echo('move %s to folder %s' % (fn, dst))5758And what it looks like:5960.. click:run::6162 invoke(copy, args=['foo.txt', 'bar.txt', 'my_folder'])63

Added docs for path typeArmin Ronacherbefore adoptionc13d723

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

Documentation fixesKevin Yapbefore adoption67523af

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

Added docs for path typeArmin Ronacherbefore adoptionc13d723

line 69why, one step back
69

Added license to docs and changelog.Armin Ronacherbefore adoptionddf58b2

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

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

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

Added license to docs and changelog.Armin Ronacherbefore adoptionddf58b2

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

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

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

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

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

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

line 78why, one step back
78 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 79–80why, one step back
79 inputs from the command line and they should not error out if the
80 wildcard is empty.

Added license to docs and changelog.Armin Ronacherbefore adoptionddf58b2

lines 81–86why, one step back
81
82.. _file-args:8384File Arguments85--------------86

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

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

Added license to docs and changelog.Armin Ronacherbefore adoptionddf58b2

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

Documentation fixesKevin Yapbefore adoption67523af

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

Added license to docs and changelog.Armin Ronacherbefore adoptionddf58b2

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

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

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

Added license to docs and changelog.Armin Ronacherbefore adoptionddf58b2

lines 94–117why, one step back
94correctly for all versions of Python so your script stays very portable.
9596Example:9798.. click:example::99100 @click.command()101 @click.argument('input', type=click.File('rb'))102 @click.argument('output', type=click.File('wb'))103 def inout(input, output):104 while True:105 chunk = input.read(1024)106 if not chunk:107 break108 output.write(chunk)109110And what it does:111112.. click:run::113114 with isolated_filesystem():115 invoke(inout, args=['-', 'hello.txt'], input=['hello'],116 terminate_input=True)117 invoke(inout, args=['hello.txt', '-'])

Added docs for path typeArmin Ronacherbefore adoptionc13d723

lines 118–121why, one step back
118
119File Path Arguments120-------------------121

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

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

Merge pull request #153 from iKevinY/click-renameArmin Ronacherbefore adoption327cfde

line 124why, one step back
124argument type. However, remember that Click is Unicode-based, so the string

Documentation fixesKevin Yapbefore adoption67523af

lines 125–127why, one step back
125will always be a Unicode value. Unfortunately, filenames can be Unicode or
126bytes depending on which operating system is being used. As such, the type127is insufficient.

Added docs for path typeArmin Ronacherbefore adoptionc13d723

line 128why, one step back
128

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

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

Added docs for path typeArmin Ronacherbefore adoptionc13d723

lines 131–153why, one step back
131depending on what makes more sense, but it will also be able to do some
132basic checks for you such as existence checks.133134Example:135136.. click:example::137138 @click.command()139 @click.argument('f', type=click.Path(exists=True))140 def touch(f):141 click.echo(click.format_filename(f))142143And what it does:144145.. click:run::146147 with isolated_filesystem():148 with open('hello.txt', 'w') as f:149 f.write('Hello World!\n')150 invoke(touch, args=['hello.txt'])151 println()152 invoke(touch, args=['missing.txt'])153

Added license to docs and changelog.Armin Ronacherbefore adoptionddf58b2

lines 154–157why, one step back
154
155File Opening Safety156-------------------157

Documentation fixesKevin Yapbefore adoption67523af

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

Added license to docs and changelog.Armin Ronacherbefore adoptionddf58b2

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

Documentation fixesKevin Yapbefore adoption67523af

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

Added license to docs and changelog.Armin Ronacherbefore adoptionddf58b2

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

Documentation fixesKevin Yapbefore adoption67523af

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

Added license to docs and changelog.Armin Ronacherbefore adoptionddf58b2

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

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

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

Added license to docs and changelog.Armin Ronacherbefore adoptionddf58b2

lines 172–173why, one step back
172that this is intended behavior.
173

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

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

Added license to docs and changelog.Armin Ronacherbefore adoptionddf58b2

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

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

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

Added license to docs and changelog.Armin Ronacherbefore adoptionddf58b2

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

Merge pull request #153 from iKevinY/click-renameArmin Ronacherbefore adoption327cfde

line 182why, one step back
182Starting 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 183why, one step back
183passing ``atomic=True``. In atomic mode, all writes go into a separate

Documentation fixesKevin Yapbefore adoption67523af

line 184why, one step back
184file 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 185–186why, one step back
185the original location. This is useful if a file regularly read by other
186users is modified.

Added license to docs and changelog.Armin Ronacherbefore adoptionddf58b2

lines 187–190why, one step back
187
188Environment Variables189---------------------190

Documentation fixesKevin Yapbefore adoption67523af

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

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

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

Added license to docs and changelog.Armin Ronacherbefore adoptionddf58b2

lines 193–212why, one step back
193environment variables.
194195Example usage:196197.. click:example::198199 @click.command()200 @click.argument('src', envvar='SRC', type=click.File('r'))201 def echo(src):202 click.echo(src.read())203204And from the command line:205206.. click:run::207208 with isolated_filesystem():209 with open('hello.txt', 'w') as f:210 f.write('Hello World!')211 invoke(echo, env={'SRC': 'hello.txt'})212

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

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

Added license to docs and changelog.Armin Ronacherbefore adoptionddf58b2

lines 214–215why, one step back
214where the first one is picked.
215

Documentation fixesKevin Yapbefore adoption67523af

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

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

line 218why, one step back
218

Documentation fixesKevin Yapbefore adoption67523af

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

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

line 220why, one step back
220---------------------

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

line 221why, one step back
221

Documentation fixesKevin Yapbefore adoption67523af

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

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

line 223why, one step back
223instance, imagine you have a file named ``-foo.txt``. If you pass this as

Merge pull request #153 from iKevinY/click-renameArmin Ronacherbefore adoption327cfde

line 224why, one step back
224an argument in this manner, Click will treat it as an option.

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

line 225why, one step back
225

Merge pull request #153 from iKevinY/click-renameArmin Ronacherbefore adoption327cfde

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

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

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

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

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

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

lines 229–245why, one step back
229arguments.
230231Example usage:232233.. click:example::234235 @click.command()236 @click.argument('files', nargs=-1, type=click.Path())237 def touch(files):238 for filename in files:239 click.echo(filename)240241And from the command line:242243.. click:run::244245 invoke(touch, ['--', '-foo.txt', 'bar.txt'])