refactor from rest to myst
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Jaron Rosenau · committed by Jaron Rosenau · parent a29ec1a · Browse files · 1 file·+91 added−61 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
1 file+91 added−61 removed
- other:+91 added−61 removed
Provenance
- Commit
a1a5e8e- Tree
5949e63- Author
Jaron Rosenau <jaron.rosenau@gmail.com>- Committer
Jaron Rosenau <jaron.rosenau@gmail.com>- Parent
a29ec1a
1 file changed+91 added−61 removed
modified:docs/commands-and-groups.md+91 added-61 removed
1-Basic Commands, Groups, Context2-================================1+# Basic Commands, Groups, Context2 4-.. currentmodule:: click3+```{currentmodule} click4+```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 8-.. contents::9- :depth: 210- :local:8+```{contents}9+---10+depth: 211+local: true12+---13+```14 12-Commands13---------------------15+## Commands16 15-Basic Command Example16-^^^^^^^^^^^^^^^^^^^^^^^17+### Basic Command Example18+19 A simple command decorator takes no arguments.20 21+```{eval-rst}22 .. click:example::23+24 @click.command()25 @click.option('--count', default=1)26 def hello(count):27 for x in range(count):28 click.echo("Hello!")29 30 .. click:run::31 invoke(hello, args=['--count', '2',])32+```33 29-Renaming Commands30-^^^^^^^^^^^^^^^^^^^34+### Renaming Commands35+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.37 38+```{eval-rst}39 .. click:example::40+41 @click.command('say-hello')42 @click.option('--count', default=1)43 def hello(count):44 for x in range(count):45 click.echo("Hello!")46 47 .. click:run::48 invoke(hello, args=['--count', '2',])49+```50 43-Deprecating Commands44-^^^^^^^^^^^^^^^^^^^^^^45-To mark a command as deprecated pass in ``deprecated=True``51+### Deprecating Commands52 53+To mark a command as deprecated pass in `deprecated=True`54+55+```{eval-rst}56 .. click:example::57+58 @click.command('say-hello', deprecated=True)59 @click.option('--count', default=1)60 def hello(count):61 for x in range(count):62 click.echo("Hello!")63 64 .. click:run::65 invoke(hello, args=['--count', '2',])66+```67 57-Groups58-------------68+## Groups69 60-Basic Group Example61-^^^^^^^^^^^^^^^^^^^^^70+### Basic Group Example71+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.73 74+```{eval-rst}75 .. click:example::76+77 @click.group()78 def greeting():79 click.echo('Starting greeting ...')80 14 unmodified lines95 .. click:run::96 97 invoke(greeting, args=['say-hello'])98 invoke(greeting, args=['say-hello', '--help'])99+```100 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).102 90-Renaming Groups91-^^^^^^^^^^^^^^^^^103+### Renaming Groups104+105 To have a name other than the decorated function name as the group name, pass it in as the first positional argument.106 107+```{eval-rst}108 .. click:example::109+110 @click.group('greet-someone')111 def greeting():112 click.echo('Starting greeting ...')113 5 unmodified lines119 120 .. click:run::121 122 invoke(greeting, args=['say-hello'])123+```124 109-Group Invocation Without Command110-^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^125+### Group Invocation Without Command126 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.128 129+```{eval-rst}130 .. click:example::131 132 @click.group(invoke_without_command=True)133 @click.pass_context10 unmodified lines144 .. click:run::145 146 invoke(cli, prog_name='tool', args=[])147 invoke(cli, prog_name='tool', args=['sync'])148+```149 150+### Group Separation151 152+Command {ref}`parameters` attached to a command belong only to that command.153 135-Group Separation136-^^^^^^^^^^^^^^^^^^^137-Command :ref:`parameters` attached to a command belong only to that command.138-154+```{eval-rst}155 .. click:example::156+157 @click.group()158 def greeting():159 pass160 13 unmodified lines174 175 invoke(greeting, args=['hello', '--count', '2'])176 invoke(greeting, args=['goodbye', '--count', '2'])177 invoke(greeting)178+```179 180 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.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`.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.187 170-Arbitrary Nesting171-^^^^^^^^^^^^^^^^^^^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 Nesting189 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}193 .. click:example::194 195 @click.group()196 def cli():14 unmodified lines211 212 .. click:run::213 214 invoke(cli, args=['session', 'initdb'])215+```216 197-Lazily Attaching Commands198-^^^^^^^^^^^^^^^^^^^^^^^^^^^217+### Lazily Attaching Commands218+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.220 221+```{eval-rst}222 .. click:example::223 224 @click.group()225 def cli():12 unmodified lines238 .. click:run::239 240 invoke(cli, args=['initdb'])241 invoke(cli, args=['dropdb'])242+```243 222-Context Object223--------------------224-The :class:`Context` object is how commands and groups communicate.244+## Context Object245+The {class}`Context` object is how commands and groups communicate.246 226-Auto Envvar Prefix227-^^^^^^^^^^^^^^^^^^^^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 subcommand229-called ``run`` taking an option called ``reload`` and the prefix is ``WEB``, then the variable is ``WEB_RUN_RELOAD``.247+### Auto Envvar Prefix248 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 subcommand250+called `run` taking an option called `reload` and the prefix is `WEB`, then the variable is `WEB_RUN_RELOAD`.251+252 Example usage:253 254+```{eval-rst}255 .. click:example::256 257 @click.command()258 @click.option('--username')8 unmodified lines267 .. click:run::268 269 invoke(greet, env={'GREETER_USERNAME': 'john'},270 auto_envvar_prefix='GREETER')271+```272 250-When using ``auto_envvar_prefix`` with command groups, the command name273+When using `auto_envvar_prefix` with command groups, the command name274 needs to be included in the environment variable, between the prefix and252-the parameter name, *i.e.* ``PREFIX_COMMAND_VARIABLE``. If you have a253-subcommand called ``run-server`` taking an option called ``host`` and254-the prefix is ``WEB``, then the variable is ``WEB_RUN_SERVER_HOST``.275+the parameter name, *i.e.* `PREFIX_COMMAND_VARIABLE`. If you have a276+subcommand called `run-server` taking an option called `host` and277+the prefix is `WEB`, then the variable is `WEB_RUN_SERVER_HOST`.278 279+```{eval-rst}280 .. click:example::281 282 @click.group()283 @click.option('--debug/--no-debug')12 unmodified lines296 297 invoke(cli, args=['greet',],298 env={'GREETER_GREET_USERNAME': 'John', 'GREETER_DEBUG': 'false'},299 auto_envvar_prefix='GREETER')300+```301 277-Global Context Access278-^^^^^^^^^^^^^^^^^^^^^^302+### Global Context Access303 280-.. versionadded:: 5.0304+```{versionadded} 5.0305+```306 307 Starting with Click 5.0 it is possible to access the current context from308 anywhere within the same thread through the use of the284-:func:`get_current_context` function which returns it. This is primarily309+{func}`get_current_context` function which returns it. This is primarily310 useful for accessing the context bound object as well as some flags that311 are stored on it to customize the runtime behavior. For instance the287-: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`313 flag.314 290-Example usage::315+Example usage:316 317+```python318 def get_current_command_name():319 return click.get_current_context().info_name320+```321 322 It should be noted that this only works within the current thread. If you323 spawn additional threads then those threads will not have the ability to324 refer to the current context. If you want to give another thread the325 ability to refer to this context you need to use the context within the299-thread as a context manager::326+thread as a context manager:327 328+```python329 def spawn_thread(ctx, func):330 def wrapper():331 with ctx:332 func()333 t = threading.Thread(target=wrapper)334 t.start()335 return t336+```337 338 Now the thread function can access the context like the main thread would339 do. However if you do use this for threading you need to be very careful340 as the vast majority of the context is not thread safe! You are only341 allowed to read from the context, but not to perform any modifications on342 it.343 344 316-Detecting the Source of a Parameter317-^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^345+### Detecting the Source of a Parameter346 347 In some situations it's helpful to understand whether or not an option348 or parameter came from the command line, the environment, the default321-value, or :attr:`Context.default_map`. The322-:meth:`Context.get_parameter_source` method can be used to find this323-out. It will return a member of the :class:`~click.core.ParameterSource`349+value, or {attr}`Context.default_map`. The350+{meth}`Context.get_parameter_source` method can be used to find this351+out. It will return a member of the {class}`~click.core.ParameterSource`352 enum.353 354+```{eval-rst}355 .. click:example::356 357 @click.command()358 @click.argument('port', nargs=1, default=8080, envvar="PORT")9 unmodified lines368 invoke(cli, prog_name='cli', args=[], env={"PORT": "8080"})369 println()370 invoke(cli, prog_name='cli', args=[])371 println()372+```