Skip to content
Myr

options.md

This file

Path

docs/options.md

636 lines · 17.8 KiB · Markdown

Last changed by

Apply format guidelines to commits since latest 8.3.2 release (#3343)Rowlando13before adoptionf1f191e

History of this file

Take it

Convert options docs to md. (#2924)Rowlando13before adoption937c68d

lines 1–9why, one step back
1(options)=
23# Options45```{eval-rst}6.. currentmodule:: click7```89Adding options to commands can be accomplished with the {func}`option`

Apply format guidelines to commits since latest 8.3.2 release (#3343)Rowlando13before adoptionf1f191e

lines 10–11why, one step back
10decorator. At runtime the decorator invokes the {class}`Option` class. Options
11in Click are distinct from {ref}`positional arguments <arguments>`.

Convert options docs to md. (#2924)Rowlando13before adoption937c68d

lines 12–27why, one step back
12
13Useful and often used kwargs are:1415- `default`: Passes a default.16- `help`: Sets help message.17- `nargs`: Sets the number of arguments.18- `required`: Makes option required.19- `type`: Sets {ref}`parameter type <parameter-types>`2021```{contents}22:depth: 223:local: true24```2526## Option Decorator27

Apply format guidelines to commits since latest 8.3.2 release (#3343)Rowlando13before adoptionf1f191e

lines 28–29why, one step back
28The {func}`option()` decorator is usually passed two positional arguments: the
29option name and the decorated function argument name.

Convert options docs to md. (#2924)Rowlando13before adoption937c68d

lines 30–44why, one step back
30
31```{eval-rst}32.. click:example::3334 @click.command()35 @click.option('--string-to-echo', 'string_to_echo')36 def echo(string_to_echo):37 click.echo(string_to_echo)383940.. click:run::4142 invoke(echo, args=['--help'])43```44

Apply format guidelines to commits since latest 8.3.2 release (#3343)Rowlando13before adoptionf1f191e

lines 45–48why, one step back
45However, if the decorated function argument name is not passed in, then Click
46will try to infer it. A simple way to name the option so that Click will infer47it correctly is by taking the function argument, adding two dashes to the front48and converting underscores to dashes.

Convert options docs to md. (#2924)Rowlando13before adoption937c68d

lines 49–62why, one step back
49
50```{eval-rst}51.. click:example::5253 @click.command()54 @click.option('--string-to-echo')55 def echo(string_to_echo):56 click.echo(string_to_echo)5758.. click:run::5960 invoke(echo, args=['--string-to-echo', 'Hi!'])61```62

Apply format guidelines to commits since latest 8.3.2 release (#3343)Rowlando13before adoptionf1f191e

lines 63–64why, one step back
63More formally, Click will try to infer the decorated function argument name as
64follows:

Convert options docs to md. (#2924)Rowlando13before adoption937c68d

line 65why, one step back
65

revise 'Options' documentation (#3192)Rowlando13before adoption81efdc9

line 66why, one step back
661. 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.

Apply format guidelines to commits since latest 8.3.2 release (#3343)Rowlando13before adoptionf1f191e

lines 67–68why, one step back
672. If multiple positional arguments are prefixed with `--`, the first one
68 declared is chosen.

revise 'Options' documentation (#3192)Rowlando13before adoption81efdc9

line 69why, one step back
693. Otherwise, the first positional argument prefixed with `-` is chosen.

Convert options docs to md. (#2924)Rowlando13before adoption937c68d

line 70why, one step back
70

Apply format guidelines to commits since latest 8.3.2 release (#3343)Rowlando13before adoptionf1f191e

lines 71–73why, one step back
71To get the argument name, the chosen positional argument is converted to lower
72case, a leading `-` or `--` is removed if found, and any remaining `-`73characters are replaced with `_`.

Convert options docs to md. (#2924)Rowlando13before adoption937c68d

lines 74–76why, one step back
74
75```{eval-rst}76.. list-table:: Examples

revise 'Options' documentation (#3192)Rowlando13before adoption81efdc9

line 77why, one step back
77 :widths: 15 15

Convert options docs to md. (#2924)Rowlando13before adoption937c68d

lines 78–80why, one step back
78 :header-rows: 1
7980 * - Decorator Arguments

revise 'Options' documentation (#3192)Rowlando13before adoption81efdc9

line 81why, one step back
81 - Inferred Argument Name

Convert options docs to md. (#2924)Rowlando13before adoption937c68d

lines 82–99why, one step back
82 * - ``"-f", "--foo-bar"``
83 - foo_bar84 * - ``"-x"``85 - x86 * - ``"-f", "--filename", "dest"``87 - dest88 * - ``"--CamelCase"``89 - camelcase90 * - ``"-f", "-fb"``91 - f92 * - ``"--f", "--foo-bar"``93 - f94 * - ``"---f"``95 - _f96```9798## Basic Example99

Apply format guidelines to commits since latest 8.3.2 release (#3343)Rowlando13before adoptionf1f191e

lines 100–103why, one step back
100A simple {class}`click.Option` takes one option name. By default, it's assumed
101that the decorated function argument is not required and the expected type is102`str`. If the decorated function takes a positional argument but the option is103not passed with the command, then `None` is passed.

Convert options docs to md. (#2924)Rowlando13before adoption937c68d

lines 104–128why, one step back
104
105```{eval-rst}106.. click:example::107108 @click.command()109 @click.option('--text')110 def print_this(text):111 click.echo(text)112113114.. click:run::115116 invoke(print_this, args=['--text=this'])117118 invoke(print_this, args=[])119120121.. click:run::122123 invoke(print_this, args=['--help'])124125```126127## Setting a Default128

Apply format guidelines to commits since latest 8.3.2 release (#3343)Rowlando13before adoptionf1f191e

lines 129–130why, one step back
129Instead of setting the `type`, you may set a default and Click will try to infer
130the type.

Convert options docs to md. (#2924)Rowlando13before adoption937c68d

lines 131–146why, one step back
131
132```{eval-rst}133.. click:example::134135 @click.command()136 @click.option('--n', default=1)137 def dots(n):138 click.echo('.' * n)139140.. click:run::141142 invoke(dots, args=['--help'])143```144145## Multi Value Options146

Apply format guidelines to commits since latest 8.3.2 release (#3343)Rowlando13before adoptionf1f191e

lines 147–149why, one step back
147To make an option take multiple values, pass in `nargs`. Note you may pass in
148any positive integer, but not -1. The values are passed to the decorated149function as a tuple.

Convert options docs to md. (#2924)Rowlando13before adoption937c68d

lines 150–169why, one step back
150
151```{eval-rst}152.. click:example::153154 @click.command()155 @click.option('--pos', nargs=2, type=float)156 def findme(pos):157 a, b = pos158 click.echo(f"{a} / {b}")159160.. click:run::161162 invoke(findme, args=['--pos', '2.0', '3.0'])163164```165166(tuple-type)=167168## Multi Value Options as Tuples169

Fix version added and version changed in options docs. (#2957)Rowlando13before adoption497d1b6

lines 170–171why, one step back
170```{versionadded} 4.0
171```

Convert options docs to md. (#2924)Rowlando13before adoption937c68d

line 172why, one step back
172

revise 'Options' documentation (#3192)Rowlando13before adoption81efdc9

line 173why, one step back
173By setting `nargs` to a specific number, each item in

Apply format guidelines to commits since latest 8.3.2 release (#3343)Rowlando13before adoptionf1f191e

lines 174–175why, one step back
174the resulting tuple is of the same type. Alternatively, you might want to use
175different types for different indexes in

revise 'Options' documentation (#3192)Rowlando13before adoption81efdc9

line 176why, one step back
176the tuple. For this you can directly specify a tuple as `type`:

Convert options docs to md. (#2924)Rowlando13before adoption937c68d

lines 177–194why, one step back
177
178```{eval-rst}179.. click:example::180181 @click.command()182 @click.option('--item', type=(str, int))183 def putitem(item):184 name, id = item185 click.echo(f"name={name} id={id}")186187188And on the command line:189190.. click:run::191192 invoke(putitem, args=['--item', 'peter', '1338'])193```194

revise 'Options' documentation (#3192)Rowlando13before adoption81efdc9

line 195why, one step back
195By using a tuple literal as the `type`, `nargs` gets automatically set to the

Convert options docs to md. (#2924)Rowlando13before adoption937c68d

lines 196–212why, one step back
196length of the tuple and the {class}`click.Tuple` type is automatically
197used. The above example is thus equivalent to this:198199```{eval-rst}200.. click:example::201202 @click.command()203 @click.option('--item', nargs=2, type=click.Tuple([str, int]))204 def putitem(item):205 name, id = item206 click.echo(f"name={name} id={id}")207```208209(multiple-options)=210211## Multiple Options212

Apply format guidelines to commits since latest 8.3.2 release (#3343)Rowlando13before adoptionf1f191e

lines 213–216why, one step back
213The multiple options format allows options to take an arbitrary number of
214arguments (which is called variadic). The arguments are passed to the decorated215function as a tuple. If set, `default` must be a list or tuple. Setting a string216as `default` will be interpreted as a list of characters.

Convert options docs to md. (#2924)Rowlando13before adoption937c68d

lines 217–223why, one step back
217
218```{eval-rst}219.. click:example::220221 @click.command()222 @click.option('--message', '-m', multiple=True)223 def commit(message):

Fix option multiple docs (#3086)Rowlando13before adoption2ed395b

lines 224–226why, one step back
224 click.echo(message)
225 for m in message:226 click.echo(m)

Convert options docs to md. (#2924)Rowlando13before adoption937c68d

lines 227–234why, one step back
227
228.. click:run::229230 invoke(commit, args=['-m', 'foo', '-m', 'bar', '-m', 'here'])231```232233## Counting234

Apply format guidelines to commits since latest 8.3.2 release (#3343)Rowlando13before adoptionf1f191e

lines 235–237why, one step back
235To count the occurrence of an option, set `count=True`. If the option is not
236passed on the command line, then the count is 0. Counting is commonly used for237verbosity.

Convert options docs to md. (#2924)Rowlando13before adoption937c68d

lines 238–256why, one step back
238
239```{eval-rst}240.. click:example::241242 @click.command()243 @click.option('-v', '--verbose', count=True)244 def log(verbose):245 click.echo(f"Verbosity: {verbose}")246247.. click:run::248249 invoke(log, args=[])250 invoke(log, args=['-vvv'])251```252253(option-boolean-flag)=254255## Boolean256

Apply format guidelines to commits since latest 8.3.2 release (#3343)Rowlando13before adoptionf1f191e

lines 257–259why, one step back
257Boolean options (boolean flags) take the values `True` or `False`. The simplest
258case sets the default value to `False` if the flag is not passed, and `True` if259it is.

Convert options docs to md. (#2924)Rowlando13before adoption937c68d

lines 260–281why, one step back
260
261```{eval-rst}262.. click:example::263264 import sys265266 @click.command()267 @click.option('--shout', is_flag=True)268 def info(shout):269 rv = sys.platform270 if shout:271 rv = rv.upper() + '!!!!111'272 click.echo(rv)273274275.. click:run::276277 invoke(info)278 invoke(info, args=['--shout'])279280```281

Apply format guidelines to commits since latest 8.3.2 release (#3343)Rowlando13before adoptionf1f191e

lines 282–283why, one step back
282To implement this more explicitly, declare `--{on-option}/--{off-option}`. Click
283will automatically set `is_flag=True`.

Convert options docs to md. (#2924)Rowlando13before adoption937c68d

lines 284–303why, one step back
284
285```{eval-rst}286.. click:example::287288 import sys289290 @click.command()291 @click.option('--shout/--no-shout', default=False)292 def info(shout):293 rv = sys.platform294 if shout:295 rv = rv.upper() + '!!!!111'296 click.echo(rv)297298.. click:run::299300 invoke(info)301 invoke(info, args=['--shout'])302 invoke(info, args=['--no-shout'])303```

Document option no option pattern better (#3103)Rowlando13before adoption06f52a4

lines 304–306why, one step back
304
305Use cases for this more explicit pattern include:306

Apply format guidelines to commits since latest 8.3.2 release (#3343)Rowlando13before adoptionf1f191e

lines 307–308why, one step back
307* The default can be dynamic so the user can explicitly specify the option with
308 either on or off option, or pass in no option to use the dynamic default.

Document option no option pattern better (#3103)Rowlando13before adoption06f52a4

line 309why, one step back
309* Shell scripts sometimes want to be explicit even when it's the default

Apply format guidelines to commits since latest 8.3.2 release (#3343)Rowlando13before adoptionf1f191e

lines 310–311why, one step back
310* Shell aliases can set a flag, then an invocation can add a negation of the
311 flag

Convert options docs to md. (#2924)Rowlando13before adoption937c68d

line 312why, one step back
312

Apply format guidelines to commits since latest 8.3.2 release (#3343)Rowlando13before adoptionf1f191e

lines 313–315why, one step back
313If a forward slash(`/`) is contained in your option name already, you can split
314the parameters using `;`. In Windows `/` is commonly used as the prefix315character.

Convert options docs to md. (#2924)Rowlando13before adoption937c68d

lines 316–325why, one step back
316
317```{eval-rst}318.. click:example::319320 @click.command()321 @click.option('/debug;/no-debug')322 def log(debug):323 click.echo(f"debug={debug}")324```325

Fix version added and version changed in options docs. (#2957)Rowlando13before adoption497d1b6

lines 326–327why, one step back
326```{versionchanged} 6.0
327```

Convert options docs to md. (#2924)Rowlando13before adoption937c68d

line 328why, one step back
328

Apply format guidelines to commits since latest 8.3.2 release (#3343)Rowlando13before adoptionf1f191e

lines 329–330why, one step back
329If you want to define an alias for the second option only, then you will need to
330use leading whitespace to disambiguate the format string.

Convert options docs to md. (#2924)Rowlando13before adoption937c68d

lines 331–351why, one step back
331
332```{eval-rst}333.. click:example::334335 import sys336337 @click.command()338 @click.option('--shout/--no-shout', ' /-N', default=False)339 def info(shout):340 rv = sys.platform341 if shout:342 rv = rv.upper() + '!!!!111'343 click.echo(rv)344345.. click:run::346347 invoke(info, args=['--help'])348```349350## Flag Value351

Apply format guidelines to commits since latest 8.3.2 release (#3343)Rowlando13before adoptionf1f191e

lines 352–354why, one step back
352To have a flag pass a value to the decorated function set `flag_value`. This
353automatically sets `is_flag=True`. To mark the flag as default, set354`default=True`. Setting flag values can be used to create patterns like this:

Convert options docs to md. (#2924)Rowlando13before adoption937c68d

lines 355–374why, one step back
355
356```{eval-rst}357.. click:example::358359 import sys360361 @click.command()362 @click.option('--upper', 'transformation', flag_value='upper', default=True)363 @click.option('--lower', 'transformation', flag_value='lower')364 def info(transformation):365 click.echo(getattr(sys.platform, transformation)())366367.. click:run::368369 invoke(info, args=['--help'])370 invoke(info, args=['--upper'])371 invoke(info, args=['--lower'])372 invoke(info)373```374

Reconcile default value passing and default activation (#3239)Rowlando13before adoption8c95c73

line 375why, one step back
375### How `default` and `flag_value` interact

Merge branch 'main' into stableDavid Lordbefore adoption81a482f

line 376why, one step back
376

Reconcile default value passing and default activation (#3239)Rowlando13before adoption8c95c73

lines 377–379why, one step back
377The `default` value is given to the underlying function
378as-is. So if you set `default=None`, the function receives379`None`. Same for any other type.

Merge branch 'main' into stableDavid Lordbefore adoption81a482f

line 380why, one step back
380

Reconcile default value passing and default activation (#3239)Rowlando13before adoption8c95c73

lines 381–385why, one step back
381But there is a special case for **non-boolean** flags: if a
382flag has a non-boolean `flag_value` (like a string or a383class), then `default=True` is interpreted as *the flag384should be activated by default*. The function receives the385`flag_value`, not the Python `True`.

Merge branch 'main' into stableDavid Lordbefore adoption81a482f

line 386why, one step back
386

Reconcile default value passing and default activation (#3239)Rowlando13before adoption8c95c73

lines 387–388why, one step back
387Which means, in the example above, this option:
388

Merge branch 'main' into stableDavid Lordbefore adoption81a482f

lines 389–398why, one step back
389```python
390@click.option('--upper', 'transformation', flag_value='upper', default=True)391```392393is equivalent to:394395```python396@click.option('--upper', 'transformation', flag_value='upper', default='upper')397```398

Reconcile default value passing and default activation (#3239)Rowlando13before adoption8c95c73

lines 399–437why, one step back
399Because the two are equivalent, it is recommended to always
400use the second form and set `default` to the actual value401you want. This makes code more explicit and predictable.402403This special case does **not** apply to boolean flags (where404`flag_value` is `True` or `False`). For boolean flags,405`default=True` is the literal Python value `True`.406407The tables below show the value received by the function for408each combination of `default`, `flag_value`, and whether409the flag was passed on the command line.410411#### Boolean flags (`is_flag=True`, boolean `flag_value`)412413These are flags where `flag_value` is `True` or `False`.414The `default` value is always passed through literally415without any special substitution.416417| `default` | `flag_value` | Not passed | `--flag` passed |418|-----------|--------------|------------|-----------------|419| *(unset)* | *(unset)* | `False` | `True` |420| `True` | *(unset)* | `True` | `True` |421| `False` | *(unset)* | `False` | `True` |422| `None` | *(unset)* | `None` | `True` |423| `True` | `True` | `True` | `True` |424| `True` | `False` | `True` | `False` |425| `False` | `True` | `False` | `True` |426| `False` | `False` | `False` | `False` |427| `None` | `True` | `None` | `True` |428| `None` | `False` | `None` | `False` |429430````{tip}431For a negative flag that defaults to off, prefer the432explicit pair form `--with-xyz/--without-xyz` over the433single-flag `flag_value=False, default=True`:434435```python436@click.option('--with-xyz/--without-xyz', 'enable_xyz', default=True)437```

Merge branch 'main' into stableDavid Lordbefore adoption81a482f

line 438why, one step back
438````

Reconcile default value passing and default activation (#3239)Rowlando13before adoption8c95c73

lines 439–480why, one step back
439
440#### Boolean flag pairs (`--flag/--no-flag`)441442These use secondary option names to provide both an on and443off switch. The `default` value is always literal.444445| `default` | Not passed | `--flag` | `--no-flag` |446|-----------|------------|----------|-------------|447| *(unset)* | `False` | `True` | `False` |448| `True` | `True` | `True` | `False` |449| `False` | `False` | `True` | `False` |450| `None` | `None` | `True` | `False` |451452#### Non-boolean feature switches (`flag_value` is a string, class, etc.)453454For these flags, `default=True` is a **special case**: it455means "activate this flag by default" and resolves to the456`flag_value`. All other `default` values are passed through457literally.458459| `default` | `flag_value` | Not passed | `--flag` passed |460|------------|--------------|-------------|-----------------|461| *(unset)* | `"upper"` | `None` | `"upper"` |462| `True` | `"upper"` | `"upper"`¹ | `"upper"` |463| `"lower"` | `"upper"` | `"lower"` | `"upper"` |464| `None` | `"upper"` | `None` | `"upper"` |465466```{hint}467¹: `default=True` is substituted with `flag_value`.468```469470#### Feature switch groups (multiple flags sharing one variable)471472When multiple `flag_value` options target the same parameter473name, `default=True` on one of them marks it as the default474choice.475476| Definition | Not passed | `--upper` | `--lower` |477|--------------------------------------------------------|------------|-----------|-----------|478| `--upper` with `flag_value='upper'`, `default=True` | `"upper"` | `"upper"` | `"lower"` |479| `--upper` with `flag_value='upper'`, `default='upper'` | `"upper"` | `"upper"` | `"lower"` |480| Both without `default` | `None` | `"upper"` | `"lower"` |

Merge branch 'main' into stableDavid Lordbefore adoption81a482f

line 481why, one step back
481

Convert options docs to md. (#2924)Rowlando13before adoption937c68d

lines 482–483why, one step back
482## Values from Environment Variables
483

revise 'Options' documentation (#3192)Rowlando13before adoption81efdc9

line 484why, one step back
484To pass in a value from a specific environment variable use `envvar`.

Convert options docs to md. (#2924)Rowlando13before adoption937c68d

lines 485–514why, one step back
485
486```{eval-rst}487.. click:example::488489 @click.command()490 @click.option('--username', envvar='USERNAME')491 def greet(username):492 click.echo(f"Hello {username}!")493494.. click:run::495496 invoke(greet, env={'USERNAME': 'john'})497```498499If a list is passed to `envvar`, the first environment variable found is picked.500501```{eval-rst}502.. click:example::503504 @click.command()505 @click.option('--username', envvar=['ALT_USERNAME', 'USERNAME'])506 def greet(username):507 click.echo(f"Hello {username}!")508509.. click:run::510511 invoke(greet, env={'ALT_USERNAME': 'Bill', 'USERNAME': 'john'})512513```514

Fix reconciliation of envvar with `default`, `flag_value` and `type` parameters for flag options (#2956)Kevin Deldyckebefore adoption565f36d

lines 515–516why, one step back
515Variable names are:
516 - [Case-insensitive on Windows but not on other platforms](https://github.com/python/cpython/blob/aa9eb5f757ceff461e6e996f12c89e5d9b583b01/Lib/os.py#L777-L789).

Apply format guidelines to commits since latest 8.3.2 release (#3343)Rowlando13before adoptionf1f191e

lines 517–518why, one step back
517 - Not stripped of whitespace and should match the exact name provided to the
518 `envvar` argument.

Fix reconciliation of envvar with `default`, `flag_value` and `type` parameters for flag options (#2956)Kevin Deldyckebefore adoption565f36d

line 519why, one step back
519

Apply format guidelines to commits since latest 8.3.2 release (#3343)Rowlando13before adoptionf1f191e

lines 520–522why, one step back
520For flag options, there are two concepts to consider: the activation of the flag
521driven by the environment variable, and the value of the flag if it is522activated.

Fix reconciliation of envvar with `default`, `flag_value` and `type` parameters for flag options (#2956)Kevin Deldyckebefore adoption565f36d

line 523why, one step back
523

Apply format guidelines to commits since latest 8.3.2 release (#3343)Rowlando13before adoptionf1f191e

lines 524–526why, one step back
524The values read from environment variables are always strings and will require
525extra processing. We need to transform these strings into boolean values that526will determine if the flag is activated or not.

Fix reconciliation of envvar with `default`, `flag_value` and `type` parameters for flag options (#2956)Kevin Deldyckebefore adoption565f36d

lines 527–529why, one step back
527
528Here are the rules used to parse environment variable values for flag options:529 - `true`, `1`, `yes`, `on`, `t`, `y` are interpreted as activating the flag

Apply format guidelines to commits since latest 8.3.2 release (#3343)Rowlando13before adoptionf1f191e

lines 530–533why, one step back
530 - `false`, `0`, `no`, `off`, `f`, `n` are interpreted as deactivating the
531 flag532 - The presence of the environment variable without value is interpreted as533 deactivating the flag

Fix reconciliation of envvar with `default`, `flag_value` and `type` parameters for flag options (#2956)Kevin Deldyckebefore adoption565f36d

line 534why, one step back
534 - Empty strings are interpreted as deactivating the flag

Apply format guidelines to commits since latest 8.3.2 release (#3343)Rowlando13before adoptionf1f191e

lines 535–542why, one step back
535 - Values are case-insensitive, so the `True`, `TRUE`, `tRuE` strings are all
536 interpreted as activating the flag537 - Values are stripped of leading and trailing whitespace before being538 interpreted, so the `" True "` string is transformed to `"true"` and thus539 activates the flag540 - If the flag option has a `flag_value` argument, passing that value in the541 environment variable will activate the flag, in addition to all the cases542 described above

Fix reconciliation of envvar with `default`, `flag_value` and `type` parameters for flag options (#2956)Kevin Deldyckebefore adoption565f36d

lines 543–544why, one step back
543 - Any other value is interpreted as deactivating the flag
544

Merge branch 'main' into stableDavid Lordbefore adoption81a482f

lines 545–546why, one step back
545```{caution}
546For boolean flags with a pair of values, the only recognized environment variable is the one provided to the `envvar` argument.

Fix reconciliation of envvar with `default`, `flag_value` and `type` parameters for flag options (#2956)Kevin Deldyckebefore adoption565f36d

line 547why, one step back
547

Merge branch 'main' into stableDavid Lordbefore adoption81a482f

lines 548–549why, one step back
548So an option defined as `--flag\--no-flag`, with a `envvar="FLAG"` parameter, there is no magical `NO_FLAG=<anything>` variable that is recognized. Only the `FLAG=<anything>` environment variable is recognized.
549```

Fix reconciliation of envvar with `default`, `flag_value` and `type` parameters for flag options (#2956)Kevin Deldyckebefore adoption565f36d

line 550why, one step back
550

Apply format guidelines to commits since latest 8.3.2 release (#3343)Rowlando13before adoptionf1f191e

lines 551–552why, one step back
551If the flag is activated, its value is set to `flag_value`. Otherwise, the value
552defaults to `None`.

Fix reconciliation of envvar with `default`, `flag_value` and `type` parameters for flag options (#2956)Kevin Deldyckebefore adoption565f36d

line 553why, one step back
553

Convert options docs to md. (#2924)Rowlando13before adoption937c68d

lines 554–556why, one step back
554## Multiple Options from Environment Values
555556As options can accept multiple values, pulling in such values from

Apply format guidelines to commits since latest 8.3.2 release (#3343)Rowlando13before adoptionf1f191e

lines 557–559why, one step back
557environment variables (which are strings) is a bit more complex. Click handles
558this by deferring customization of the behavior to the `type`. For both559`multiple` and `nargs` with values other than

Convert options docs to md. (#2924)Rowlando13before adoption937c68d

lines 560–589why, one step back
560`1`, Click will invoke the {meth}`ParamType.split_envvar_value` method to
561perform the splitting.562563The default implementation for all types is to split on whitespace. The564exceptions to this rule are the {class}`File` and {class}`Path` types565which both split according to the operating system's path splitting rules.566On Unix systems like Linux and OS X, the splitting happens on567every colon (`:`), and for Windows, splitting on every semicolon (`;`).568569```{eval-rst}570.. click:example::571572 @click.command()573 @click.option('paths', '--path', envvar='PATHS', multiple=True,574 type=click.Path())575 def perform(paths):576 for path in paths:577 click.echo(path)578579 if __name__ == '__main__':580 perform()581582.. click:run::583584 import os585 invoke(perform, env={"PATHS": f"./foo/bar{os.path.pathsep}./test"})586```587588## Other Prefix Characters589

Apply format guidelines to commits since latest 8.3.2 release (#3343)Rowlando13before adoptionf1f191e

lines 590–592why, one step back
590Click can deal with prefix characters besides `-` for options, including `/` and
591`+`, as well as others. Note that alternative prefix characters are generally592used very sparingly if at all within POSIX.

Convert options docs to md. (#2924)Rowlando13before adoption937c68d

lines 593–607why, one step back
593
594```{eval-rst}595.. click:example::596597 @click.command()598 @click.option('+w/-w')599 def chmod(w):600 click.echo(f"writable={w}")601602.. click:run::603604 invoke(chmod, args=['+w'])605 invoke(chmod, args=['-w'])606```607

Apply format guidelines to commits since latest 8.3.2 release (#3343)Rowlando13before adoptionf1f191e

lines 608–609why, one step back
608There are special considerations for using `/` as prefix character. See
609{ref}`option-boolean-flag` for more.

Convert options docs to md. (#2924)Rowlando13before adoption937c68d

lines 610–619why, one step back
610
611(optional-value)=612613## Optional Value614615Providing the value to an option can be made optional, in which case616providing only the option's flag without a value will either show a617prompt or use its `flag_value`.618619Setting `is_flag=False, flag_value=value` tells Click that the option

Fixed the description of `is_flag=False, flag_value=value` (#2938)Rowlando13before adoptionbf7e691

lines 620–621why, one step back
620can still be passed a value, but if only the flag is given, the
621value will be `flag_value`.

Convert options docs to md. (#2924)Rowlando13before adoption937c68d

lines 622–636why, one step back
622
623```{eval-rst}624.. click:example::625626 @click.command()627 @click.option("--name", is_flag=False, flag_value="Flag", default="Default")628 def hello(name):629 click.echo(f"Hello, {name}!")630631.. click:run::632633 invoke(hello, args=[])634 invoke(hello, args=["--name", "Value"])635 invoke(hello, args=["--name"])636```