Skip to content
Myr

apply review suggestions

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Brian Hall · committed by Brian Hall · parent 74e542e · Browse files · 1 file+7 added−9 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+7 added−9 removed

  • other:+7 added−9 removed

1 file+7 added−9 removed

modified:docs/options.md+7 added-9 removed
22 unmodified lines
23```2425## Option Decorator2627The {func}`option()` decorator accepts parameter declarations as positional arguments. Parameter declarations can consist of any number of option names and, optionally, a single identifier matching an argument name in the decorated callback function.27The {func}`option()` decorator is usually passed two positional arguments: the option name and the decorated function argument name.2829```{eval-rst}30.. click:example::31
7 unmodified lines
3940 invoke(echo, args=['--help'])41```4243If an identifier for the callback argument is not declared, then Click will try to infer it. A simple way to name an option is by prepending `--` to the callback argument name and converting underscores to `-`; this ensures that Click can correctly infer the callback argument name. Consequently, {func}`option()` only requires declarations for the option name(s).43However, if the decorated function argument name is not passed in, then Click will try to infer it. A simple way to name the option so that Click will infer it correctly is by taking the function argument, adding two dashes to the front and converting underscores to dashes.4445```{eval-rst}46.. click:example::47
6 unmodified lines
5455 invoke(echo, args=['--string-to-echo', 'Hi!'])56```5758More formally, Click will try to infer the callback argument name as follows:58More formally, Click will try to infer the decorated function argument name as follows:59601. If a positional argument is a valid [Python identifier], it is chosen.612. If multiple positional arguments are prefixed with `--`, the one that was declared first is chosen.601. If a positional argument is a valid [Python identifier](https://docs.python.org/3/reference/lexical_analysis.html#identifiers) (and thus does not have dashes), it is chosen.612. If multiple positional arguments are prefixed with `--`, the first one declared is chosen.623. Otherwise, the first positional argument prefixed with `-` is chosen.6364[Python identifier]: https://docs.python.org/3/reference/lexical_analysis.html#identifiers6564To get the argument name, the chosen positional argument is converted to lower case, a leading `-` or `--` is removed if found, and any remaining `-` characters are replaced with `_`.6566```{eval-rst}67.. list-table:: Examples
107 unmodified lines
175176 invoke(putitem, args=['--item', 'peter', '1338'])177```178181By using a tuple literal as `type`, `nargs` gets automatically set to the179By using a tuple literal as the `type`, `nargs` gets automatically set to the180length of the tuple and the {class}`click.Tuple` type is automatically181used. The above example is thus equivalent to this:182183```{eval-rst}
269 unmodified lines
453```454455## Other Prefix Characters456459Click can deal with prefix characters besides `-` for options, including `/` and `+`. Note that alternative prefix characters are generally used very sparingly if at all within POSIX.457Click can deal with prefix characters besides `-` for options, including `/` and `+`, as well as others. Note that alternative prefix characters are generally used very sparingly if at all within POSIX.458459```{eval-rst}460.. click:example::461
39 unmodified lines