Skip to content
Myr

arguments.md

This file

Path

docs/arguments.md

189 lines · 4.9 KiB · Markdown

Last changed by

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

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

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

lines 47–49why, one step back
47An argument may be assigned a {ref}`parameter type <parameter-types>`.
48If no type is provided, the type of the default value is used. If no49default value is provided, the type is assumed to be {data}`STRING`.

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

lines 50–53why, one step back
50
51```{admonition} Note on Required Arguments52:class: note53

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

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

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

lines 59–62why, one step back
59```
6061## Multiple Arguments62

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

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

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

lines 68–89why, one step back
68
69```{eval-rst}70.. click:example::7172 @click.command()73 @click.argument('src', nargs=1)74 @click.argument('dsts', nargs=-1)75 def copy(src: str, dsts: tuple[str, ...]):76 """Move file SRC to DST."""77 for destination in dsts:78 click.echo(f"Copy {src} to folder {destination}")7980And from the command line:8182.. click:run::8384 invoke(copy, args=['foo.txt', 'usr/david/foo.txt', 'usr/mitsuko/foo.txt'])85```8687```{admonition} Note on Handling Files88:class: note89

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

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

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

lines 93–96why, one step back
93```
9495## Argument Escape Sequences96

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

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

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

lines 101–120why, one step back
101
102Example usage:103104```{eval-rst}105.. click:example::106107 @click.command()108 @click.argument('files', nargs=-1, type=click.Path())109 def touch(files):110 """Print all FILES file names."""111 for filename in files:112 click.echo(filename)113114And from the command line:115116.. click:run::117118 invoke(touch, ['--', '-foo.txt', 'bar.txt'])119```120

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

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

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

lines 123–144why, one step back
123
124```{eval-rst}125.. click:example::126127 @click.command(context_settings={"ignore_unknown_options": True})128 @click.argument('files', nargs=-1, type=click.Path())129 def touch(files):130 """Print all FILES file names."""131 for filename in files:132 click.echo(filename)133134And from the command line:135136.. click:run::137138 invoke(touch, ['-foo.txt', 'bar.txt'])139```140141(environment-variables)=142143## Environment Variables144

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

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

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

lines 147–189why, one step back
147
148Checking one environment variable:149150```{eval-rst}151.. click:example::152153 @click.command()154 @click.argument('src', envvar='SRC', type=click.File('r'))155 def echo(src):156 """Print value of SRC environment variable."""157 click.echo(src.read())158159And from the command line:160161.. click:run::162163 with isolated_filesystem():164 # Writing the file in the filesystem.165 with open('hello.txt', 'w') as f:166 f.write('Hello World!')167 invoke(echo, env={'SRC': 'hello.txt'})168169170Checking multiple environment variables:171172.. click:example::173174 @click.command()175 @click.argument('src', envvar=['SRC', 'SRC_2'], type=click.File('r'))176 def echo(src):177 """Print value of SRC environment variable."""178 click.echo(src.read())179180And from the command line:181182.. click:run::183184 with isolated_filesystem():185 # Writing the file in the filesystem.186 with open('hello.txt', 'w') as f:187 f.write('Hello World from second variable!')188 invoke(echo, env={'SRC_2': 'hello.txt'})189```