Skip to content
Myr

arguments.rst

This file

Path

docs/arguments.rst

158 lines · 4.7 KiB

Last changed by

Merge `stable` into `main`; Release 8.2.0 (#2873)Rowlando13before adoptionb7c0ab4

History of this file

Take it

The walk stopped after 200 commits: lines older than 55b2715 point at it.

Merge pull request #2203 from pallets/command-decorator-paramsDavid Lordbefore adoption55b2715

lines 1–7why, one step back
1.. _arguments:
23Arguments4=========56.. currentmodule:: click7

Merge `stable` into `main`; Release 8.2.0 (#2873)Rowlando13before adoptionb7c0ab4

line 8why, one step back
8Arguments are:

Merge pull request #2203 from pallets/command-decorator-paramsDavid Lordbefore adoption55b2715

line 9why, one step back
9

Merge `stable` into `main`; Release 8.2.0 (#2873)Rowlando13before adoptionb7c0ab4

lines 10–18why, one step back
10* Are positional in nature.
11* Similar to a limited version of :ref:`options <options>` that can take an arbitrary number of inputs12* :ref:`Documented manually <documenting-arguments>`.1314Useful and often used kwargs are:1516* ``default``: Passes a default.17* ``nargs``: Sets the number of arguments. Set to -1 to take an arbitrary number.18

Merge pull request #2203 from pallets/command-decorator-paramsDavid Lordbefore adoption55b2715

lines 19–21why, one step back
19Basic Arguments
20---------------21

Merge `stable` into `main`; Release 8.2.0 (#2873)Rowlando13before adoptionb7c0ab4

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

Merge pull request #2203 from pallets/command-decorator-paramsDavid Lordbefore adoption55b2715

lines 23–29why, one step back
23
24Example:2526.. click:example::2728 @click.command()29 @click.argument('filename')

Merge `stable` into `main`; Release 8.2.0 (#2873)Rowlando13before adoptionb7c0ab4

line 30why, one step back
30 def touch(filename: str):

Merge pull request #2203 from pallets/command-decorator-paramsDavid Lordbefore adoption55b2715

lines 31–33why, one step back
31 """Print FILENAME."""
32 click.echo(filename)33

Merge `stable` into `main`; Release 8.2.0 (#2873)Rowlando13before adoptionb7c0ab4

line 34why, one step back
34And from the command line:

Merge pull request #2203 from pallets/command-decorator-paramsDavid Lordbefore adoption55b2715

lines 35–40why, one step back
35
36.. click:run::3738 invoke(touch, args=['foo.txt'])3940

Merge `stable` into `main`; Release 8.2.0 (#2873)Rowlando13before adoptionb7c0ab4

line 41why, one step back
41An argument may be assigned a :ref:`parameter type <parameter-types>`. If no type is provided, the type of the default value is used. If no default value is provided, the type is assumed to be :data:`STRING`.

Merge pull request #2203 from pallets/command-decorator-paramsDavid Lordbefore adoption55b2715

line 42why, one step back
42

Merge `stable` into `main`; Release 8.2.0 (#2873)Rowlando13before adoptionb7c0ab4

line 43why, one step back
43.. admonition:: Note on Required Arguments

Merge pull request #2203 from pallets/command-decorator-paramsDavid Lordbefore adoption55b2715

line 44why, one step back
44

Merge `stable` into `main`; Release 8.2.0 (#2873)Rowlando13before adoptionb7c0ab4

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

Merge pull request #2203 from pallets/command-decorator-paramsDavid Lordbefore adoption55b2715

line 46why, one step back
46

Merge `stable` into `main`; Release 8.2.0 (#2873)Rowlando13before adoptionb7c0ab4

lines 47–51why, one step back
47Multiple Arguments
48-----------------------------------4950To set the number of argument use the ``nargs`` kwarg. It can be set to any positive integer and -1. Setting it to -1, makes the number of arguments arbitrary (which is called variadic) and can only be used once. The arguments are then packed as a tuple and passed to the function.51

Merge pull request #2203 from pallets/command-decorator-paramsDavid Lordbefore adoption55b2715

lines 52–54why, one step back
52.. click:example::
5354 @click.command()

Merge `stable` into `main`; Release 8.2.0 (#2873)Rowlando13before adoptionb7c0ab4

lines 55–57why, one step back
55 @click.argument('src', nargs=1)
56 @click.argument('dsts', nargs=-1)57 def copy(src: str, dsts: tuple[str, ...]):

Merge pull request #2203 from pallets/command-decorator-paramsDavid Lordbefore adoption55b2715

line 58why, one step back
58 """Move file SRC to DST."""

Merge `stable` into `main`; Release 8.2.0 (#2873)Rowlando13before adoptionb7c0ab4

lines 59–60why, one step back
59 for destination in dsts:
60 click.echo(f"Copy {src} to folder {destination}")

Merge pull request #2203 from pallets/command-decorator-paramsDavid Lordbefore adoption55b2715

line 61why, one step back
61

Merge `stable` into `main`; Release 8.2.0 (#2873)Rowlando13before adoptionb7c0ab4

line 62why, one step back
62And from the command line:

Merge pull request #2203 from pallets/command-decorator-paramsDavid Lordbefore adoption55b2715

lines 63–65why, one step back
63
64.. click:run::65

Merge `stable` into `main`; Release 8.2.0 (#2873)Rowlando13before adoptionb7c0ab4

line 66why, one step back
66 invoke(copy, args=['foo.txt', 'usr/david/foo.txt', 'usr/mitsuko/foo.txt'])

Merge pull request #2203 from pallets/command-decorator-paramsDavid Lordbefore adoption55b2715

line 67why, one step back
67

Merge `stable` into `main`; Release 8.2.0 (#2873)Rowlando13before adoptionb7c0ab4

line 68why, one step back
68.. admonition:: Note on Handling Files

Merge pull request #2203 from pallets/command-decorator-paramsDavid Lordbefore adoption55b2715

line 69why, one step back
69

Merge `stable` into `main`; Release 8.2.0 (#2873)Rowlando13before adoptionb7c0ab4

line 70why, one step back
70 This is not how you should handle files and files paths. This merely used as a simple example. See :ref:`handling-files` to learn more about how to handle files in parameters.

Merge pull request #2203 from pallets/command-decorator-paramsDavid Lordbefore adoption55b2715

line 71why, one step back
71

Merge `stable` into `main`; Release 8.2.0 (#2873)Rowlando13before adoptionb7c0ab4

lines 72–73why, one step back
72Argument Escape Sequences
73---------------------------

Merge pull request #2203 from pallets/command-decorator-paramsDavid Lordbefore adoption55b2715

line 74why, one step back
74

Merge `stable` into `main`; Release 8.2.0 (#2873)Rowlando13before adoptionb7c0ab4

line 75why, one step back
75If you want to process arguments that look like options, like a file named ``-foo.txt`` or ``--foo.txt`` , you must pass the ``--`` separator first. After you pass the ``--``, you may only pass arguments. This is a common feature for POSIX command line tools.

Merge pull request #2203 from pallets/command-decorator-paramsDavid Lordbefore adoption55b2715

line 76why, one step back
76

Merge `stable` into `main`; Release 8.2.0 (#2873)Rowlando13before adoptionb7c0ab4

line 77why, one step back
77Example usage:

Merge pull request #2203 from pallets/command-decorator-paramsDavid Lordbefore adoption55b2715

lines 78–81why, one step back
78
79.. click:example::8081 @click.command()

Merge `stable` into `main`; Release 8.2.0 (#2873)Rowlando13before adoptionb7c0ab4

lines 82–86why, one step back
82 @click.argument('files', nargs=-1, type=click.Path())
83 def touch(files):84 """Print all FILES file names."""85 for filename in files:86 click.echo(filename)

Merge pull request #2203 from pallets/command-decorator-paramsDavid Lordbefore adoption55b2715

line 87why, one step back
87

Merge `stable` into `main`; Release 8.2.0 (#2873)Rowlando13before adoptionb7c0ab4

line 88why, one step back
88And from the command line:

Merge pull request #2203 from pallets/command-decorator-paramsDavid Lordbefore adoption55b2715

lines 89–91why, one step back
89
90.. click:run::91

Merge `stable` into `main`; Release 8.2.0 (#2873)Rowlando13before adoptionb7c0ab4

line 92why, one step back
92 invoke(touch, ['--', '-foo.txt', 'bar.txt'])

Merge pull request #2203 from pallets/command-decorator-paramsDavid Lordbefore adoption55b2715

line 93why, one step back
93

Merge `stable` into `main`; Release 8.2.0 (#2873)Rowlando13before adoptionb7c0ab4

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

Merge pull request #2203 from pallets/command-decorator-paramsDavid Lordbefore adoption55b2715

lines 95–97why, one step back
95
96.. click:example::97

Merge `stable` into `main`; Release 8.2.0 (#2873)Rowlando13before adoptionb7c0ab4

lines 98–103why, one step back
98 @click.command(context_settings={"ignore_unknown_options": True})
99 @click.argument('files', nargs=-1, type=click.Path())100 def touch(files):101 """Print all FILES file names."""102 for filename in files:103 click.echo(filename)

Merge pull request #2203 from pallets/command-decorator-paramsDavid Lordbefore adoption55b2715

line 104why, one step back
104

Merge `stable` into `main`; Release 8.2.0 (#2873)Rowlando13before adoptionb7c0ab4

line 105why, one step back
105And from the command line:

Merge pull request #2203 from pallets/command-decorator-paramsDavid Lordbefore adoption55b2715

lines 106–108why, one step back
106
107.. click:run::108

Merge `stable` into `main`; Release 8.2.0 (#2873)Rowlando13before adoptionb7c0ab4

line 109why, one step back
109 invoke(touch, ['-foo.txt', 'bar.txt'])

Merge pull request #2203 from pallets/command-decorator-paramsDavid Lordbefore adoption55b2715

lines 110–111why, one step back
110
111

Merge stable into main (#2799)Rowlando13before adoptiond791537

lines 112–113why, one step back
112.. _environment-variables:
113

Merge pull request #2203 from pallets/command-decorator-paramsDavid Lordbefore adoption55b2715

lines 114–116why, one step back
114Environment Variables
115---------------------116

Merge `stable` into `main`; Release 8.2.0 (#2873)Rowlando13before adoptionb7c0ab4

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

Merge pull request #2203 from pallets/command-decorator-paramsDavid Lordbefore adoption55b2715

line 118why, one step back
118

Merge `stable` into `main`; Release 8.2.0 (#2873)Rowlando13before adoptionb7c0ab4

line 119why, one step back
119Checking one environment variable:

Merge pull request #2203 from pallets/command-decorator-paramsDavid Lordbefore adoption55b2715

lines 120–133why, one step back
120
121.. click:example::122123 @click.command()124 @click.argument('src', envvar='SRC', type=click.File('r'))125 def echo(src):126 """Print value of SRC environment variable."""127 click.echo(src.read())128129And from the command line:130131.. click:run::132133 with isolated_filesystem():

Merge `stable` into `main`; Release 8.2.0 (#2873)Rowlando13before adoptionb7c0ab4

line 134why, one step back
134 # Writing the file in the filesystem.

Merge pull request #2203 from pallets/command-decorator-paramsDavid Lordbefore adoption55b2715

lines 135–139why, one step back
135 with open('hello.txt', 'w') as f:
136 f.write('Hello World!')137 invoke(echo, env={'SRC': 'hello.txt'})138139

Merge `stable` into `main`; Release 8.2.0 (#2873)Rowlando13before adoptionb7c0ab4

line 140why, one step back
140Checking multiple environment variables:

Merge pull request #2203 from pallets/command-decorator-paramsDavid Lordbefore adoption55b2715

lines 141–144why, one step back
141
142.. click:example::143144 @click.command()

Merge `stable` into `main`; Release 8.2.0 (#2873)Rowlando13before adoptionb7c0ab4

lines 145–148why, one step back
145 @click.argument('src', envvar=['SRC', 'SRC_2'], type=click.File('r'))
146 def echo(src):147 """Print value of SRC environment variable."""148 click.echo(src.read())

Merge pull request #2203 from pallets/command-decorator-paramsDavid Lordbefore adoption55b2715

lines 149–153why, one step back
149
150And from the command line:151152.. click:run::153

Merge `stable` into `main`; Release 8.2.0 (#2873)Rowlando13before adoptionb7c0ab4

lines 154–158why, one step back
154 with isolated_filesystem():
155 # Writing the file in the filesystem.156 with open('hello.txt', 'w') as f:157 f.write('Hello World from second variable!')158 invoke(echo, env={'SRC_2': 'hello.txt'})