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 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 inputsPre-8.5.0 release changelog and doc fixes (#3697)Rowlando13before adoption00e592c
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
line 14why, one step back14 section of the help page, or be {ref}`documented in the command docstringMerge stable into main (#3876)Rowlando13before adoption3cbaa76
lines 15–16why, one step back15 <documenting-arguments>`. One argument with a `help` makes the section list16 all the arguments of the command.Move Arguments doc from reST to MyST (#3267)Rowlando13before adoptionc8111c2
lines 17–19why, one step back1718Useful and often used kwargs are:19Add help parameter to arguments (#3473)Kevin Deldyckebefore adoptionc480210
line 20why, one step back20* `help`: Help text for the argument.Move Arguments doc from reST to MyST (#3267)Rowlando13before adoptionc8111c2
lines 21–25why, one step back21* `default`: Passes a default.22* `nargs`: Sets the number of arguments. Set to -1 to take an arbitrary number.2324## Basic Arguments25Apply format guidelines to commits since latest 8.3.2 release (#3343)Rowlando13before adoptionf1f191e
lines 26–28why, one step back26A 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 back2930Example: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```47Apply format guidelines to commits since latest 8.3.2 release (#3343)Rowlando13before adoptionf1f191e
line 48why, one step back48An argument may be assigned a {ref}`parameter type <parameter-types>`.Stable (#3851)Rowlando13before adoption6aabf09
lines 49–52why, one step back49If no type is provided, the type of the default value is used if it is50recognized, 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 back5354```{admonition} Note on Required Arguments55:class: note56Apply format guidelines to commits since latest 8.3.2 release (#3343)Rowlando13before adoptionf1f191e
lines 57–61why, one step back57 It is possible to make an argument required by setting58 `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 back62```6364## Multiple Arguments65Apply format guidelines to commits since latest 8.3.2 release (#3343)Rowlando13before adoptionf1f191e
lines 66–70why, one step back66To set the number of argument use the `nargs` kwarg. It can be set to67any 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 back7172```{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: note92Apply format guidelines to commits since latest 8.3.2 release (#3343)Rowlando13before adoptionf1f191e
lines 93–95why, one step back93 This is not how you should handle files and files paths. This94 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 back96```9798## Argument Escape Sequences99Apply format guidelines to commits since latest 8.3.2 release (#3343)Rowlando13before adoptionf1f191e
lines 100–103why, one step back100If you want to process arguments that look like options, like a file101named `-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 back104105Example 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```123Apply format guidelines to commits since latest 8.3.2 release (#3343)Rowlando13before adoptionf1f191e
lines 124–125why, one step back124If you don't like the `--` marker, you can set125`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 back126127```{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 Variables147Apply format guidelines to commits since latest 8.3.2 release (#3343)Rowlando13before adoptionf1f191e
lines 148–149why, one step back148Arguments can use environment variables. To do so, pass the name(s) of149the environment variable(s) via `envvar` in `click.argument`.Move Arguments doc from reST to MyST (#3267)Rowlando13before adoptionc8111c2
lines 150–192why, one step back150151Checking 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```