Skip to content
Myr

arguments.md

This file

Path

docs/arguments.md

186 lines · 4.7 KiB · Markdown

Last changed by

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

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

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

lines 13–21why, one step back
13* {ref}`Documented manually <documenting-arguments>`.
1415Useful and often used kwargs are:1617* `default`: Passes a default.18* `nargs`: Sets the number of arguments. Set to -1 to take an arbitrary number.1920## Basic Arguments21

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

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

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

lines 25–43why, one step back
25
26Example:2728```{eval-rst}29.. click:example::3031 @click.command()32 @click.argument('filename')33 def touch(filename: str):34 """Print FILENAME."""35 click.echo(filename)3637And from the command line:3839.. click:run::4041 invoke(touch, args=['foo.txt'])42```43

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

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

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

lines 47–50why, one step back
47
48```{admonition} Note on Required Arguments49:class: note50

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

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

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

lines 56–59why, one step back
56```
5758## Multiple Arguments59

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

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

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

lines 65–86why, one step back
65
66```{eval-rst}67.. click:example::6869 @click.command()70 @click.argument('src', nargs=1)71 @click.argument('dsts', nargs=-1)72 def copy(src: str, dsts: tuple[str, ...]):73 """Move file SRC to DST."""74 for destination in dsts:75 click.echo(f"Copy {src} to folder {destination}")7677And from the command line:7879.. click:run::8081 invoke(copy, args=['foo.txt', 'usr/david/foo.txt', 'usr/mitsuko/foo.txt'])82```8384```{admonition} Note on Handling Files85:class: note86

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

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

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

lines 90–93why, one step back
90```
9192## Argument Escape Sequences93

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

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

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

lines 98–117why, one step back
98
99Example usage:100101```{eval-rst}102.. click:example::103104 @click.command()105 @click.argument('files', nargs=-1, type=click.Path())106 def touch(files):107 """Print all FILES file names."""108 for filename in files:109 click.echo(filename)110111And from the command line:112113.. click:run::114115 invoke(touch, ['--', '-foo.txt', 'bar.txt'])116```117

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

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

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

lines 120–141why, one step back
120
121```{eval-rst}122.. click:example::123124 @click.command(context_settings={"ignore_unknown_options": True})125 @click.argument('files', nargs=-1, type=click.Path())126 def touch(files):127 """Print all FILES file names."""128 for filename in files:129 click.echo(filename)130131And from the command line:132133.. click:run::134135 invoke(touch, ['-foo.txt', 'bar.txt'])136```137138(environment-variables)=139140## Environment Variables141

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

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

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

lines 144–186why, one step back
144
145Checking one environment variable:146147```{eval-rst}148.. click:example::149150 @click.command()151 @click.argument('src', envvar='SRC', type=click.File('r'))152 def echo(src):153 """Print value of SRC environment variable."""154 click.echo(src.read())155156And from the command line:157158.. click:run::159160 with isolated_filesystem():161 # Writing the file in the filesystem.162 with open('hello.txt', 'w') as f:163 f.write('Hello World!')164 invoke(echo, env={'SRC': 'hello.txt'})165166167Checking multiple environment variables:168169.. click:example::170171 @click.command()172 @click.argument('src', envvar=['SRC', 'SRC_2'], type=click.File('r'))173 def echo(src):174 """Print value of SRC environment variable."""175 click.echo(src.read())176177And from the command line:178179.. click:run::180181 with isolated_filesystem():182 # Writing the file in the filesystem.183 with open('hello.txt', 'w') as f:184 f.write('Hello World from second variable!')185 invoke(echo, env={'SRC_2': 'hello.txt'})186```