Skip to content
Myr

arguments.md

This file

Path

docs/arguments.md

192 lines · 5.1 KiB · Markdown

Last changed by

Merge stable into main (#3876)Rowlando13before adoption3cbaa76

History of this file

Take it

Move Arguments doc from reST to MyST (#3267)Rowlando13before adoptionc8111c2

lines 1–10why, one step back
1(arguments)=
23# Arguments45```{currentmodule} click6```78Arguments are:910* Are positional in nature.

Apply format guidelines to commits since latest 8.3.2 release (#3343)Rowlando13before adoptionf1f191e

lines 11–12why, one step back
11* Similar to a limited version of {ref}`options <options>` that
12 can take an arbitrary number of inputs

Pre-8.5.0 release changelog and doc fixes (#3697)Rowlando13before adoption00e592c

line 13why, one step back
13* Can take an optional `help` string shown in the `Positional arguments`

Add help parameter to arguments (#3473)Kevin Deldyckebefore adoptionc480210

line 14why, one step back
14 section of the help page, or be {ref}`documented in the command docstring

Merge stable into main (#3876)Rowlando13before adoption3cbaa76

lines 15–16why, one step back
15 <documenting-arguments>`. One argument with a `help` makes the section list
16 all the arguments of the command.

Move Arguments doc from reST to MyST (#3267)Rowlando13before adoptionc8111c2

lines 17–19why, one step back
17
18Useful and often used kwargs are:19

Add help parameter to arguments (#3473)Kevin Deldyckebefore adoptionc480210

line 20why, one step back
20* `help`: Help text for the argument.

Move Arguments doc from reST to MyST (#3267)Rowlando13before adoptionc8111c2

lines 21–25why, one step back
21* `default`: Passes a default.
22* `nargs`: Sets the number of arguments. Set to -1 to take an arbitrary number.2324## Basic Arguments25

Apply format guidelines to commits since latest 8.3.2 release (#3343)Rowlando13before adoptionf1f191e

lines 26–28why, one step back
26A minimal {class}`click.Argument` solely takes one string argument:
27the name of the argument. This will assume the argument is required,28has no default, and is of the type `str`.

Move Arguments doc from reST to MyST (#3267)Rowlando13before adoptionc8111c2

lines 29–47why, one step back
29
30Example:3132```{eval-rst}33.. click:example::3435 @click.command()36 @click.argument('filename')37 def touch(filename: str):38 """Print FILENAME."""39 click.echo(filename)4041And from the command line:4243.. click:run::4445 invoke(touch, args=['foo.txt'])46```47

Apply format guidelines to commits since latest 8.3.2 release (#3343)Rowlando13before adoptionf1f191e

line 48why, one step back
48An argument may be assigned a {ref}`parameter type <parameter-types>`.

Stable (#3851)Rowlando13before adoption6aabf09

lines 49–52why, one step back
49If no type is provided, the type of the default value is used if it is
50recognized, otherwise {data}`STRING` is used. If no default value is51provided, the type is assumed to be {data}`STRING`. See52{ref}`type-inference` for the types that are recognized.

Move Arguments doc from reST to MyST (#3267)Rowlando13before adoptionc8111c2

lines 53–56why, one step back
53
54```{admonition} Note on Required Arguments55:class: note56

Apply format guidelines to commits since latest 8.3.2 release (#3343)Rowlando13before adoptionf1f191e

lines 57–61why, one step back
57 It is possible to make an argument required by setting
58 `required=True`. It is not recommended since we think command line59 tools should gracefully degrade into becoming no ops. We think this60 because command line tools are often invoked with wildcard inputs61 and they should not error out if the wildcard is empty.

Move Arguments doc from reST to MyST (#3267)Rowlando13before adoptionc8111c2

lines 62–65why, one step back
62```
6364## Multiple Arguments65

Apply format guidelines to commits since latest 8.3.2 release (#3343)Rowlando13before adoptionf1f191e

lines 66–70why, one step back
66To set the number of argument use the `nargs` kwarg. It can be set to
67any positive integer and -1. Setting it to -1, makes the number of68arguments arbitrary (which is called variadic) and can only be used69once. The arguments are then packed as a tuple and passed to the70function.

Move Arguments doc from reST to MyST (#3267)Rowlando13before adoptionc8111c2

lines 71–92why, one step back
71
72```{eval-rst}73.. click:example::7475 @click.command()76 @click.argument('src', nargs=1)77 @click.argument('dsts', nargs=-1)78 def copy(src: str, dsts: tuple[str, ...]):79 """Move file SRC to DST."""80 for destination in dsts:81 click.echo(f"Copy {src} to folder {destination}")8283And from the command line:8485.. click:run::8687 invoke(copy, args=['foo.txt', 'usr/david/foo.txt', 'usr/mitsuko/foo.txt'])88```8990```{admonition} Note on Handling Files91:class: note92

Apply format guidelines to commits since latest 8.3.2 release (#3343)Rowlando13before adoptionf1f191e

lines 93–95why, one step back
93 This is not how you should handle files and files paths. This
94 merely used as a simple example. See {ref}`handling-files` to learn95 more about how to handle files in parameters.

Move Arguments doc from reST to MyST (#3267)Rowlando13before adoptionc8111c2

lines 96–99why, one step back
96```
9798## Argument Escape Sequences99

Apply format guidelines to commits since latest 8.3.2 release (#3343)Rowlando13before adoptionf1f191e

lines 100–103why, one step back
100If you want to process arguments that look like options, like a file
101named `-foo.txt` or `--foo.txt`, you must pass the `--` separator102first. After you pass the `--`, you may only pass arguments. This is a103common feature for POSIX command line tools.

Move Arguments doc from reST to MyST (#3267)Rowlando13before adoptionc8111c2

lines 104–123why, one step back
104
105Example usage:106107```{eval-rst}108.. click:example::109110 @click.command()111 @click.argument('files', nargs=-1, type=click.Path())112 def touch(files):113 """Print all FILES file names."""114 for filename in files:115 click.echo(filename)116117And from the command line:118119.. click:run::120121 invoke(touch, ['--', '-foo.txt', 'bar.txt'])122```123

Apply format guidelines to commits since latest 8.3.2 release (#3343)Rowlando13before adoptionf1f191e

lines 124–125why, one step back
124If you don't like the `--` marker, you can set
125`ignore_unknown_options` to `True` to avoid checking unknown options:

Move Arguments doc from reST to MyST (#3267)Rowlando13before adoptionc8111c2

lines 126–147why, one step back
126
127```{eval-rst}128.. click:example::129130 @click.command(context_settings={"ignore_unknown_options": True})131 @click.argument('files', nargs=-1, type=click.Path())132 def touch(files):133 """Print all FILES file names."""134 for filename in files:135 click.echo(filename)136137And from the command line:138139.. click:run::140141 invoke(touch, ['-foo.txt', 'bar.txt'])142```143144(environment-variables)=145146## Environment Variables147

Apply format guidelines to commits since latest 8.3.2 release (#3343)Rowlando13before adoptionf1f191e

lines 148–149why, one step back
148Arguments can use environment variables. To do so, pass the name(s) of
149the environment variable(s) via `envvar` in `click.argument`.

Move Arguments doc from reST to MyST (#3267)Rowlando13before adoptionc8111c2

lines 150–192why, one step back
150
151Checking one environment variable:152153```{eval-rst}154.. click:example::155156 @click.command()157 @click.argument('src', envvar='SRC', type=click.File('r'))158 def echo(src):159 """Print value of SRC environment variable."""160 click.echo(src.read())161162And from the command line:163164.. click:run::165166 with isolated_filesystem():167 # Writing the file in the filesystem.168 with open('hello.txt', 'w') as f:169 f.write('Hello World!')170 invoke(echo, env={'SRC': 'hello.txt'})171172173Checking multiple environment variables:174175.. click:example::176177 @click.command()178 @click.argument('src', envvar=['SRC', 'SRC_2'], type=click.File('r'))179 def echo(src):180 """Print value of SRC environment variable."""181 click.echo(src.read())182183And from the command line:184185.. click:run::186187 with isolated_filesystem():188 # Writing the file in the filesystem.189 with open('hello.txt', 'w') as f:190 f.write('Hello World from second variable!')191 invoke(echo, env={'SRC_2': 'hello.txt'})192```