Skip to content

Commit 8c2afd7

Browse files
committed
refactor from rest to myst
1 parent 467f930 commit 8c2afd7

1 file changed

Lines changed: 91 additions & 61 deletions

File tree

docs/commands-and-groups.md

Lines changed: 91 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,26 @@
1-
Basic Commands, Groups, Context
2-
================================
1+
# Basic Commands, Groups, Context
32

4-
.. currentmodule:: click
3+
```{currentmodule} click
4+
```
55

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.
77

8-
.. contents::
9-
:depth: 2
10-
:local:
8+
```{contents}
9+
---
10+
depth: 2
11+
local: true
12+
---
13+
```
1114

12-
Commands
13-
--------------------
15+
## Commands
16+
17+
### Basic Command Example
1418

15-
Basic Command Example
16-
^^^^^^^^^^^^^^^^^^^^^^^
1719
A simple command decorator takes no arguments.
1820

21+
```{eval-rst}
1922
.. click:example::
23+
2024
@click.command()
2125
@click.option('--count', default=1)
2226
def hello(count):
@@ -25,12 +29,15 @@ A simple command decorator takes no arguments.
2529
2630
.. click:run::
2731
invoke(hello, args=['--count', '2',])
32+
```
33+
34+
### Renaming Commands
2835

29-
Renaming Commands
30-
^^^^^^^^^^^^^^^^^^^
3136
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.
3237

38+
```{eval-rst}
3339
.. click:example::
40+
3441
@click.command('say-hello')
3542
@click.option('--count', default=1)
3643
def hello(count):
@@ -39,12 +46,15 @@ By default the command is the function name with underscores replaced by dashes.
3946
4047
.. click:run::
4148
invoke(hello, args=['--count', '2',])
49+
```
4250

43-
Deprecating Commands
44-
^^^^^^^^^^^^^^^^^^^^^^
45-
To mark a command as deprecated pass in ``deprecated=True``
51+
### Deprecating Commands
4652

53+
To mark a command as deprecated pass in `deprecated=True`
54+
55+
```{eval-rst}
4756
.. click:example::
57+
4858
@click.command('say-hello', deprecated=True)
4959
@click.option('--count', default=1)
5060
def hello(count):
@@ -53,15 +63,17 @@ To mark a command as deprecated pass in ``deprecated=True``
5363
5464
.. click:run::
5565
invoke(hello, args=['--count', '2',])
66+
```
5667

57-
Groups
58-
------------
68+
## Groups
69+
70+
### Basic Group Example
5971

60-
Basic Group Example
61-
^^^^^^^^^^^^^^^^^^^^^
6272
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.
6373

74+
```{eval-rst}
6475
.. click:example::
76+
6577
@click.group()
6678
def greeting():
6779
click.echo('Starting greeting ...')
@@ -84,14 +96,17 @@ At the command level:
8496
8597
invoke(greeting, args=['say-hello'])
8698
invoke(greeting, args=['say-hello', '--help'])
99+
```
87100

88101
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).
89102

90-
Renaming Groups
91-
^^^^^^^^^^^^^^^^^
103+
### Renaming Groups
104+
92105
To have a name other than the decorated function name as the group name, pass it in as the first positional argument.
93106

107+
```{eval-rst}
94108
.. click:example::
109+
95110
@click.group('greet-someone')
96111
def greeting():
97112
click.echo('Starting greeting ...')
@@ -105,12 +120,13 @@ To have a name other than the decorated function name as the group name, pass it
105120
.. click:run::
106121
107122
invoke(greeting, args=['say-hello'])
123+
```
108124

109-
Group Invocation Without Command
110-
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
125+
### Group Invocation Without Command
111126

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.
113128

129+
```{eval-rst}
114130
.. click:example::
115131
116132
@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
129145
130146
invoke(cli, prog_name='tool', args=[])
131147
invoke(cli, prog_name='tool', args=['sync'])
148+
```
132149

150+
### Group Separation
133151

152+
Command {ref}`parameters` attached to a command belong only to that command.
134153

135-
Group Separation
136-
^^^^^^^^^^^^^^^^^^^
137-
Command :ref:`parameters` attached to a command belong only to that command.
138-
154+
```{eval-rst}
139155
.. click:example::
156+
140157
@click.group()
141158
def greeting():
142159
pass
@@ -158,19 +175,21 @@ Command :ref:`parameters` attached to a command belong only to that command.
158175
invoke(greeting, args=['hello', '--count', '2'])
159176
invoke(greeting, args=['goodbye', '--count', '2'])
160177
invoke(greeting)
178+
```
161179

