| 1 |
(parameters)= |
| 2 |
|
| 3 |
# Parameters |
| 4 |
|
| 5 |
```{currentmodule} click |
| 6 |
``` |
| 7 |
|
| 8 |
Click supports only two principle types of parameters for scripts (by design): options and arguments. |
| 9 |
|
| 10 |
## Options |
| 11 |
|
| 12 |
- Are optional. |
| 13 |
- Recommended to use for everything except subcommands, urls, or files. |
| 14 |
- Can take a fixed number of arguments. The default is 1. They may be specified multiple times using {ref}`multiple-options`. |
| 15 |
- Are fully documented by the help page. |
| 16 |
- Have automatic prompting for missing input. |
| 17 |
- Can act as flags (boolean or otherwise). |
| 18 |
- Can be pulled from environment variables. |
| 19 |
|
| 20 |
## Arguments |
| 21 |
|
| 22 |
- Are optional with in reason, but not entirely so. |
| 23 |
- Recommended to use for subcommands, urls, or files. |
| 24 |
- Can take an arbitrary number of arguments. |
| 25 |
- Are not fully documented by the help page since they may be too specific to be automatically documented. For more see {ref}`documenting-arguments`. |
| 26 |
- Can be pulled from environment variables but only explicitly named ones. For more see {ref}`environment-variables`. |
| 27 |
|
| 28 |
On each principle type you can specify {ref}`parameter-types`. Specifying these types helps Click add details to your help pages and help with the handling of those types. |
| 29 |
|
| 30 |
(parameter-names)= |
| 31 |
|
| 32 |
## Parameter Names |
| 33 |
|
| 34 |
Parameters (options and arguments) have a name that will be used as |
| 35 |
the Python argument name when calling the decorated function with |
| 36 |
values. |
| 37 |
|
| 38 |
In the example, the argument's name is `filename`. The name must match the python arg name. To provide a different name for use in help text, see {ref}`doc-meta-variables`. |
| 39 |
The option's names are `-t` and `--times`. More names are available for options and are covered in {ref}`options`. |
| 40 |
|
| 41 |
```{eval-rst} |
| 42 |
.. click:example:: |
| 43 |
|
| 44 |
@click.command() |
| 45 |
@click.argument('filename') |
| 46 |
@click.option('-t', '--times', type=int) |
| 47 |
def multi_echo(filename, times): |
| 48 |
"""Print value filename multiple times.""" |
| 49 |
for x in range(times): |
| 50 |
click.echo(filename) |
| 51 |
|
| 52 |
.. click:run:: |
| 53 |
|
| 54 |
invoke(multi_echo, ['--times=3', 'index.txt'], prog_name='multi_echo') |
| 55 |
``` |