Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ Unreleased
parallel tests, and Flask smoke tests. :pr:`3151` :pr:`3177`
- Show custom ``show_default`` string in prompts, matching the existing
help text behavior. :issue:`2836` :pr:`2837` :pr:`3165` :pr:`3262` :pr:`3280`
:pr:`3328``
:pr:`3328`
- Fix ``default=True`` with boolean ``flag_value`` always returning the
``flag_value`` instead of ``True``. The ``default=True`` to ``flag_value``
substitution now only applies to non-boolean flags, where ``True`` acts as a
Expand Down
38 changes: 29 additions & 9 deletions docs/arguments.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
Arguments are:

* Are positional in nature.
* Similar to a limited version of {ref}`options <options>` that can take an arbitrary number of inputs
* Similar to a limited version of {ref}`options <options>` that
can take an arbitrary number of inputs
* {ref}`Documented manually <documenting-arguments>`.

Useful and often used kwargs are:
Expand All @@ -18,7 +19,9 @@ Useful and often used kwargs are:

## Basic Arguments

A minimal {class}`click.Argument` solely takes one string argument: the name of the argument. This will assume the argument is required, has no default, and is of the type `str`.
A minimal {class}`click.Argument` solely takes one string argument:
the name of the argument. This will assume the argument is required,
has no default, and is of the type `str`.

Example:

Expand All @@ -38,17 +41,27 @@ And from the command line:
invoke(touch, args=['foo.txt'])
```

An argument may be assigned a {ref}`parameter type <parameter-types>`. If no type is provided, the type of the default value is used. If no default value is provided, the type is assumed to be {data}`STRING`.
An argument may be assigned a {ref}`parameter type <parameter-types>`.
If no type is provided, the type of the default value is used. If no
default value is provided, the type is assumed to be {data}`STRING`.

```{admonition} Note on Required Arguments
:class: note

It is possible to make an argument required by setting `required=True`. It is not recommended since we think command line tools should gracefully degrade into becoming no ops. We think this because command line tools are often invoked with wildcard inputs and they should not error out if the wildcard is empty.
It is possible to make an argument required by setting
`required=True`. It is not recommended since we think command line
tools should gracefully degrade into becoming no ops. We think this
because command line tools are often invoked with wildcard inputs
and they should not error out if the wildcard is empty.
```

## Multiple Arguments

To set the number of argument use the `nargs` kwarg. It can be set to any positive integer and -1. Setting it to -1, makes the number of arguments arbitrary (which is called variadic) and can only be used once. The arguments are then packed as a tuple and passed to the function.
To set the number of argument use the `nargs` kwarg. It can be set to
any positive integer and -1. Setting it to -1, makes the number of
arguments arbitrary (which is called variadic) and can only be used
once. The arguments are then packed as a tuple and passed to the
function.

```{eval-rst}
.. click:example::
Expand All @@ -71,12 +84,17 @@ And from the command line:
```{admonition} Note on Handling Files
:class: note

This is not how you should handle files and files paths. This merely used as a simple example. See {ref}`handling-files` to learn more about how to handle files in parameters.
This is not how you should handle files and files paths. This
merely used as a simple example. See {ref}`handling-files` to learn
more about how to handle files in parameters.
```

## Argument Escape Sequences

If you want to process arguments that look like options, like a file named `-foo.txt` or `--foo.txt` , you must pass the `--` separator first. After you pass the `--`, you may only pass arguments. This is a common feature for POSIX command line tools.
If you want to process arguments that look like options, like a file
named `-foo.txt` or `--foo.txt`, you must pass the `--` separator
first. After you pass the `--`, you may only pass arguments. This is a
common feature for POSIX command line tools.

Example usage:

Expand All @@ -97,7 +115,8 @@ And from the command line:
invoke(touch, ['--', '-foo.txt', 'bar.txt'])
```

If you don't like the `--` marker, you can set ignore_unknown_options to True to avoid checking unknown options:
If you don't like the `--` marker, you can set
`ignore_unknown_options` to `True` to avoid checking unknown options:

```{eval-rst}
.. click:example::
Expand All @@ -120,7 +139,8 @@ And from the command line:

## Environment Variables

Arguments can use environment variables. To do so, pass the name(s) of the environment variable(s) via `envvar` in `click.argument`.
Arguments can use environment variables. To do so, pass the name(s) of
the environment variable(s) via `envvar` in `click.argument`.

Checking one environment variable:

Expand Down
49 changes: 37 additions & 12 deletions docs/commands-and-groups.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
```{currentmodule} click
```

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

```{contents}
---
Expand Down Expand Up @@ -33,7 +36,8 @@ A simple command decorator takes no arguments.

### Renaming Commands

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

```{eval-rst}
.. click:example::
Expand Down Expand Up @@ -69,7 +73,9 @@ To mark a command as deprecated pass in `deprecated=True`

### Basic Group Example

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

```{eval-rst}
.. click:example::
Expand Down Expand Up @@ -98,11 +104,13 @@ At the command level:
invoke(greeting, args=['say-hello', '--help'])
```

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

### Renaming Groups

To have a name other than the decorated function name as the group name, pass it in as the first positional argument.
To have a name other than the decorated function name as the group name, pass it
in as the first positional argument.

```{eval-rst}
.. click:example::
Expand All @@ -124,7 +132,11 @@ To have a name other than the decorated function name as the group name, pass it

### Group Invocation Without Command

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

```{eval-rst}
.. click:example::
Expand Down Expand Up @@ -177,13 +189,19 @@ Command {ref}`parameters` attached to a command belong only to that command.
invoke(greeting)
```

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

This behavior is observable with the `--help` option. Suppose we have a group called `tool` containing a command called `sub`.
This behavior is observable with the `--help` option. Suppose we have a group
called `tool` containing a command called `sub`.

- `tool --help` returns the help for the whole program (listing subcommands).
- `tool sub --help` returns the help for the `sub` subcommand.
- 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.
- 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.

### Arbitrary Nesting

Expand Down Expand Up @@ -216,7 +234,10 @@ This behavior is observable with the `--help` option. Suppose we have a group ca

### Lazily Attaching Commands

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

```{eval-rst}
.. click:example::
Expand Down Expand Up @@ -246,8 +267,12 @@ The {class}`Context` object is how commands and groups communicate.

### Auto Envvar Prefix

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
called `run` taking an option called `reload` and the prefix is `WEB`, then the variable is `WEB_RUN_RELOAD`.
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
called `run` taking an option called `reload` and the prefix is `WEB`, then the
variable is `WEB_RUN_RELOAD`.

Example usage:

Expand Down
3 changes: 2 additions & 1 deletion docs/commands.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
```{currentmodule} click
```

In addition to the capabilities covered in the previous section, Groups have more advanced capabilities that leverage the Context.
In addition to the capabilities covered in the previous section, Groups have
more advanced capabilities that leverage the Context.

```{contents}
---
Expand Down
Loading