You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
* ``nargs``: Sets the number of arguments. Set to -1 to take an arbitrary number.
13
18
14
19
Basic Arguments
15
20
---------------
16
21
17
-
The most basic option is a simple string argument of one value. If no
18
-
type is provided, the type of the default value is used, and if no default
19
-
value is provided, the type is assumed to be :data:`STRING`.
22
+
A 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``.
20
23
21
24
Example:
22
25
23
26
.. click:example::
24
27
25
28
@click.command()
26
29
@click.argument('filename')
27
-
def touch(filename):
30
+
def touch(filename: str):
28
31
"""Print FILENAME."""
29
32
click.echo(filename)
30
33
31
-
And what it looks like:
34
+
And from the command line:
32
35
33
36
.. click:run::
34
37
35
38
invoke(touch, args=['foo.txt'])
36
39
37
-
Variadic Arguments
38
-
------------------
39
40
40
-
The second most common version is variadic arguments where a specific (or
41
-
unlimited) number of arguments is accepted. This can be controlled with
42
-
the ``nargs`` parameter. If it is set to ``-1``, then an unlimited number
43
-
of arguments is accepted.
41
+
An 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`.
44
42
45
-
The value is then passed as a tuple. Note that only one argument can be
46
-
set to ``nargs=-1``, as it will eat up all arguments.
43
+
.. admonition:: Note on Required Arguments
47
44
48
-
Example:
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.
46
+
47
+
Multiple Arguments
48
+
-----------------------------------
49
+
50
+
To 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.
.. admonition:: Note on Non-Empty Variadic Arguments
68
+
.. admonition:: Note on Handling Files
73
69
74
-
If you come from ``argparse``, you might be missing support for setting
75
-
``nargs`` to ``+`` to indicate that at least one argument is required.
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.
76
71
77
-
This is supported by setting ``required=True``. However, this should
78
-
not be used if you can avoid it as we believe scripts should gracefully
79
-
degrade into becoming noops if a variadic argument is empty. The
80
-
reason for this is that very often, scripts are invoked with wildcard
81
-
inputs from the command line and they should not error out if the
82
-
wildcard is empty.
72
+
Argument Escape Sequences
73
+
---------------------------
83
74
84
-
.. _file-args:
75
+
If 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.
85
76
86
-
File Arguments
87
-
--------------
88
-
89
-
Since all the examples have already worked with filenames, it makes sense
90
-
to explain how to deal with files properly. Command line tools are more
91
-
fun if they work with files the Unix way, which is to accept ``-`` as a
92
-
special file that refers to stdin/stdout.
93
-
94
-
Click supports this through the :class:`click.File` type which
95
-
intelligently handles files for you. It also deals with Unicode and bytes
96
-
correctly for all versions of Python so your script stays very portable.
0 commit comments