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 fileTake it
Move Arguments doc from reST to MyST (#3267)Rowlando13before adoptionc8111c2
lines 1–10why, one step back1(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 back11* Similar to a limited version of {ref}`options <options>` that12 can take an arbitrary number of inputsMerge main into stable (#3800)Rowlando13before adoption420c8fb
line 13why, one step back13* 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 back14 section of the help page, or be {ref}`documented in the command docstring15 <documenting-arguments>`.Move Arguments doc from reST to MyST (#3267)Rowlando13before adoptionc8111c2
lines 16–18why, one step back1617Useful and often used kwargs are:18Add help parameter to arguments (#3473)Kevin Deldyckebefore adoptionc480210
line 19why, one step back19* `help`: Help text for the argument.Move Arguments doc from reST to MyST (#3267)Rowlando13before adoptionc8111c2
lines 20–24why, one step back20* `default`: Passes a default.21* `nargs`: Sets the number of arguments. Set to -1 to take an arbitrary number.2223## Basic Arguments24Apply format guidelines to commits since latest 8.3.2 release (#3343)Rowlando13before adoptionf1f191e
lines 25–27why, one step back25A 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 back2829Example: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```46Apply format guidelines to commits since latest 8.3.2 release (#3343)Rowlando13before adoptionf1f191e
line 47why, one step back47An 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 back48If no type is provided, the type of the default value is used if it is49recognized, 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 back5253```{admonition} Note on Required Arguments54:class: note55Apply format guidelines to commits since latest 8.3.2 release (#3343)Rowlando13before adoptionf1f191e
lines 56–60why, one step back56 It is possible to make an argument required by setting57 `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 back61```6263## Multiple Arguments64Apply format guidelines to commits since latest 8.3.2 release (#3343)Rowlando13before adoptionf1f191e
lines 65–69why, one step back65To set the number of argument use the `nargs` kwarg. It can be set to66any 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 back7071```{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: note91Apply format guidelines to commits since latest 8.3.2 release (#3343)Rowlando13before adoptionf1f191e
lines 92–94why, one step back92 This is not how you should handle files and files paths. This93 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 back95```9697## Argument Escape Sequences98Apply format guidelines to commits since latest 8.3.2 release (#3343)Rowlando13before adoptionf1f191e
lines 99–102why, one step back99If you want to process arguments that look like options, like a file100named `-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 back103104Example 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```122Apply format guidelines to commits since latest 8.3.2 release (#3343)Rowlando13before adoptionf1f191e
lines 123–124why, one step back123If you don't like the `--` marker, you can set124`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 back125126```{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 Variables146Apply format guidelines to commits since latest 8.3.2 release (#3343)Rowlando13before adoptionf1f191e
lines 147–148why, one step back147Arguments can use environment variables. To do so, pass the name(s) of148the environment variable(s) via `envvar` in `click.argument`.Move Arguments doc from reST to MyST (#3267)Rowlando13before adoptionc8111c2
lines 149–191why, one step back149150Checking 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```