Debian Codegraph @

docs/arguments.md

raw
1 (arguments)=
2
3 # Arguments
4
5 ```{currentmodule} click
6 ```
7
8 Arguments are:
9
10 * Are positional in nature.
11 * Similar to a limited version of {ref}`options <options>` that
12 can take an arbitrary number of inputs
13 * {ref}`Documented manually <documenting-arguments>`.
14
15 Useful and often used kwargs are:
16
17 * `default`: Passes a default.
18 * `nargs`: Sets the number of arguments. Set to -1 to take an arbitrary number.
19
20 ## Basic Arguments
21
22 A minimal {class}`click.Argument` solely takes one string argument:
23 the name of the argument. This will assume the argument is required,
24 has no default, and is of the type `str`.
25
26 Example:
27
28 ```{eval-rst}
29 .. click:example::
30
31 @click.command()
32 @click.argument('filename')
33 def touch(filename: str):
34 """Print FILENAME."""
35 click.echo(filename)
36
37 And from the command line:
38
39 .. click:run::
40
41 invoke(touch, args=['foo.txt'])
42 ```
43
44 An argument may be assigned a {ref}`parameter type <parameter-types>`.
45 If no type is provided, the type of the default value is used. If no
46 default value is provided, the type is assumed to be {data}`STRING`.
47
48 ```{admonition} Note on Required Arguments
49 :class: note
50
51 It is possible to make an argument required by setting
52 `required=True`. It is not recommended since we think command line
53 tools should gracefully degrade into becoming no ops. We think this
54 because command line tools are often invoked with wildcard inputs
55 and they should not error out if the wildcard is empty.
56 ```
57
58 ## Multiple Arguments
59
60 To set the number of argument use the `nargs` kwarg. It can be set to
61 any positive integer and -1. Setting it to -1, makes the number of
62 arguments arbitrary (which is called variadic) and can only be used
63 once. The arguments are then packed as a tuple and passed to the
64 function.
65
66 ```{eval-rst}
67 .. click:example::
68
69 @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}")
76
77 And from the command line:
78
79 .. click:run::
80
81 invoke(copy, args=['foo.txt', 'usr/david/foo.txt', 'usr/mitsuko/foo.txt'])
82 ```
83
84 ```{admonition} Note on Handling Files
85 :class: note
86
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 learn
89 more about how to handle files in parameters.
90 ```
91
92 ## Argument Escape Sequences
93
94 If you want to process arguments that look like options, like a file
95 named `-foo.txt` or `--foo.txt`, you must pass the `--` separator
96 first. After you pass the `--`, you may only pass arguments. This is a
97 common feature for POSIX command line tools.
98
99 Example usage:
100
101 ```{eval-rst}
102 .. click:example::
103
104 @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)
110
111 And from the command line:
112
113 .. click:run::
114
115 invoke(touch, ['--', '-foo.txt', 'bar.txt'])
116 ```
117
118 If you don't like the `--` marker, you can set
119 `ignore_unknown_options` to `True` to avoid checking unknown options:
120
121 ```{eval-rst}
122 .. click:example::
123
124 @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)
130
131 And from the command line:
132
133 .. click:run::
134
135 invoke(touch, ['-foo.txt', 'bar.txt'])
136 ```
137
138 (environment-variables)=
139
140 ## Environment Variables
141
142 Arguments can use environment variables. To do so, pass the name(s) of
143 the environment variable(s) via `envvar` in `click.argument`.
144
145 Checking one environment variable:
146
147 ```{eval-rst}
148 .. click:example::
149
150 @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())
155
156 And from the command line:
157
158 .. click:run::
159
160 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'})
165
166
167 Checking multiple environment variables:
168
169 .. click:example::
170
171 @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())
176
177 And from the command line:
178
179 .. click:run::
180
181 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 ```