parameter-types.md
This file
Path
docs/parameter-types.md
186 lines · 5.6 KiB · Markdown
Last changed by
[pre-commit.ci lite] apply automatic fixespre-commit-ci-lite[bot]before adoptione19772d
History of this fileTake it
Move Parameter-types doc from reST to MySTdenuowebbefore adoptiond502a68
line 1why, one step back1(parameter-types)=rename extension from .rst to .mddenuowebbefore adoption868230d
line 2why, one step back2Move Parameter-types doc from reST to MySTdenuowebbefore adoptiond502a68
line 3why, one step back3# Parameter Typesrename extension from .rst to .mddenuowebbefore adoption868230d
line 4why, one step back4Move Parameter-types doc from reST to MySTdenuowebbefore adoptiond502a68
lines 5–6why, one step back5```{currentmodule} click6```rename extension from .rst to .mddenuowebbefore adoption868230d
line 7why, one step back7Move Parameter-types doc from reST to MySTdenuowebbefore adoptiond502a68
lines 8–9why, one step back8When the parameter type is set using `type`, Click will leverage the type to make your life easier, for example adding9data to your help pages. Most examples are done with options, but types are available to options and arguments.rename extension from .rst to .mddenuowebbefore adoption868230d
line 10why, one step back10Move Parameter-types doc from reST to MySTdenuowebbefore adoptiond502a68
lines 11–16why, one step back11```{contents}12---13depth: 214local: true15---16```rename extension from .rst to .mddenuowebbefore adoption868230d
line 17why, one step back17Move Parameter-types doc from reST to MySTdenuowebbefore adoptiond502a68
line 18why, one step back18## Built-in Types Examplesrename extension from .rst to .mddenuowebbefore adoption868230d
line 19why, one step back19Move Parameter-types doc from reST to MySTdenuowebbefore adoptiond502a68
line 20why, one step back20(choice-opts)=rename extension from .rst to .mddenuowebbefore adoption868230d
line 21why, one step back21Move Parameter-types doc from reST to MySTdenuowebbefore adoptiond502a68
line 22why, one step back22### Choicerename extension from .rst to .mddenuowebbefore adoption868230d
line 23why, one step back23Move Parameter-types doc from reST to MySTdenuowebbefore adoptiond502a68
lines 24–27why, one step back24Sometimes, you want to have a parameter be a choice of a list of values. In that case you can use {class}`Choice` type.25It can be instantiated with a list of valid values. The originally passed choice will be returned, not the str passed on26the command line. Token normalization functions and `case_sensitive=False` can cause the two to be different but still27match. {meth}`Choice.normalize_choice` for more info.rename extension from .rst to .mddenuowebbefore adoption868230d
line 28why, one step back28Move Parameter-types doc from reST to MySTdenuowebbefore adoptiond502a68
line 29why, one step back29rename extension from .rst to .mddenuowebbefore adoption868230d
lines 30–31why, one step back30Example:31Move Parameter-types doc from reST to MySTdenuowebbefore adoptiond502a68
line 32why, one step back32```{eval-rst}rename extension from .rst to .mddenuowebbefore adoption868230d
lines 33–57why, one step back33.. click:example::3435 import enum3637 class HashType(enum.Enum):38 MD5 = enum.auto()39 SHA1 = enum.auto()4041 @click.command()42 @click.option('--hash-type',43 type=click.Choice(HashType, case_sensitive=False))44 def digest(hash_type: HashType):45 click.echo(hash_type)4647What it looks like:4849.. click:run::5051 invoke(digest, args=['--hash-type=MD5'])52 println()53 invoke(digest, args=['--hash-type=md5'])54 println()55 invoke(digest, args=['--hash-type=foo'])56 println()57 invoke(digest, args=['--help'])Move Parameter-types doc from reST to MySTdenuowebbefore adoptiond502a68
line 58why, one step back58```rename extension from .rst to .mddenuowebbefore adoption868230d
line 59why, one step back59Move Parameter-types doc from reST to MySTdenuowebbefore adoptiond502a68
line 60why, one step back60Any iterable may be passed to {class}`Choice`. If an `Enum` is passed, the names of the enum members will be used asrename extension from .rst to .mddenuowebbefore adoption868230d
lines 61–62why, one step back61valid choices.62Move Parameter-types doc from reST to MySTdenuowebbefore adoptiond502a68
lines 63–64why, one step back63Choices work with options that have `multiple=True`. If a `default` value is given with `multiple=True`, it should be a64list or tuple of valid choices.rename extension from .rst to .mddenuowebbefore adoption868230d
line 65why, one step back65Move Parameter-types doc from reST to MySTdenuowebbefore adoptiond502a68
line 66why, one step back66Choices should be unique after normalization, see {meth}`Choice.normalize_choice` for more info.rename extension from .rst to .mddenuowebbefore adoption868230d
line 67why, one step back67Move Parameter-types doc from reST to MySTdenuowebbefore adoptiond502a68
lines 68–71why, one step back68```{versionchanged} 7.169The resulting value from an option will always be one of the originally passed choices70regardless of `case_sensitive`.71```rename extension from .rst to .mddenuowebbefore adoption868230d
line 72why, one step back72Move Parameter-types doc from reST to MySTdenuowebbefore adoptiond502a68
line 73why, one step back73(ranges)=rename extension from .rst to .mddenuowebbefore adoption868230d
line 74why, one step back74Move Parameter-types doc from reST to MySTdenuowebbefore adoptiond502a68
line 75why, one step back75### Int and Float Rangesrename extension from .rst to .mddenuowebbefore adoption868230d
line 76why, one step back76Move Parameter-types doc from reST to MySTdenuowebbefore adoptiond502a68
lines 77–78why, one step back77The {class}`IntRange` type extends the {data}`INT` type to ensure the value is contained in the given range. The78{class}`FloatRange` type does the same for {data}`FLOAT`.rename extension from .rst to .mddenuowebbefore adoption868230d
line 79why, one step back79Move Parameter-types doc from reST to MySTdenuowebbefore adoptiond502a68
lines 80–82why, one step back80If `min` or `max` is omitted, that side is *unbounded*. Any value in that direction is accepted. By default, both bounds81are *closed*, which means the boundary value is included in the accepted range. `min_open` and `max_open` can be used to82exclude that boundary from the range.rename extension from .rst to .mddenuowebbefore adoption868230d
line 83why, one step back83Move Parameter-types doc from reST to MySTdenuowebbefore adoptiond502a68
lines 84–88why, one step back84If `clamp` mode is enabled, a value that is outside the range is set to the boundary instead of failing. For example,85the range `0, 5` would return `5` for the value `10`, or `0` for the value `-1`. When using {class}`FloatRange`, `clamp`86can only be enabled if both bounds are *closed* (the default).8788```{eval-rst}rename extension from .rst to .mddenuowebbefore adoption868230d
lines 89–100why, one step back89.. click:example::9091 @click.command()92 @click.option("--count", type=click.IntRange(0, 20, clamp=True))93 @click.option("--digit", type=click.IntRange(0, 9))94 def repeat(count, digit):95 click.echo(str(digit) * count)9697.. click:run::9899 invoke(repeat, args=['--count=100', '--digit=5'])100 invoke(repeat, args=['--count=6', '--digit=12'])Move Parameter-types doc from reST to MySTdenuowebbefore adoptiond502a68
line 101why, one step back101```rename extension from .rst to .mddenuowebbefore adoption868230d
line 102why, one step back102Move Parameter-types doc from reST to MySTdenuowebbefore adoptiond502a68
line 103why, one step back103## Built-in Types Listingrename extension from .rst to .mddenuowebbefore adoption868230d
line 104why, one step back104Move Parameter-types doc from reST to MySTdenuowebbefore adoptiond502a68
line 105why, one step back105The supported parameter {ref}`click-api-types` arerename extension from .rst to .mddenuowebbefore adoption868230d
line 106why, one step back106Move Parameter-types doc from reST to MySTdenuowebbefore adoptiond502a68
line 107why, one step back107- `str` / {data}`click.STRING`: The default parameter type which indicates unicode strings.rename extension from .rst to .mddenuowebbefore adoption868230d
line 108why, one step back108Move Parameter-types doc from reST to MySTdenuowebbefore adoptiond502a68
line 109why, one step back109- `int` / {data}`click.INT`: A parameter that only accepts integers.rename extension from .rst to .mddenuowebbefore adoption868230d
line 110why, one step back110Move Parameter-types doc from reST to MySTdenuowebbefore adoptiond502a68
line 111why, one step back111- `float` / {data}`click.FLOAT`: A parameter that only accepts floating point values.rename extension from .rst to .mddenuowebbefore adoption868230d
line 112why, one step back112Move Parameter-types doc from reST to MySTdenuowebbefore adoptiond502a68
lines 113–115why, one step back113- `bool` / {data}`click.BOOL`: A parameter that accepts boolean values. This is automatically used for boolean flags.114 The string values "1", "true", "t", "yes", "y", and "on" convert to `True`. "0", "false", "f", "no", "n", and "off"115 convert to `False`.rename extension from .rst to .mddenuowebbefore adoption868230d
line 116why, one step back116Move Parameter-types doc from reST to MySTdenuowebbefore adoptiond502a68
lines 117–118why, one step back117- {data}`click.UUID`: A parameter that accepts UUID values. This is not automatically guessed but represented as118 {class}`uuid.UUID`.rename extension from .rst to .mddenuowebbefore adoption868230d
line 119why, one step back119Move Parameter-types doc from reST to MySTdenuowebbefore adoptiond502a68
line 120why, one step back120```{eval-rst}rename extension from .rst to .mddenuowebbefore adoption868230d
lines 121–122why, one step back121* .. autoclass:: Choice122 :noindex:Move Parameter-types doc from reST to MySTdenuowebbefore adoptiond502a68
line 123why, one step back123```rename extension from .rst to .mddenuowebbefore adoption868230d
line 124why, one step back124Move Parameter-types doc from reST to MySTdenuowebbefore adoptiond502a68
line 125why, one step back125```{eval-rst}rename extension from .rst to .mddenuowebbefore adoption868230d
lines 126–127why, one step back126* .. autoclass:: DateTime127 :noindex:Move Parameter-types doc from reST to MySTdenuowebbefore adoptiond502a68
line 128why, one step back128```rename extension from .rst to .mddenuowebbefore adoption868230d
line 129why, one step back129Move Parameter-types doc from reST to MySTdenuowebbefore adoptiond502a68
line 130why, one step back130```{eval-rst}rename extension from .rst to .mddenuowebbefore adoption868230d
lines 131–132why, one step back131* .. autoclass:: File132 :noindex:Move Parameter-types doc from reST to MySTdenuowebbefore adoptiond502a68
line 133why, one step back133```rename extension from .rst to .mddenuowebbefore adoption868230d
line 134why, one step back134Move Parameter-types doc from reST to MySTdenuowebbefore adoptiond502a68
line 135why, one step back135```{eval-rst}rename extension from .rst to .mddenuowebbefore adoption868230d
lines 136–137why, one step back136* .. autoclass:: FloatRange137 :noindex:Move Parameter-types doc from reST to MySTdenuowebbefore adoptiond502a68
line 138why, one step back138```rename extension from .rst to .mddenuowebbefore adoption868230d
line 139why, one step back139Move Parameter-types doc from reST to MySTdenuowebbefore adoptiond502a68
line 140why, one step back140```{eval-rst}rename extension from .rst to .mddenuowebbefore adoption868230d
lines 141–142why, one step back141* .. autoclass:: IntRange142 :noindex:Move Parameter-types doc from reST to MySTdenuowebbefore adoptiond502a68
line 143why, one step back143```rename extension from .rst to .mddenuowebbefore adoption868230d
line 144why, one step back144Move Parameter-types doc from reST to MySTdenuowebbefore adoptiond502a68
line 145why, one step back145```{eval-rst}rename extension from .rst to .mddenuowebbefore adoption868230d
lines 146–147why, one step back146* .. autoclass:: Path147 :noindex:Move Parameter-types doc from reST to MySTdenuowebbefore adoptiond502a68
line 148why, one step back148```rename extension from .rst to .mddenuowebbefore adoption868230d
line 149why, one step back149Move Parameter-types doc from reST to MySTdenuowebbefore adoptiond502a68
line 150why, one step back150## How to Implement Custom Typesrename extension from .rst to .mddenuowebbefore adoption868230d
line 151why, one step back151Move Parameter-types doc from reST to MySTdenuowebbefore adoptiond502a68
lines 152–154why, one step back152To implement a custom type, you need to subclass the {class}`ParamType` class. For simple cases, passing a Python153function that fails with a `ValueError` is also supported, though discouraged. Override the {meth}`~ParamType.convert`154method to convert the value from a string to the correct type.rename extension from .rst to .mddenuowebbefore adoption868230d
line 155why, one step back155Move Parameter-types doc from reST to MySTdenuowebbefore adoptiond502a68
lines 156–157why, one step back156The following code implements an integer type that accepts hex and octal numbers in addition to normal integers, and157converts them into regular integers.rename extension from .rst to .mddenuowebbefore adoption868230d
line 158why, one step back158Move Parameter-types doc from reST to MySTdenuowebbefore adoptiond502a68
lines 159–160why, one step back159```python160import clickrename extension from .rst to .mddenuowebbefore adoption868230d
line 161why, one step back161Move Parameter-types doc from reST to MySTdenuowebbefore adoptiond502a68
lines 162–163why, one step back162class BasedIntParamType(click.ParamType):163 name = "integer"rename extension from .rst to .mddenuowebbefore adoption868230d
line 164why, one step back164Move Parameter-types doc from reST to MySTdenuowebbefore adoptiond502a68
lines 165–167why, one step back165 def convert(self, value, param, ctx):166 if isinstance(value, int):167 return valuerename extension from .rst to .mddenuowebbefore adoption868230d
line 168why, one step back168Move Parameter-types doc from reST to MySTdenuowebbefore adoptiond502a68
lines 169–176why, one step back169 try:170 if value[:2].lower() == "0x":171 return int(value[2:], 16)172 elif value[:1] == "0":173 return int(value, 8)174 return int(value, 10)175 except ValueError:176 self.fail(f"{value!r} is not a valid integer", param, ctx)rename extension from .rst to .mddenuowebbefore adoption868230d
line 177why, one step back177Move Parameter-types doc from reST to MySTdenuowebbefore adoptiond502a68
lines 178–179why, one step back178BASED_INT = BasedIntParamType()179```rename extension from .rst to .mddenuowebbefore adoption868230d
line 180why, one step back180Move Parameter-types doc from reST to MySTdenuowebbefore adoptiond502a68
lines 181–182why, one step back181The {attr}`~ParamType.name` attribute is optional and is used for documentation. Call {meth}`~ParamType.fail` if182conversion fails. The `param` and `ctx` arguments may be `None` in some cases such as prompts.rename extension from .rst to .mddenuowebbefore adoption868230d
line 183why, one step back183Move Parameter-types doc from reST to MySTdenuowebbefore adoptiond502a68
lines 184–186why, one step back184Values from user input or the command line will be strings, but default values and Python arguments may already be the185correct type. The custom type should check at the top if the value is already valid and pass it through to support those186cases.