click 8.3.2__tar.gz → 8.3.3__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {click-8.3.2 → click-8.3.3}/CHANGES.rst +43 -2
- {click-8.3.2 → click-8.3.3}/PKG-INFO +1 -1
- click-8.3.2/docs/arguments.rst → click-8.3.3/docs/arguments.md +58 -30
- click-8.3.3/docs/click-concepts.md +68 -0
- click-8.3.2/docs/commands-and-groups.rst → click-8.3.3/docs/commands-and-groups.md +151 -67
- click-8.3.2/docs/commands.rst → click-8.3.3/docs/commands.md +150 -140
- click-8.3.3/docs/contributing.md +21 -0
- {click-8.3.2 → click-8.3.3}/docs/index.rst +2 -2
- {click-8.3.2 → click-8.3.3}/docs/options.md +172 -36
- click-8.3.3/docs/standalone-apps.md +148 -0
- {click-8.3.2 → click-8.3.3}/docs/testing.md +61 -6
- {click-8.3.2 → click-8.3.3}/pyproject.toml +15 -1
- {click-8.3.2 → click-8.3.3}/src/click/__init__.py +1 -0
- {click-8.3.2 → click-8.3.3}/src/click/_termui_impl.py +31 -13
- {click-8.3.2 → click-8.3.3}/src/click/core.py +73 -34
- {click-8.3.2 → click-8.3.3}/src/click/termui.py +10 -2
- {click-8.3.2 → click-8.3.3}/src/click/testing.py +101 -6
- {click-8.3.2 → click-8.3.3}/src/click/utils.py +4 -1
- {click-8.3.2 → click-8.3.3}/tests/test_defaults.py +20 -0
- {click-8.3.2 → click-8.3.3}/tests/test_imports.py +9 -1
- {click-8.3.2 → click-8.3.3}/tests/test_options.py +78 -6
- {click-8.3.2 → click-8.3.3}/tests/test_termui.py +281 -3
- {click-8.3.2 → click-8.3.3}/tests/test_testing.py +76 -0
- {click-8.3.2 → click-8.3.3}/tests/test_utils.py +2 -5
- click-8.3.3/uv.lock +1665 -0
- click-8.3.2/docs/click-concepts.rst +0 -67
- click-8.3.2/uv.lock +0 -1380
- {click-8.3.2 → click-8.3.3}/LICENSE.txt +0 -0
- {click-8.3.2 → click-8.3.3}/README.md +0 -0
- {click-8.3.2 → click-8.3.3}/docs/_static/click-icon.svg +0 -0
- {click-8.3.2 → click-8.3.3}/docs/_static/click-logo.svg +0 -0
- {click-8.3.2 → click-8.3.3}/docs/_static/click-name.svg +0 -0
- {click-8.3.2 → click-8.3.3}/docs/advanced.md +0 -0
- {click-8.3.2 → click-8.3.3}/docs/api.md +0 -0
- {click-8.3.2 → click-8.3.3}/docs/changes.rst +0 -0
- {click-8.3.2 → click-8.3.3}/docs/command-line-reference.md +0 -0
- {click-8.3.2 → click-8.3.3}/docs/complex.md +0 -0
- {click-8.3.2 → click-8.3.3}/docs/conf.py +0 -0
- {click-8.3.2 → click-8.3.3}/docs/contrib.md +0 -0
- {click-8.3.2 → click-8.3.3}/docs/design-opinions.md +0 -0
- {click-8.3.2 → click-8.3.3}/docs/documentation.md +0 -0
- {click-8.3.2 → click-8.3.3}/docs/entry-points.md +0 -0
- {click-8.3.2 → click-8.3.3}/docs/exceptions.md +0 -0
- {click-8.3.2 → click-8.3.3}/docs/extending-click.md +0 -0
- {click-8.3.2 → click-8.3.3}/docs/faqs.md +0 -0
- {click-8.3.2 → click-8.3.3}/docs/handling-files.md +0 -0
- {click-8.3.2 → click-8.3.3}/docs/license.md +0 -0
- {click-8.3.2 → click-8.3.3}/docs/option-decorators.md +0 -0
- {click-8.3.2 → click-8.3.3}/docs/parameter-types.md +0 -0
- {click-8.3.2 → click-8.3.3}/docs/parameters.md +0 -0
- {click-8.3.2 → click-8.3.3}/docs/prompts.md +0 -0
- {click-8.3.2 → click-8.3.3}/docs/quickstart.md +0 -0
- {click-8.3.2 → click-8.3.3}/docs/setuptools.md +0 -0
- {click-8.3.2 → click-8.3.3}/docs/shell-completion.md +0 -0
- {click-8.3.2 → click-8.3.3}/docs/support-multiple-versions.md +0 -0
- {click-8.3.2 → click-8.3.3}/docs/unicode-support.md +0 -0
- {click-8.3.2 → click-8.3.3}/docs/upgrade-guides.md +0 -0
- {click-8.3.2 → click-8.3.3}/docs/utils.md +0 -0
- {click-8.3.2 → click-8.3.3}/docs/virtualenv.md +0 -0
- {click-8.3.2 → click-8.3.3}/docs/why.md +0 -0
- {click-8.3.2 → click-8.3.3}/docs/wincmd.md +0 -0
- {click-8.3.2 → click-8.3.3}/src/click/_compat.py +0 -0
- {click-8.3.2 → click-8.3.3}/src/click/_textwrap.py +0 -0
- {click-8.3.2 → click-8.3.3}/src/click/_utils.py +0 -0
- {click-8.3.2 → click-8.3.3}/src/click/_winconsole.py +0 -0
- {click-8.3.2 → click-8.3.3}/src/click/decorators.py +0 -0
- {click-8.3.2 → click-8.3.3}/src/click/exceptions.py +0 -0
- {click-8.3.2 → click-8.3.3}/src/click/formatting.py +0 -0
- {click-8.3.2 → click-8.3.3}/src/click/globals.py +0 -0
- {click-8.3.2 → click-8.3.3}/src/click/parser.py +0 -0
- {click-8.3.2 → click-8.3.3}/src/click/py.typed +0 -0
- {click-8.3.2 → click-8.3.3}/src/click/shell_completion.py +0 -0
- {click-8.3.2 → click-8.3.3}/src/click/types.py +0 -0
- {click-8.3.2 → click-8.3.3}/tests/conftest.py +0 -0
- {click-8.3.2 → click-8.3.3}/tests/test_arguments.py +0 -0
- {click-8.3.2 → click-8.3.3}/tests/test_basic.py +0 -0
- {click-8.3.2 → click-8.3.3}/tests/test_chain.py +0 -0
- {click-8.3.2 → click-8.3.3}/tests/test_command_decorators.py +0 -0
- {click-8.3.2 → click-8.3.3}/tests/test_commands.py +0 -0
- {click-8.3.2 → click-8.3.3}/tests/test_compat.py +0 -0
- {click-8.3.2 → click-8.3.3}/tests/test_context.py +0 -0
- {click-8.3.2 → click-8.3.3}/tests/test_custom_classes.py +0 -0
- {click-8.3.2 → click-8.3.3}/tests/test_formatting.py +0 -0
- {click-8.3.2 → click-8.3.3}/tests/test_info_dict.py +0 -0
- {click-8.3.2 → click-8.3.3}/tests/test_normalization.py +0 -0
- {click-8.3.2 → click-8.3.3}/tests/test_parser.py +0 -0
- {click-8.3.2 → click-8.3.3}/tests/test_shell_completion.py +0 -0
- {click-8.3.2 → click-8.3.3}/tests/test_stream_lifecycle.py +0 -0
- {click-8.3.2 → click-8.3.3}/tests/test_types.py +0 -0
- {click-8.3.2 → click-8.3.3}/tests/typing/typing_aliased_group.py +0 -0
- {click-8.3.2 → click-8.3.3}/tests/typing/typing_confirmation_option.py +0 -0
- {click-8.3.2 → click-8.3.3}/tests/typing/typing_group_kw_options.py +0 -0
- {click-8.3.2 → click-8.3.3}/tests/typing/typing_help_option.py +0 -0
- {click-8.3.2 → click-8.3.3}/tests/typing/typing_options.py +0 -0
- {click-8.3.2 → click-8.3.3}/tests/typing/typing_password_option.py +0 -0
- {click-8.3.2 → click-8.3.3}/tests/typing/typing_progressbar.py +0 -0
- {click-8.3.2 → click-8.3.3}/tests/typing/typing_simple_example.py +0 -0
- {click-8.3.2 → click-8.3.3}/tests/typing/typing_version_option.py +0 -0
|
@@ -1,7 +1,48 @@
|
|
|
1
1
|
.. currentmodule:: click
|
|
2
2
|
|
|
3
|
+
Version 8.3.3
|
|
4
|
+
-------------
|
|
5
|
+
|
|
6
|
+
Released 2026-04-20
|
|
7
|
+
|
|
8
|
+
- Use :func:`shlex.split` to split pager and editor commands into ``argv``
|
|
9
|
+
lists for :class:`subprocess.Popen`, removing ``shell=True``.
|
|
10
|
+
:issue:`1026` :pr:`1477` :pr:`2775`
|
|
11
|
+
- Fix ``TypeError`` when rendering help for an option whose default value is
|
|
12
|
+
an object that doesn't support equality comparison with strings, such as
|
|
13
|
+
``semver.Version``. :issue:`3298` :pr:`3299`
|
|
14
|
+
- Fix pager test pollution under parallel execution by using pytest's
|
|
15
|
+
``tmp_path`` fixture instead of a shared temporary file path. :pr:`3238`
|
|
16
|
+
- Treat ``Sentinel.UNSET`` values in a ``default_map`` as absent, so they fall
|
|
17
|
+
through to the next default source instead of being used as the value.
|
|
18
|
+
:issue:`3224` :pr:`3240`
|
|
19
|
+
- Patch ``pdb.Pdb`` in ``CliRunner`` isolation so ``pdb.set_trace()``,
|
|
20
|
+
``breakpoint()``, and debuggers subclassing ``pdb.Pdb`` (ipdb, pdbpp) can
|
|
21
|
+
interact with the real terminal instead of the captured I/O streams.
|
|
22
|
+
:issue:`654` :issue:`824` :issue:`843` :pr:`951` :pr:`3235`
|
|
23
|
+
- Add optional randomized parallel test execution using ``pytest-randomly`` and
|
|
24
|
+
``pytest-xdist`` to detect test pollution and race conditions. :pr:`3151`
|
|
25
|
+
- Add contributor documentation for running stress tests, randomized
|
|
26
|
+
parallel tests, and Flask smoke tests. :pr:`3151` :pr:`3177`
|
|
27
|
+
- Show custom ``show_default`` string in prompts, matching the existing
|
|
28
|
+
help text behavior. :issue:`2836` :pr:`2837` :pr:`3165` :pr:`3262` :pr:`3280`
|
|
29
|
+
:pr:`3328`
|
|
30
|
+
- Fix ``default=True`` with boolean ``flag_value`` always returning the
|
|
31
|
+
``flag_value`` instead of ``True``. The ``default=True`` to ``flag_value``
|
|
32
|
+
substitution now only applies to non-boolean flags, where ``True`` acts as a
|
|
33
|
+
sentinel meaning "activate this flag by default". For boolean flags,
|
|
34
|
+
``default=True`` is returned as a literal value. :issue:`3111` :pr:`3239`
|
|
35
|
+
- Mark ``make_default_short_help`` as private API. :issue:`3189` :pr:`3250`
|
|
36
|
+
- ``CliRunner``'s redirected streams now expose the original file descriptor
|
|
37
|
+
via ``fileno()``, so that ``faulthandler``, ``subprocess``, and other
|
|
38
|
+
C-level consumers no longer crash with ``io.UnsupportedOperation``.
|
|
39
|
+
:issue:`2865`
|
|
40
|
+
- Change :class:`ParameterSource` to an :class:`~enum.IntEnum` and reorder
|
|
41
|
+
its members from most to least explicit, so values can be compared to
|
|
42
|
+
check whether a parameter was explicitly provided. :issue:`2879` :pr:`3248`
|
|
43
|
+
|
|
3
44
|
Version 8.3.2
|
|
4
|
-
|
|
45
|
+
-------------
|
|
5
46
|
|
|
6
47
|
Released 2026-04-02
|
|
7
48
|
|
|
@@ -19,7 +60,7 @@ Released 2026-04-02
|
|
|
19
60
|
``default=True``. :issue:`3121` :pr:`3201` :pr:`3213` :pr:`3225`
|
|
20
61
|
|
|
21
62
|
Version 8.3.1
|
|
22
|
-
|
|
63
|
+
-------------
|
|
23
64
|
|
|
24
65
|
Released 2025-11-15
|
|
25
66
|
|
|
@@ -1,28 +1,31 @@
|
|
|
1
|
-
|
|
1
|
+
(arguments)=
|
|
2
2
|
|
|
3
|
-
Arguments
|
|
4
|
-
=========
|
|
3
|
+
# Arguments
|
|
5
4
|
|
|
6
|
-
|
|
5
|
+
```{currentmodule} click
|
|
6
|
+
```
|
|
7
7
|
|
|
8
8
|
Arguments are:
|
|
9
9
|
|
|
10
|
-
*
|
|
11
|
-
*
|
|
12
|
-
|
|
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>`.
|
|
13
14
|
|
|
14
15
|
Useful and often used kwargs are:
|
|
15
16
|
|
|
16
|
-
*
|
|
17
|
-
*
|
|
17
|
+
* `default`: Passes a default.
|
|
18
|
+
* `nargs`: Sets the number of arguments. Set to -1 to take an arbitrary number.
|
|
18
19
|
|
|
19
|
-
Basic Arguments
|
|
20
|
-
---------------
|
|
20
|
+
## Basic Arguments
|
|
21
21
|
|
|
22
|
-
A minimal
|
|
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`.
|
|
23
25
|
|
|
24
26
|
Example:
|
|
25
27
|
|
|
28
|
+
```{eval-rst}
|
|
26
29
|
.. click:example::
|
|
27
30
|
|
|
28
31
|
@click.command()
|
|
@@ -36,19 +39,31 @@ And from the command line:
|
|
|
36
39
|
.. click:run::
|
|
37
40
|
|
|
38
41
|
invoke(touch, args=['foo.txt'])
|
|
42
|
+
```
|
|
39
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`.
|
|
40
47
|
|
|
41
|
-
|
|
48
|
+
```{admonition} Note on Required Arguments
|
|
49
|
+
:class: note
|
|
42
50
|
|
|
43
|
-
|
|
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
|
+
```
|
|
44
57
|
|
|
45
|
-
|
|
58
|
+
## Multiple Arguments
|
|
46
59
|
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
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.
|
|
51
65
|
|
|
66
|
+
```{eval-rst}
|
|
52
67
|
.. click:example::
|
|
53
68
|
|
|
54
69
|
@click.command()
|
|
@@ -64,18 +79,26 @@ And from the command line:
|
|
|
64
79
|
.. click:run::
|
|
65
80
|
|
|
66
81
|
invoke(copy, args=['foo.txt', 'usr/david/foo.txt', 'usr/mitsuko/foo.txt'])
|
|
82
|
+
```
|
|
67
83
|
|
|
68
|
-
|
|
84
|
+
```{admonition} Note on Handling Files
|
|
85
|
+
:class: note
|
|
69
86
|
|
|
70
|
-
|
|
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
|
+
```
|
|
71
91
|
|
|
72
|
-
Argument Escape Sequences
|
|
73
|
-
---------------------------
|
|
92
|
+
## Argument Escape Sequences
|
|
74
93
|
|
|
75
|
-
If you want to process arguments that look like options, like a file
|
|
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.
|
|
76
98
|
|
|
77
99
|
Example usage:
|
|
78
100
|
|
|
101
|
+
```{eval-rst}
|
|
79
102
|
.. click:example::
|
|
80
103
|
|
|
81
104
|
@click.command()
|
|
@@ -90,9 +113,12 @@ And from the command line:
|
|
|
90
113
|
.. click:run::
|
|
91
114
|
|
|
92
115
|
invoke(touch, ['--', '-foo.txt', 'bar.txt'])
|
|
116
|
+
```
|
|
93
117
|
|
|
94
|
-
If you don't like the
|
|
118
|
+
If you don't like the `--` marker, you can set
|
|
119
|
+
`ignore_unknown_options` to `True` to avoid checking unknown options:
|
|
95
120
|
|
|
121
|
+
```{eval-rst}
|
|
96
122
|
.. click:example::
|
|
97
123
|
|
|
98
124
|
@click.command(context_settings={"ignore_unknown_options": True})
|
|
@@ -107,17 +133,18 @@ And from the command line:
|
|
|
107
133
|
.. click:run::
|
|
108
134
|
|
|
109
135
|
invoke(touch, ['-foo.txt', 'bar.txt'])
|
|
136
|
+
```
|
|
110
137
|
|
|
138
|
+
(environment-variables)=
|
|
111
139
|
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
Environment Variables
|
|
115
|
-
---------------------
|
|
140
|
+
## Environment Variables
|
|
116
141
|
|
|
117
|
-
Arguments can use environment variables. To do so, pass the name(s) of
|
|
142
|
+
Arguments can use environment variables. To do so, pass the name(s) of
|
|
143
|
+
the environment variable(s) via `envvar` in `click.argument`.
|
|
118
144
|
|
|
119
145
|
Checking one environment variable:
|
|
120
146
|
|
|
147
|
+
```{eval-rst}
|
|
121
148
|
.. click:example::
|
|
122
149
|
|
|
123
150
|
@click.command()
|
|
@@ -156,3 +183,4 @@ And from the command line:
|
|
|
156
183
|
with open('hello.txt', 'w') as f:
|
|
157
184
|
f.write('Hello World from second variable!')
|
|
158
185
|
invoke(echo, env={'SRC_2': 'hello.txt'})
|
|
186
|
+
```
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
# Click Concepts
|
|
2
|
+
|
|
3
|
+
This section covers concepts about Click's design.
|
|
4
|
+
|
|
5
|
+
```{contents}
|
|
6
|
+
---
|
|
7
|
+
depth: 1
|
|
8
|
+
local: true
|
|
9
|
+
---
|
|
10
|
+
```
|
|
11
|
+
|
|
12
|
+
(callback-evaluation-order)=
|
|
13
|
+
|
|
14
|
+
## Callback Evaluation Order
|
|
15
|
+
|
|
16
|
+
Click works a bit differently than some other command line parsers in that
|
|
17
|
+
it attempts to reconcile the order of arguments as defined by the
|
|
18
|
+
programmer with the order of arguments as defined by the user before
|
|
19
|
+
invoking any callbacks.
|
|
20
|
+
|
|
21
|
+
This is an important concept to understand when porting complex
|
|
22
|
+
patterns to Click from optparse or other systems. A parameter
|
|
23
|
+
callback invocation in optparse happens as part of the parsing step,
|
|
24
|
+
whereas a callback invocation in Click happens after the parsing.
|
|
25
|
+
|
|
26
|
+
The main difference is that in optparse, callbacks are invoked with the raw
|
|
27
|
+
value as it happens, whereas a callback in Click is invoked after the
|
|
28
|
+
value has been fully converted.
|
|
29
|
+
|
|
30
|
+
Generally, the order of invocation is driven by the order in which the user
|
|
31
|
+
provides the arguments to the script; if there is an option called `--foo`
|
|
32
|
+
and an option called `--bar` and the user calls it as `--bar --foo`, then
|
|
33
|
+
the callback for `bar` will fire before the one for `foo`.
|
|
34
|
+
|
|
35
|
+
There are three exceptions to this rule which are important to know:
|
|
36
|
+
|
|
37
|
+
Eagerness:
|
|
38
|
+
> An option can be set to be "eager". All eager parameters are
|
|
39
|
+
> evaluated before all non-eager parameters, but again in the order as
|
|
40
|
+
> they were provided on the command line by the user.
|
|
41
|
+
|
|
42
|
+
> This is important for parameters that execute and exit like `--help`
|
|
43
|
+
> and `--version`. Both are eager parameters, but whatever parameter
|
|
44
|
+
> comes first on the command line will win and exit the program.
|
|
45
|
+
|
|
46
|
+
Repeated parameters:
|
|
47
|
+
> If an option or argument is split up on the command line into multiple
|
|
48
|
+
> places because it is repeated -- for instance, `--exclude foo --include
|
|
49
|
+
> baz --exclude bar` -- the callback will fire based on the position of
|
|
50
|
+
> the first option. In this case, the callback will fire for
|
|
51
|
+
> `exclude` and it will be passed both options (`foo` and
|
|
52
|
+
> `bar`), then the callback for `include` will fire with `baz`
|
|
53
|
+
> only.
|
|
54
|
+
|
|
55
|
+
> Note that even if a parameter does not allow multiple versions, Click
|
|
56
|
+
> will still accept the position of the first, but it will ignore every
|
|
57
|
+
> value except the last. The reason for this is to allow composability
|
|
58
|
+
> through shell aliases that set defaults.
|
|
59
|
+
|
|
60
|
+
Missing parameters:
|
|
61
|
+
> If a parameter is not defined on the command line, the callback will
|
|
62
|
+
> still fire. This is different from how it works in optparse where
|
|
63
|
+
> undefined values do not fire the callback. Missing parameters fire
|
|
64
|
+
> their callbacks at the very end which makes it possible for them to
|
|
65
|
+
> default to values from a parameter that came before.
|
|
66
|
+
|
|
67
|
+
Most of the time you do not need to be concerned about any of this,
|
|
68
|
+
but it is important to know how it works for some advanced cases.
|
|
@@ -1,22 +1,29 @@
|
|
|
1
|
-
Basic Commands, Groups, Context
|
|
2
|
-
================================
|
|
1
|
+
# Basic Commands, Groups, Context
|
|
3
2
|
|
|
4
|
-
|
|
3
|
+
```{currentmodule} click
|
|
4
|
+
```
|
|
5
5
|
|
|
6
|
-
Commands and Groups are the building blocks for Click applications.
|
|
6
|
+
Commands and Groups are the building blocks for Click applications.
|
|
7
|
+
{class}`Command` wraps a function to make it into a cli command. {class}`Group`
|
|
8
|
+
wraps Commands and Groups to make them into applications. {class}`Context` is
|
|
9
|
+
how groups and commands communicate.
|
|
7
10
|
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
+
```{contents}
|
|
12
|
+
---
|
|
13
|
+
depth: 2
|
|
14
|
+
local: true
|
|
15
|
+
---
|
|
16
|
+
```
|
|
11
17
|
|
|
12
|
-
Commands
|
|
13
|
-
|
|
18
|
+
## Commands
|
|
19
|
+
|
|
20
|
+
### Basic Command Example
|
|
14
21
|
|
|
15
|
-
Basic Command Example
|
|
16
|
-
^^^^^^^^^^^^^^^^^^^^^^^
|
|
17
22
|
A simple command decorator takes no arguments.
|
|
18
23
|
|
|
24
|
+
```{eval-rst}
|
|
19
25
|
.. click:example::
|
|
26
|
+
|
|
20
27
|
@click.command()
|
|
21
28
|
@click.option('--count', default=1)
|
|
22
29
|
def hello(count):
|
|
@@ -25,12 +32,16 @@ A simple command decorator takes no arguments.
|
|
|
25
32
|
|
|
26
33
|
.. click:run::
|
|
27
34
|
invoke(hello, args=['--count', '2',])
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
### Renaming Commands
|
|
28
38
|
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
By default the command is the function name with underscores replaced by dashes. To change this pass the desired name into the first positional argument.
|
|
39
|
+
By default the command is the function name with underscores replaced by dashes.
|
|
40
|
+
To change this pass the desired name into the first positional argument.
|
|
32
41
|
|
|
42
|
+
```{eval-rst}
|
|
33
43
|
.. click:example::
|
|
44
|
+
|
|
34
45
|
@click.command('say-hello')
|
|
35
46
|
@click.option('--count', default=1)
|
|
36
47
|
def hello(count):
|
|
@@ -39,12 +50,15 @@ By default the command is the function name with underscores replaced by dashes.
|
|
|
39
50
|
|
|
40
51
|
.. click:run::
|
|
41
52
|
invoke(hello, args=['--count', '2',])
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
### Deprecating Commands
|
|
42
56
|
|
|
43
|
-
|
|
44
|
-
^^^^^^^^^^^^^^^^^^^^^^
|
|
45
|
-
To mark a command as deprecated pass in ``deprecated=True``
|
|
57
|
+
To mark a command as deprecated pass in `deprecated=True`
|
|
46
58
|
|
|
59
|
+
```{eval-rst}
|
|
47
60
|
.. click:example::
|
|
61
|
+
|
|
48
62
|
@click.command('say-hello', deprecated=True)
|
|
49
63
|
@click.option('--count', default=1)
|
|
50
64
|
def hello(count):
|
|
@@ -53,15 +67,19 @@ To mark a command as deprecated pass in ``deprecated=True``
|
|
|
53
67
|
|
|
54
68
|
.. click:run::
|
|
55
69
|
invoke(hello, args=['--count', '2',])
|
|
70
|
+
```
|
|
56
71
|
|
|
57
|
-
Groups
|
|
58
|
-
------------
|
|
72
|
+
## Groups
|
|
59
73
|
|
|
60
|
-
Basic Group Example
|
|
61
|
-
^^^^^^^^^^^^^^^^^^^^^
|
|
62
|
-
A group wraps one or more commands. After being wrapped, the commands are nested under that group. You can see that on the help pages and in the execution. By default, invoking the group with no command shows the help page.
|
|
74
|
+
### Basic Group Example
|
|
63
75
|
|
|
76
|
+
A group wraps one or more commands. After being wrapped, the commands are nested
|
|
77
|
+
under that group. You can see that on the help pages and in the execution. By
|
|
78
|
+
default, invoking the group with no command shows the help page.
|
|
79
|
+
|
|
80
|
+
```{eval-rst}
|
|
64
81
|
.. click:example::
|
|
82
|
+
|
|
65
83
|
@click.group()
|
|
66
84
|
def greeting():
|
|
67
85
|
click.echo('Starting greeting ...')
|
|
@@ -84,14 +102,19 @@ At the command level:
|
|
|
84
102
|
|
|
85
103
|
invoke(greeting, args=['say-hello'])
|
|
86
104
|
invoke(greeting, args=['say-hello', '--help'])
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
As you can see from the above example, the function wrapped by the group
|
|
108
|
+
decorator executes unless it is interrupted (for example by calling the help).
|
|
87
109
|
|
|
88
|
-
|
|
110
|
+
### Renaming Groups
|
|
89
111
|
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
To have a name other than the decorated function name as the group name, pass it in as the first positional argument.
|
|
112
|
+
To have a name other than the decorated function name as the group name, pass it
|
|
113
|
+
in as the first positional argument.
|
|
93
114
|
|
|
115
|
+
```{eval-rst}
|
|
94
116
|
.. click:example::
|
|
117
|
+
|
|
95
118
|
@click.group('greet-someone')
|
|
96
119
|
def greeting():
|
|
97
120
|
click.echo('Starting greeting ...')
|
|
@@ -105,12 +128,17 @@ To have a name other than the decorated function name as the group name, pass it
|
|
|
105
128
|
.. click:run::
|
|
106
129
|
|
|
107
130
|
invoke(greeting, args=['say-hello'])
|
|
131
|
+
```
|
|
108
132
|
|
|
109
|
-
Group Invocation Without Command
|
|
110
|
-
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
|
133
|
+
### Group Invocation Without Command
|
|
111
134
|
|
|
112
|
-
By default, if a group is passed without a command, the group is not invoked and
|
|
135
|
+
By default, if a group is passed without a command, the group is not invoked and
|
|
136
|
+
a command automatically passes `--help`. To change this, pass
|
|
137
|
+
`invoke_without_command=True` to the group. The context object also includes
|
|
138
|
+
information about whether or not the group invocation would go to a command
|
|
139
|
+
nested under it.
|
|
113
140
|
|
|
141
|
+
```{eval-rst}
|
|
114
142
|
.. click:example::
|
|
115
143
|
|
|
116
144
|
@click.group(invoke_without_command=True)
|
|
@@ -129,14 +157,15 @@ By default, if a group is passed without a command, the group is not invoked and
|
|
|
129
157
|
|
|
130
158
|
invoke(cli, prog_name='tool', args=[])
|
|
131
159
|
invoke(cli, prog_name='tool', args=['sync'])
|
|
160
|
+
```
|
|
132
161
|
|
|
162
|
+
### Group Separation
|
|
133
163
|
|
|
164
|
+
Command {ref}`parameters` attached to a command belong only to that command.
|
|
134
165
|
|
|
135
|
-
|
|
136
|
-
^^^^^^^^^^^^^^^^^^^
|
|
137
|
-
Command :ref:`parameters` attached to a command belong only to that command.
|
|
138
|
-
|
|
166
|
+
```{eval-rst}
|
|
139
167
|
.. click:example::
|
|
168
|
+
|
|
140
169
|
@click.group()
|
|
141
170
|
def greeting():
|
|
142
171
|
pass
|
|
@@ -158,19 +187,27 @@ Command :ref:`parameters` attached to a command belong only to that command.
|
|
|
158
187
|
invoke(greeting, args=['hello', '--count', '2'])
|
|
159
188
|
invoke(greeting, args=['goodbye', '--count', '2'])
|
|
160
189
|
invoke(greeting)
|
|
190
|
+
```
|
|
161
191
|
|
|
162
|
-
Additionally parameters for a given group belong only to that group and not to
|
|
192
|
+
Additionally parameters for a given group belong only to that group and not to
|
|
193
|
+
the commands under it. What this means is that options and arguments for a
|
|
194
|
+
specific command have to be specified *after* the command name itself, but
|
|
195
|
+
*before* any other command names.
|
|
163
196
|
|
|
164
|
-
This behavior is observable with the
|
|
197
|
+
This behavior is observable with the `--help` option. Suppose we have a group
|
|
198
|
+
called `tool` containing a command called `sub`.
|
|
165
199
|
|
|
166
|
-
-
|
|
167
|
-
-
|
|
168
|
-
- But
|
|
200
|
+
- `tool --help` returns the help for the whole program (listing subcommands).
|
|
201
|
+
- `tool sub --help` returns the help for the `sub` subcommand.
|
|
202
|
+
- But `tool --help sub` treats `--help` as an argument for the main program.
|
|
203
|
+
Click then invokes the callback for `--help`, which prints the help and aborts
|
|
204
|
+
the program before click can process the subcommand.
|
|
169
205
|
|
|
170
|
-
Arbitrary Nesting
|
|
171
|
-
^^^^^^^^^^^^^^^^^^^
|
|
172
|
-
:class:`Commands <Command>` are attached to a :class:`Group`. Multiple groups can be attached to another group. Groups containing multiple groups can be attached to a group, and so on. To invoke a command nested under multiple groups, all the groups under which it is nested must be invoked.
|
|
206
|
+
### Arbitrary Nesting
|
|
173
207
|
|
|
208
|
+
{class}`Commands <Command>` are attached to a {class}`Group`. Multiple groups can be attached to another group. Groups containing multiple groups can be attached to a group, and so on. To invoke a command nested under multiple groups, all the groups under which it is nested must be invoked.
|
|
209
|
+
|
|
210
|
+
```{eval-rst}
|
|
174
211
|
.. click:example::
|
|
175
212
|
|
|
176
213
|
@click.group()
|
|
@@ -193,11 +230,16 @@ Arbitrary Nesting
|
|
|
193
230
|
.. click:run::
|
|
194
231
|
|
|
195
232
|
invoke(cli, args=['session', 'initdb'])
|
|
233
|
+
```
|
|
234
|
+
|
|
235
|
+
### Lazily Attaching Commands
|
|
196
236
|
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
237
|
+
Most examples so far have attached the commands to a group immediately, but
|
|
238
|
+
commands may be registered later. This could be used to split commands into
|
|
239
|
+
multiple Python modules. Regardless of how they are attached, the commands are
|
|
240
|
+
invoked identically.
|
|
200
241
|
|
|
242
|
+
```{eval-rst}
|
|
201
243
|
.. click:example::
|
|
202
244
|
|
|
203
245
|
@click.group()
|
|
@@ -218,18 +260,23 @@ Most examples so far have attached the commands to a group immediately, but comm
|
|
|
218
260
|
|
|
219
261
|
invoke(cli, args=['initdb'])
|
|
220
262
|
invoke(cli, args=['dropdb'])
|
|
263
|
+
```
|
|
264
|
+
|
|
265
|
+
## Context Object
|
|
266
|
+
The {class}`Context` object is how commands and groups communicate.
|
|
221
267
|
|
|
222
|
-
|
|
223
|
-
-------------------
|
|
224
|
-
The :class:`Context` object is how commands and groups communicate.
|
|
268
|
+
### Auto Envvar Prefix
|
|
225
269
|
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
270
|
+
Automatically built environment variables are supported for options only. To
|
|
271
|
+
enable this feature, the `auto_envvar_prefix` parameter needs to be passed to
|
|
272
|
+
the script that is invoked. Each command and parameter is then added as an
|
|
273
|
+
uppercase underscore-separated variable. If you have a subcommand
|
|
274
|
+
called `run` taking an option called `reload` and the prefix is `WEB`, then the
|
|
275
|
+
variable is `WEB_RUN_RELOAD`.
|
|
230
276
|
|
|
231
277
|
Example usage:
|
|
232
278
|
|
|
279
|
+
```{eval-rst}
|
|
233
280
|
.. click:example::
|
|
234
281
|
|
|
235
282
|
@click.command()
|
|
@@ -246,13 +293,15 @@ And from the command line:
|
|
|
246
293
|
|
|
247
294
|
invoke(greet, env={'GREETER_USERNAME': 'john'},
|
|
248
295
|
auto_envvar_prefix='GREETER')
|
|
296
|
+
```
|
|
249
297
|
|
|
250
|
-
When using
|
|
298
|
+
When using `auto_envvar_prefix` with command groups, the command name
|
|
251
299
|
needs to be included in the environment variable, between the prefix and
|
|
252
|
-
the parameter name, *i.e.*
|
|
253
|
-
subcommand called
|
|
254
|
-
the prefix is
|
|
300
|
+
the parameter name, *i.e.* `PREFIX_COMMAND_VARIABLE`. If you have a
|
|
301
|
+
subcommand called `run-server` taking an option called `host` and
|
|
302
|
+
the prefix is `WEB`, then the variable is `WEB_RUN_SERVER_HOST`.
|
|
255
303
|
|
|
304
|
+
```{eval-rst}
|
|
256
305
|
.. click:example::
|
|
257
306
|
|
|
258
307
|
@click.group()
|
|
@@ -273,31 +322,35 @@ the prefix is ``WEB``, then the variable is ``WEB_RUN_SERVER_HOST``.
|
|
|
273
322
|
invoke(cli, args=['greet',],
|
|
274
323
|
env={'GREETER_GREET_USERNAME': 'John', 'GREETER_DEBUG': 'false'},
|
|
275
324
|
auto_envvar_prefix='GREETER')
|
|
325
|
+
```
|
|
276
326
|
|
|
277
|
-
Global Context Access
|
|
278
|
-
^^^^^^^^^^^^^^^^^^^^^^
|
|
327
|
+
### Global Context Access
|
|
279
328
|
|
|
280
|
-
|
|
329
|
+
```{versionadded} 5.0
|
|
330
|
+
```
|
|
281
331
|
|
|
282
332
|
Starting with Click 5.0 it is possible to access the current context from
|
|
283
333
|
anywhere within the same thread through the use of the
|
|
284
|
-
|
|
334
|
+
{func}`get_current_context` function which returns it. This is primarily
|
|
285
335
|
useful for accessing the context bound object as well as some flags that
|
|
286
336
|
are stored on it to customize the runtime behavior. For instance the
|
|
287
|
-
|
|
337
|
+
{func}`echo` function does this to infer the default value of the `color`
|
|
288
338
|
flag.
|
|
289
339
|
|
|
290
|
-
Example usage
|
|
340
|
+
Example usage:
|
|
291
341
|
|
|
342
|
+
```python
|
|
292
343
|
def get_current_command_name():
|
|
293
344
|
return click.get_current_context().info_name
|
|
345
|
+
```
|
|
294
346
|
|
|
295
347
|
It should be noted that this only works within the current thread. If you
|
|
296
348
|
spawn additional threads then those threads will not have the ability to
|
|
297
349
|
refer to the current context. If you want to give another thread the
|
|
298
350
|
ability to refer to this context you need to use the context within the
|
|
299
|
-
thread as a context manager
|
|
351
|
+
thread as a context manager:
|
|
300
352
|
|
|
353
|
+
```python
|
|
301
354
|
def spawn_thread(ctx, func):
|
|
302
355
|
def wrapper():
|
|
303
356
|
with ctx:
|
|
@@ -305,6 +358,7 @@ thread as a context manager::
|
|
|
305
358
|
t = threading.Thread(target=wrapper)
|
|
306
359
|
t.start()
|
|
307
360
|
return t
|
|
361
|
+
```
|
|
308
362
|
|
|
309
363
|
Now the thread function can access the context like the main thread would
|
|
310
364
|
do. However if you do use this for threading you need to be very careful
|
|
@@ -313,16 +367,45 @@ allowed to read from the context, but not to perform any modifications on
|
|
|
313
367
|
it.
|
|
314
368
|
|
|
315
369
|
|
|
316
|
-
|
|
317
|
-
|
|
370
|
+
### Parameter Source Priority
|
|
371
|
+
|
|
372
|
+
When a parameter's value is resolved, Click checks several sources
|
|
373
|
+
in the following order of precedence:
|
|
374
|
+
|
|
375
|
+
1. **Prompt** (`PROMPT`): interactively provided by the user at a prompt.
|
|
376
|
+
2. **Command line** (`COMMANDLINE`): provided as a CLI argument or option.
|
|
377
|
+
3. **Environment variable** (`ENVIRONMENT`): read from an environment variable.
|
|
378
|
+
4. **Default map** (`DEFAULT_MAP`): looked up from {attr}`Context.default_map`.
|
|
379
|
+
5. **Default** (`DEFAULT`): the default value defined on the parameter.
|
|
380
|
+
|
|
381
|
+
{class}`~click.core.ParameterSource` members are ordered from most
|
|
382
|
+
explicit to least explicit. Because it is an {class}`~enum.IntEnum`,
|
|
383
|
+
you can use comparison operators to check how explicitly a value was
|
|
384
|
+
provided:
|
|
385
|
+
|
|
386
|
+
```python
|
|
387
|
+
source = ctx.get_parameter_source("port")
|
|
388
|
+
|
|
389
|
+
# True if the value was explicitly set (command line, prompt, or env var).
|
|
390
|
+
if source < click.ParameterSource.DEFAULT_MAP:
|
|
391
|
+
...
|
|
392
|
+
|
|
393
|
+
# True if the value came from any kind of default.
|
|
394
|
+
if source >= click.ParameterSource.DEFAULT_MAP:
|
|
395
|
+
...
|
|
396
|
+
```
|
|
397
|
+
|
|
398
|
+
|
|
399
|
+
### Detecting the Source of a Parameter
|
|
318
400
|
|
|
319
401
|
In some situations it's helpful to understand whether or not an option
|
|
320
402
|
or parameter came from the command line, the environment, the default
|
|
321
|
-
value, or
|
|
322
|
-
|
|
323
|
-
out. It will return a member of the
|
|
403
|
+
value, or {attr}`Context.default_map`. The
|
|
404
|
+
{meth}`Context.get_parameter_source` method can be used to find this
|
|
405
|
+
out. It will return a member of the {class}`~click.core.ParameterSource`
|
|
324
406
|
enum.
|
|
325
407
|
|
|
408
|
+
```{eval-rst}
|
|
326
409
|
.. click:example::
|
|
327
410
|
|
|
328
411
|
@click.command()
|
|
@@ -340,3 +423,4 @@ enum.
|
|
|
340
423
|
println()
|
|
341
424
|
invoke(cli, prog_name='cli', args=[])
|
|
342
425
|
println()
|
|
426
|
+
```
|