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
Copy file name to clipboardExpand all lines: docs/commands-and-groups.md
+91-61Lines changed: 91 additions & 61 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,22 +1,26 @@
1
-
Basic Commands, Groups, Context
2
-
================================
1
+
# Basic Commands, Groups, Context
3
2
4
-
.. currentmodule:: click
3
+
```{currentmodule} click
4
+
```
5
5
6
-
Commands and Groups are the building blocks for Click applications. :class:`Command` wraps a function to make it into a cli command. :class:`Group` wraps Commands and Groups to make them into applications. :class:`Context` is how groups and commands communicate.
6
+
Commands and Groups are the building blocks for Click applications. {class}`Command` wraps a function to make it into a cli command. {class}`Group` wraps Commands and Groups to make them into applications. {class}`Context` is how groups and commands communicate.
7
7
8
-
.. contents::
9
-
:depth: 2
10
-
:local:
8
+
```{contents}
9
+
---
10
+
depth: 2
11
+
local: true
12
+
---
13
+
```
11
14
12
-
Commands
13
-
--------------------
15
+
## Commands
16
+
17
+
### Basic Command Example
14
18
15
-
Basic Command Example
16
-
^^^^^^^^^^^^^^^^^^^^^^^
17
19
A simple command decorator takes no arguments.
18
20
21
+
```{eval-rst}
19
22
.. click:example::
23
+
20
24
@click.command()
21
25
@click.option('--count', default=1)
22
26
def hello(count):
@@ -25,12 +29,15 @@ A simple command decorator takes no arguments.
25
29
26
30
.. click:run::
27
31
invoke(hello, args=['--count', '2',])
32
+
```
33
+
34
+
### Renaming Commands
28
35
29
-
Renaming Commands
30
-
^^^^^^^^^^^^^^^^^^^
31
36
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.
32
37
38
+
```{eval-rst}
33
39
.. click:example::
40
+
34
41
@click.command('say-hello')
35
42
@click.option('--count', default=1)
36
43
def hello(count):
@@ -39,12 +46,15 @@ By default the command is the function name with underscores replaced by dashes.
39
46
40
47
.. click:run::
41
48
invoke(hello, args=['--count', '2',])
49
+
```
42
50
43
-
Deprecating Commands
44
-
^^^^^^^^^^^^^^^^^^^^^^
45
-
To mark a command as deprecated pass in ``deprecated=True``
51
+
### Deprecating Commands
46
52
53
+
To mark a command as deprecated pass in `deprecated=True`
54
+
55
+
```{eval-rst}
47
56
.. click:example::
57
+
48
58
@click.command('say-hello', deprecated=True)
49
59
@click.option('--count', default=1)
50
60
def hello(count):
@@ -53,15 +63,17 @@ To mark a command as deprecated pass in ``deprecated=True``
53
63
54
64
.. click:run::
55
65
invoke(hello, args=['--count', '2',])
66
+
```
56
67
57
-
Groups
58
-
------------
68
+
## Groups
69
+
70
+
### Basic Group Example
59
71
60
-
Basic Group Example
61
-
^^^^^^^^^^^^^^^^^^^^^
62
72
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.
63
73
74
+
```{eval-rst}
64
75
.. click:example::
76
+
65
77
@click.group()
66
78
def greeting():
67
79
click.echo('Starting greeting ...')
@@ -84,14 +96,17 @@ At the command level:
84
96
85
97
invoke(greeting, args=['say-hello'])
86
98
invoke(greeting, args=['say-hello', '--help'])
99
+
```
87
100
88
101
As you can see from the above example, the function wrapped by the group decorator executes unless it is interrupted (for example by calling the help).
89
102
90
-
Renaming Groups
91
-
^^^^^^^^^^^^^^^^^
103
+
### Renaming Groups
104
+
92
105
To have a name other than the decorated function name as the group name, pass it in as the first positional argument.
93
106
107
+
```{eval-rst}
94
108
.. click:example::
109
+
95
110
@click.group('greet-someone')
96
111
def greeting():
97
112
click.echo('Starting greeting ...')
@@ -105,12 +120,13 @@ To have a name other than the decorated function name as the group name, pass it
105
120
.. click:run::
106
121
107
122
invoke(greeting, args=['say-hello'])
123
+
```
108
124
109
-
Group Invocation Without Command
110
-
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
125
+
### Group Invocation Without Command
111
126
112
-
By default, if a group is passed without a command, the group is not invoked and a command automatically passes ``--help``. To change this, pass ``invoke_without_command=True`` to the group. The context object also includes information about whether or not the group invocation would go to a command nested under it.
127
+
By default, if a group is passed without a command, the group is not invoked and a command automatically passes `--help`. To change this, pass `invoke_without_command=True` to the group. The context object also includes information about whether or not the group invocation would go to a command nested under it.
113
128
129
+
```{eval-rst}
114
130
.. click:example::
115
131
116
132
@click.group(invoke_without_command=True)
@@ -129,14 +145,15 @@ By default, if a group is passed without a command, the group is not invoked and
129
145
130
146
invoke(cli, prog_name='tool', args=[])
131
147
invoke(cli, prog_name='tool', args=['sync'])
148
+
```
132
149
150
+
### Group Separation
133
151
152
+
Command {ref}`parameters` attached to a command belong only to that command.
134
153
135
-
Group Separation
136
-
^^^^^^^^^^^^^^^^^^^
137
-
Command :ref:`parameters` attached to a command belong only to that command.
138
-
154
+
```{eval-rst}
139
155
.. click:example::
156
+
140
157
@click.group()
141
158
def greeting():
142
159
pass
@@ -158,19 +175,21 @@ Command :ref:`parameters` attached to a command belong only to that command.
Additionally parameters for a given group belong only to that group and not to the commands under it. What this means is that options and arguments for a specific command have to be specified *after* the command name itself, but *before* any other command names.
163
181
164
-
This behavior is observable with the ``--help`` option. Suppose we have a group called ``tool`` containing a command called ``sub``.
182
+
This behavior is observable with the `--help` option. Suppose we have a group called `tool` containing a command called `sub`.
165
183
166
-
-``tool --help`` returns the help for the whole program (listing subcommands).
167
-
-``tool sub --help`` returns the help for the ``sub`` subcommand.
168
-
- But ``tool --help sub`` treats ``--help`` as an argument for the main program. Click then invokes the callback for ``--help``, which prints the help and aborts the program before click can process the subcommand.
184
+
-`tool --help` returns the help for the whole program (listing subcommands).
185
+
-`tool sub --help` returns the help for the `sub` subcommand.
186
+
- But `tool --help sub` treats `--help` as an argument for the main program. Click then invokes the callback for `--help`, which prints the help and aborts the program before click can process the subcommand.
169
187
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.
188
+
### Arbitrary Nesting
173
189
190
+
{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.
191
+
192
+
```{eval-rst}
174
193
.. click:example::
175
194
176
195
@click.group()
@@ -193,11 +212,13 @@ Arbitrary Nesting
193
212
.. click:run::
194
213
195
214
invoke(cli, args=['session', 'initdb'])
215
+
```
216
+
217
+
### Lazily Attaching Commands
196
218
197
-
Lazily Attaching Commands
198
-
^^^^^^^^^^^^^^^^^^^^^^^^^^^
199
219
Most examples so far have attached the commands to a group immediately, but commands may be registered later. This could be used to split commands into multiple Python modules. Regardless of how they are attached, the commands are invoked identically.
200
220
221
+
```{eval-rst}
201
222
.. click:example::
202
223
203
224
@click.group()
@@ -218,18 +239,19 @@ Most examples so far have attached the commands to a group immediately, but comm
218
239
219
240
invoke(cli, args=['initdb'])
220
241
invoke(cli, args=['dropdb'])
242
+
```
221
243
222
-
Context Object
223
-
-------------------
224
-
The :class:`Context` object is how commands and groups communicate.
244
+
## Context Object
245
+
The {class}`Context` object is how commands and groups communicate.
225
246
226
-
Auto Envvar Prefix
227
-
^^^^^^^^^^^^^^^^^^^^
228
-
Automatically built environment variables are supported for options only. To enable this feature, the ``auto_envvar_prefix`` parameter needs to be passed to the script that is invoked. Each command and parameter is then added as an uppercase underscore-separated variable. If you have a subcommand
229
-
called ``run`` taking an option called ``reload`` and the prefix is ``WEB``, then the variable is ``WEB_RUN_RELOAD``.
247
+
### Auto Envvar Prefix
248
+
249
+
Automatically built environment variables are supported for options only. To enable this feature, the `auto_envvar_prefix` parameter needs to be passed to the script that is invoked. Each command and parameter is then added as an uppercase underscore-separated variable. If you have a subcommand
250
+
called `run` taking an option called `reload` and the prefix is `WEB`, then the variable is `WEB_RUN_RELOAD`.
230
251
231
252
Example usage:
232
253
254
+
```{eval-rst}
233
255
.. click:example::
234
256
235
257
@click.command()
@@ -246,13 +268,15 @@ And from the command line:
246
268
247
269
invoke(greet, env={'GREETER_USERNAME': 'john'},
248
270
auto_envvar_prefix='GREETER')
271
+
```
249
272
250
-
When using ``auto_envvar_prefix`` with command groups, the command name
273
+
When using `auto_envvar_prefix` with command groups, the command name
251
274
needs to be included in the environment variable, between the prefix and
252
-
the parameter name, *i.e.*``PREFIX_COMMAND_VARIABLE``. If you have a
253
-
subcommand called ``run-server`` taking an option called ``host`` and
254
-
the prefix is ``WEB``, then the variable is ``WEB_RUN_SERVER_HOST``.
275
+
the parameter name, *i.e.*`PREFIX_COMMAND_VARIABLE`. If you have a
276
+
subcommand called `run-server` taking an option called `host` and
277
+
the prefix is `WEB`, then the variable is `WEB_RUN_SERVER_HOST`.
255
278
279
+
```{eval-rst}
256
280
.. click:example::
257
281
258
282
@click.group()
@@ -273,38 +297,43 @@ the prefix is ``WEB``, then the variable is ``WEB_RUN_SERVER_HOST``.
0 commit comments