162180
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.
163181

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`.
165183

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.
169187

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
173189

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}
174193
.. click:example::
175194
176195
@click.group()
@@ -193,11 +212,13 @@ Arbitrary Nesting
193212
.. click:run::
194213
195214
invoke(cli, args=['session', 'initdb'])
215+
```
216+
217+
### Lazily Attaching Commands
196218

197-
Lazily Attaching Commands
198-
^^^^^^^^^^^^^^^^^^^^^^^^^^^
199219
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.
200220

221+
```{eval-rst}
201222
.. click:example::
202223
203224
@click.group()
@@ -218,18 +239,19 @@ Most examples so far have attached the commands to a group immediately, but comm
218239
219240
invoke(cli, args=['initdb'])
220241
invoke(cli, args=['dropdb'])
242+
```
221243

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.
225246

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`.
230251

231252
Example usage:
232253

254+
```{eval-rst}
233255
.. click:example::
234256
235257
@click.command()
@@ -246,13 +268,15 @@ And from the command line:
246268
247269
invoke(greet, env={'GREETER_USERNAME': 'john'},
248270
auto_envvar_prefix='GREETER')
271+
```
249272

250-
When using ``auto_envvar_prefix`` with command groups, the command name
273+
When using `auto_envvar_prefix` with command groups, the command name
251274
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`.
255278

279+
```{eval-rst}
256280
.. click:example::
257281
258282
@click.group()
@@ -273,38 +297,43 @@ the prefix is ``WEB``, then the variable is ``WEB_RUN_SERVER_HOST``.
273297
invoke(cli, args=['greet',],
274298
env={'GREETER_GREET_USERNAME': 'John', 'GREETER_DEBUG': 'false'},
275299
auto_envvar_prefix='GREETER')
300+
```
276301

277-
Global Context Access
278-
^^^^^^^^^^^^^^^^^^^^^^
302+
### Global Context Access
279303

280-
.. versionadded:: 5.0
304+
```{versionadded} 5.0
305+
```
281306

282307
Starting with Click 5.0 it is possible to access the current context from
283308
anywhere within the same thread through the use of the
284-
:func:`get_current_context` function which returns it. This is primarily
309+
{func}`get_current_context` function which returns it. This is primarily
285310
useful for accessing the context bound object as well as some flags that
286311
are stored on it to customize the runtime behavior. For instance the
287-
:func:`echo` function does this to infer the default value of the `color`
312+
{func}`echo` function does this to infer the default value of the `color`
288313
flag.
289314

290-
Example usage::
315+
Example usage:
291316

317+
```python
292318
def get_current_command_name():
293319
return click.get_current_context().info_name
320+
```
294321

295322
It should be noted that this only works within the current thread. If you
296323
spawn additional threads then those threads will not have the ability to
297324
refer to the current context. If you want to give another thread the
298325
ability to refer to this context you need to use the context within the
299-
thread as a context manager::
326+
thread as a context manager:
300327

328+
```python
301329
def spawn_thread(ctx, func):
302330
def wrapper():
303331
with ctx:
304332
func()
305333
t = threading.Thread(target=wrapper)
306334
t.start()
307335
return t
336+
```
308337

309338
Now the thread function can access the context like the main thread would
310339
do. However if you do use this for threading you need to be very careful
@@ -313,16 +342,16 @@ allowed to read from the context, but not to perform any modifications on
313342
it.
314343

315344

316-
Detecting the Source of a Parameter
317-
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
345+
### Detecting the Source of a Parameter
318346

319347
In some situations it's helpful to understand whether or not an option
320348
or parameter came from the command line, the environment, the default
321-
value, or :attr:`Context.default_map`. The
322-
:meth:`Context.get_parameter_source` method can be used to find this
323-
out. It will return a member of the :class:`~click.core.ParameterSource`
349+
value, or {attr}`Context.default_map`. The
350+
{meth}`Context.get_parameter_source` method can be used to find this
351+
out. It will return a member of the {class}`~click.core.ParameterSource`
324352
enum.
325353

354+
```{eval-rst}
326355
.. click:example::
327356
328357
@click.command()
@@ -340,3 +369,4 @@ enum.
340369
println()
341370
invoke(cli, prog_name='cli', args=[])
342371
println()
372+
```

0 commit comments

Comments
 (0)