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 fileTake it
Convert options docs to md. (#2924)Rowlando13before adoption937c68d
lines 1–9why, one step back1(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 back10decorator. At runtime the decorator invokes the {class}`Option` class. Options11in Click are distinct from {ref}`positional arguments <arguments>`.Convert options docs to md. (#2924)Rowlando13before adoption937c68d
lines 12–27why, one step back1213Useful 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 Decorator27Apply format guidelines to commits since latest 8.3.2 release (#3343)Rowlando13before adoptionf1f191e
lines 28–29why, one step back28The {func}`option()` decorator is usually passed two positional arguments: the29option name and the decorated function argument name.Convert options docs to md. (#2924)Rowlando13before adoption937c68d
lines 30–44why, one step back3031```{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```44Apply format guidelines to commits since latest 8.3.2 release (#3343)Rowlando13before adoptionf1f191e
lines 45–48why, one step back45However, if the decorated function argument name is not passed in, then Click46will 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 back4950```{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```62Apply format guidelines to commits since latest 8.3.2 release (#3343)Rowlando13before adoptionf1f191e
lines 63–64why, one step back63More formally, Click will try to infer the decorated function argument name as64follows:Convert options docs to md. (#2924)Rowlando13before adoption937c68d
line 65why, one step back65revise 'Options' documentation (#3192)Rowlando13before adoption81efdc9
line 66why, one step back661. 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 back672. If multiple positional arguments are prefixed with `--`, the first one68 declared is chosen.revise 'Options' documentation (#3192)Rowlando13before adoption81efdc9
line 69why, one step back693. Otherwise, the first positional argument prefixed with `-` is chosen.Convert options docs to md. (#2924)Rowlando13before adoption937c68d
line 70why, one step back70Apply format guidelines to commits since latest 8.3.2 release (#3343)Rowlando13before adoptionf1f191e
lines 71–73why, one step back71To get the argument name, the chosen positional argument is converted to lower72case, 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 back7475```{eval-rst}76.. list-table:: Examplesrevise 'Options' documentation (#3192)Rowlando13before adoption81efdc9
line 77why, one step back77 :widths: 15 15Convert options docs to md. (#2924)Rowlando13before adoption937c68d
lines 78–80why, one step back78 :header-rows: 17980 * - Decorator Argumentsrevise 'Options' documentation (#3192)Rowlando13before adoption81efdc9
line 81why, one step back81 - Inferred Argument NameConvert options docs to md. (#2924)Rowlando13before adoption937c68d
lines 82–99why, one step back82 * - ``"-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 Example99Apply format guidelines to commits since latest 8.3.2 release (#3343)Rowlando13before adoptionf1f191e
lines 100–103why, one step back100A simple {class}`click.Option` takes one option name. By default, it's assumed101that 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 back104105```{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 Default128Apply format guidelines to commits since latest 8.3.2 release (#3343)Rowlando13before adoptionf1f191e
lines 129–130why, one step back129Instead of setting the `type`, you may set a default and Click will try to infer130the type.Convert options docs to md. (#2924)Rowlando13before adoption937c68d
lines 131–146why, one step back131132```{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 Options146Apply format guidelines to commits since latest 8.3.2 release (#3343)Rowlando13before adoptionf1f191e
lines 147–149why, one step back147To make an option take multiple values, pass in `nargs`. Note you may pass in148any 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 back150151```{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 Tuples169Fix version added and version changed in options docs. (#2957)Rowlando13before adoption497d1b6
lines 170–171why, one step back170```{versionadded} 4.0171```Convert options docs to md. (#2924)Rowlando13before adoption937c68d
line 172why, one step back172revise 'Options' documentation (#3192)Rowlando13before adoption81efdc9
line 173why, one step back173By setting `nargs` to a specific number, each item inApply format guidelines to commits since latest 8.3.2 release (#3343)Rowlando13before adoptionf1f191e
lines 174–175why, one step back174the resulting tuple is of the same type. Alternatively, you might want to use175different types for different indexes inrevise 'Options' documentation (#3192)Rowlando13before adoption81efdc9
line 176why, one step back176the tuple. For this you can directly specify a tuple as `type`:Convert options docs to md. (#2924)Rowlando13before adoption937c68d
lines 177–194why, one step back177178```{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```194revise 'Options' documentation (#3192)Rowlando13before adoption81efdc9
line 195why, one step back195By using a tuple literal as the `type`, `nargs` gets automatically set to theConvert options docs to md. (#2924)Rowlando13before adoption937c68d
lines 196–212why, one step back196length of the tuple and the {class}`click.Tuple` type is automatically197used. 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 Options212Apply format guidelines to commits since latest 8.3.2 release (#3343)Rowlando13before adoptionf1f191e
lines 213–216why, one step back213The multiple options format allows options to take an arbitrary number of214arguments (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 back217218```{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 back224 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 back227228.. click:run::229230 invoke(commit, args=['-m', 'foo', '-m', 'bar', '-m', 'here'])231```232233## Counting234Apply format guidelines to commits since latest 8.3.2 release (#3343)Rowlando13before adoptionf1f191e
lines 235–237why, one step back235To count the occurrence of an option, set `count=True`. If the option is not236passed 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 back238239```{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## Boolean256Apply format guidelines to commits since latest 8.3.2 release (#3343)Rowlando13before adoptionf1f191e
lines 257–259why, one step back257Boolean options (boolean flags) take the values `True` or `False`. The simplest258case 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 back260261```{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```281Apply format guidelines to commits since latest 8.3.2 release (#3343)Rowlando13before adoptionf1f191e
lines 282–283why, one step back282To implement this more explicitly, declare `--{on-option}/--{off-option}`. Click283will automatically set `is_flag=True`.Convert options docs to md. (#2924)Rowlando13before adoption937c68d
lines 284–303why, one step back284285```{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 back304305Use cases for this more explicit pattern include:306Apply format guidelines to commits since latest 8.3.2 release (#3343)Rowlando13before adoptionf1f191e
lines 307–308why, one step back307* The default can be dynamic so the user can explicitly specify the option with308 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 back309* Shell scripts sometimes want to be explicit even when it's the defaultApply format guidelines to commits since latest 8.3.2 release (#3343)Rowlando13before adoptionf1f191e
lines 310–311why, one step back310* Shell aliases can set a flag, then an invocation can add a negation of the311 flagConvert options docs to md. (#2924)Rowlando13before adoption937c68d
line 312why, one step back312Apply format guidelines to commits since latest 8.3.2 release (#3343)Rowlando13before adoptionf1f191e
lines 313–315why, one step back313If a forward slash(`/`) is contained in your option name already, you can split314the parameters using `;`. In Windows `/` is commonly used as the prefix315character.Convert options docs to md. (#2924)Rowlando13before adoption937c68d
lines 316–325why, one step back316317```{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```325Fix version added and version changed in options docs. (#2957)Rowlando13before adoption497d1b6
lines 326–327why, one step back326```{versionchanged} 6.0327```Convert options docs to md. (#2924)Rowlando13before adoption937c68d
line 328why, one step back328Apply format guidelines to commits since latest 8.3.2 release (#3343)Rowlando13before adoptionf1f191e
lines 329–330why, one step back329If you want to define an alias for the second option only, then you will need to330use leading whitespace to disambiguate the format string.Convert options docs to md. (#2924)Rowlando13before adoption937c68d
lines 331–351why, one step back331332```{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 Value351Apply format guidelines to commits since latest 8.3.2 release (#3343)Rowlando13before adoptionf1f191e
lines 352–354why, one step back352To have a flag pass a value to the decorated function set `flag_value`. This353automatically 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 back355356```{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```374Reconcile default value passing and default activation (#3239)Rowlando13before adoption8c95c73
line 375why, one step back375### How `default` and `flag_value` interactMerge branch 'main' into stableDavid Lordbefore adoption81a482f
line 376why, one step back376Reconcile default value passing and default activation (#3239)Rowlando13before adoption8c95c73
lines 377–379why, one step back377The `default` value is given to the underlying function378as-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 back380Reconcile default value passing and default activation (#3239)Rowlando13before adoption8c95c73
lines 381–385why, one step back381But there is a special case for **non-boolean** flags: if a382flag 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 back386Reconcile default value passing and default activation (#3239)Rowlando13before adoption8c95c73
lines 387–388why, one step back387Which means, in the example above, this option:388Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 389–398why, one step back389```python390@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```398Reconcile default value passing and default activation (#3239)Rowlando13before adoption8c95c73
lines 399–437why, one step back399Because the two are equivalent, it is recommended to always400use 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 back438````Reconcile default value passing and default activation (#3239)Rowlando13before adoption8c95c73
lines 439–480why, one step back439440#### 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 back481Convert options docs to md. (#2924)Rowlando13before adoption937c68d
lines 482–483why, one step back482## Values from Environment Variables483revise 'Options' documentation (#3192)Rowlando13before adoption81efdc9
line 484why, one step back484To pass in a value from a specific environment variable use `envvar`.Convert options docs to md. (#2924)Rowlando13before adoption937c68d
lines 485–514why, one step back485486```{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```514Fix reconciliation of envvar with `default`, `flag_value` and `type` parameters for flag options (#2956)Kevin Deldyckebefore adoption565f36d
lines 515–516why, one step back515Variable 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 back517 - Not stripped of whitespace and should match the exact name provided to the518 `envvar` argument.Fix reconciliation of envvar with `default`, `flag_value` and `type` parameters for flag options (#2956)Kevin Deldyckebefore adoption565f36d
line 519why, one step back519Apply format guidelines to commits since latest 8.3.2 release (#3343)Rowlando13before adoptionf1f191e
lines 520–522why, one step back520For flag options, there are two concepts to consider: the activation of the flag521driven 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 back523Apply format guidelines to commits since latest 8.3.2 release (#3343)Rowlando13before adoptionf1f191e
lines 524–526why, one step back524The values read from environment variables are always strings and will require525extra 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 back527528Here are the rules used to parse environment variable values for flag options:529 - `true`, `1`, `yes`, `on`, `t`, `y` are interpreted as activating the flagApply format guidelines to commits since latest 8.3.2 release (#3343)Rowlando13before adoptionf1f191e
lines 530–533why, one step back530 - `false`, `0`, `no`, `off`, `f`, `n` are interpreted as deactivating the531 flag532 - The presence of the environment variable without value is interpreted as533 deactivating the flagFix reconciliation of envvar with `default`, `flag_value` and `type` parameters for flag options (#2956)Kevin Deldyckebefore adoption565f36d
line 534why, one step back534 - Empty strings are interpreted as deactivating the flagApply format guidelines to commits since latest 8.3.2 release (#3343)Rowlando13before adoptionf1f191e
lines 535–542why, one step back535 - Values are case-insensitive, so the `True`, `TRUE`, `tRuE` strings are all536 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 aboveFix reconciliation of envvar with `default`, `flag_value` and `type` parameters for flag options (#2956)Kevin Deldyckebefore adoption565f36d
lines 543–544why, one step back543 - Any other value is interpreted as deactivating the flag544Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 545–546why, one step back545```{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 back547Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 548–549why, one step back548So 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 back550Apply format guidelines to commits since latest 8.3.2 release (#3343)Rowlando13before adoptionf1f191e
lines 551–552why, one step back551If the flag is activated, its value is set to `flag_value`. Otherwise, the value552defaults to `None`.Fix reconciliation of envvar with `default`, `flag_value` and `type` parameters for flag options (#2956)Kevin Deldyckebefore adoption565f36d
line 553why, one step back553Convert options docs to md. (#2924)Rowlando13before adoption937c68d
lines 554–556why, one step back554## Multiple Options from Environment Values555556As options can accept multiple values, pulling in such values fromApply format guidelines to commits since latest 8.3.2 release (#3343)Rowlando13before adoptionf1f191e
lines 557–559why, one step back557environment variables (which are strings) is a bit more complex. Click handles558this by deferring customization of the behavior to the `type`. For both559`multiple` and `nargs` with values other thanConvert options docs to md. (#2924)Rowlando13before adoption937c68d
lines 560–589why, one step back560`1`, Click will invoke the {meth}`ParamType.split_envvar_value` method to561perform 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 Characters589Apply format guidelines to commits since latest 8.3.2 release (#3343)Rowlando13before adoptionf1f191e
lines 590–592why, one step back590Click can deal with prefix characters besides `-` for options, including `/` and591`+`, 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 back593594```{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```607Apply format guidelines to commits since latest 8.3.2 release (#3343)Rowlando13before adoptionf1f191e
lines 608–609why, one step back608There are special considerations for using `/` as prefix character. See609{ref}`option-boolean-flag` for more.Convert options docs to md. (#2924)Rowlando13before adoption937c68d
lines 610–619why, one step back610611(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 optionFixed the description of `is_flag=False, flag_value=value` (#2938)Rowlando13before adoptionbf7e691
lines 620–621why, one step back620can still be passed a value, but if only the flag is given, the621value will be `flag_value`.Convert options docs to md. (#2924)Rowlando13before adoption937c68d
lines 622–636why, one step back622623```{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```