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 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 inputsMove Arguments doc from reST to MyST (#3267)Rowlando13before adoptionc8111c2
lines 13–21why, one step back13* {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 Arguments21Apply format guidelines to commits since latest 8.3.2 release (#3343)Rowlando13before adoptionf1f191e
lines 22–24why, one step back22A 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 back2526Example: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```43Apply format guidelines to commits since latest 8.3.2 release (#3343)Rowlando13before adoptionf1f191e
lines 44–46why, one step back44An 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 back4748```{admonition} Note on Required Arguments49:class: note50Apply format guidelines to commits since latest 8.3.2 release (#3343)Rowlando13before adoptionf1f191e
lines 51–55why, one step back51 It is possible to make an argument required by setting52 `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 back56```5758## Multiple Arguments59Apply format guidelines to commits since latest 8.3.2 release (#3343)Rowlando13before adoptionf1f191e
lines 60–64why, one step back60To set the number of argument use the `nargs` kwarg. It can be set to61any 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 back6566```{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: note86Apply format guidelines to commits since latest 8.3.2 release (#3343)Rowlando13before adoptionf1f191e
lines 87–89why, one step back87 This is not how you should handle files and files paths. This88 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 back90```9192## Argument Escape Sequences93Apply format guidelines to commits since latest 8.3.2 release (#3343)Rowlando13before adoptionf1f191e
lines 94–97why, one step back94If you want to process arguments that look like options, like a file95named `-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 back9899Example 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```117Apply format guidelines to commits since latest 8.3.2 release (#3343)Rowlando13before adoptionf1f191e
lines 118–119why, one step back118If you don't like the `--` marker, you can set119`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 back120121```{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 Variables141Apply format guidelines to commits since latest 8.3.2 release (#3343)Rowlando13before adoptionf1f191e
lines 142–143why, one step back142Arguments can use environment variables. To do so, pass the name(s) of143the environment variable(s) via `envvar` in `click.argument`.Move Arguments doc from reST to MyST (#3267)Rowlando13before adoptionc8111c2
lines 144–186why, one step back144145Checking 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```