Skip to content
Myr

arguments.md

This file

Path

docs/arguments.md

191 lines · 5.0 KiB · Markdown

Last changed by

Document type auto-conversion and test `set` and `frozenset` (#3808)Rowlando13before adoptione5cb12b

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

Merge main into stable (#3800)Rowlando13before adoption420c8fb

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

lines 14–15why, one step back
14 section of the help page, or be {ref}`documented in the command docstring
15 <documenting-arguments>`.

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

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

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

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

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

lines 20–24why, one step back
20* `default`: Passes a default.
21* `nargs`: Sets the number of arguments. Set to -1 to take an arbitrary number.2223## Basic Arguments24

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

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

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

lines 28–46why, one step back
28
29Example:3031```{eval-rst}32.. click:example::3334 @click.command()35 @click.argument('filename')36 def touch(filename: str):37 """Print FILENAME."""38 click.echo(filename)3940And from the command line:4142.. click:run::4344 invoke(touch, args=['foo.txt'])45```46

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

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

Document type auto-conversion and test `set` and `frozenset` (#3808)Rowlando13before adoptione5cb12b

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

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

lines 52–55why, one step back
52
53```{admonition} Note on Required Arguments54:class: note55

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

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

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

lines 61–64why, one step back
61```
6263## Multiple Arguments64

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

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

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

lines 70–91why, one step back
70
71```{eval-rst}72.. click:example::7374 @click.command()75 @click.argument('src', nargs=1)76 @click.argument('dsts', nargs=-1)77 def copy(src: str, dsts: tuple[str, ...]):78 """Move file SRC to DST."""79 for destination in dsts:80 click.echo(f"Copy {src} to folder {destination}")8182And from the command line:8384.. click:run::8586 invoke(copy, args=['foo.txt', 'usr/david/foo.txt', 'usr/mitsuko/foo.txt'])87```8889```{admonition} Note on Handling Files90:class: note91

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

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

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

lines 95–98why, one step back
95```
9697## Argument Escape Sequences98

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

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

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

lines 103–122why, one step back
103
104Example usage:105106```{eval-rst}107.. click:example::108109 @click.command()110 @click.argument('files', nargs=-1, type=click.Path())111 def touch(files):112 """Print all FILES file names."""113 for filename in files:114 click.echo(filename)115116And from the command line:117118.. click:run::119120 invoke(touch, ['--', '-foo.txt', 'bar.txt'])121```122

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

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

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

lines 125–146why, one step back
125
126```{eval-rst}127.. click:example::128129 @click.command(context_settings={"ignore_unknown_options": True})130 @click.argument('files', nargs=-1, type=click.Path())131 def touch(files):132 """Print all FILES file names."""133 for filename in files:134 click.echo(filename)135136And from the command line:137138.. click:run::139140 invoke(touch, ['-foo.txt', 'bar.txt'])141```142143(environment-variables)=144145## Environment Variables146

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

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

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

lines 149–191why, one step back
149
150Checking one environment variable:151152```{eval-rst}153.. click:example::154155 @click.command()156 @click.argument('src', envvar='SRC', type=click.File('r'))157 def echo(src):158 """Print value of SRC environment variable."""159 click.echo(src.read())160161And from the command line:162163.. click:run::164165 with isolated_filesystem():166 # Writing the file in the filesystem.167 with open('hello.txt', 'w') as f:168 f.write('Hello World!')169 invoke(echo, env={'SRC': 'hello.txt'})170171172Checking multiple environment variables:173174.. click:example::175176 @click.command()177 @click.argument('src', envvar=['SRC', 'SRC_2'], type=click.File('r'))178 def echo(src):179 """Print value of SRC environment variable."""180 click.echo(src.read())181182And from the command line:183184.. click:run::185186 with isolated_filesystem():187 # Writing the file in the filesystem.188 with open('hello.txt', 'w') as f:189 f.write('Hello World from second variable!')190 invoke(echo, env={'SRC_2': 'hello.txt'})191```