Skip to content
Myr

parameter-types.md

This file

Path

docs/parameter-types.md

186 lines · 5.6 KiB · Markdown

Last changed by

Add newline EOFdenuowebbefore adoptionbd9d4f8

History of this file

Take it

Move Parameter-types doc from reST to MySTdenuowebbefore adoptiond502a68

line 1why, one step back
1(parameter-types)=

rename extension from .rst to .mddenuowebbefore adoption868230d

line 2why, one step back
2

Move Parameter-types doc from reST to MySTdenuowebbefore adoptiond502a68

line 3why, one step back
3# Parameter Types

rename extension from .rst to .mddenuowebbefore adoption868230d

line 4why, one step back
4

Move Parameter-types doc from reST to MySTdenuowebbefore adoptiond502a68

lines 5–6why, one step back
5```{currentmodule} click
6```

rename extension from .rst to .mddenuowebbefore adoption868230d

line 7why, one step back
7

Move Parameter-types doc from reST to MySTdenuowebbefore adoptiond502a68

lines 8–9why, one step back
8When the parameter type is set using `type`, Click will leverage the type to make your life easier, for example adding
9data 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 back
10

Move Parameter-types doc from reST to MySTdenuowebbefore adoptiond502a68

lines 11–16why, one step back
11```{contents}
12---13depth: 214local: true15---16```

rename extension from .rst to .mddenuowebbefore adoption868230d

line 17why, one step back
17

Move Parameter-types doc from reST to MySTdenuowebbefore adoptiond502a68

line 18why, one step back
18## Built-in Types Examples

rename extension from .rst to .mddenuowebbefore adoption868230d

line 19why, one step back
19

Move Parameter-types doc from reST to MySTdenuowebbefore adoptiond502a68

line 20why, one step back
20(choice-opts)=

rename extension from .rst to .mddenuowebbefore adoption868230d

line 21why, one step back
21

Move Parameter-types doc from reST to MySTdenuowebbefore adoptiond502a68

line 22why, one step back
22### Choice

rename extension from .rst to .mddenuowebbefore adoption868230d

line 23why, one step back
23

Move Parameter-types doc from reST to MySTdenuowebbefore adoptiond502a68

lines 24–27why, one step back
24Sometimes, 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 back
28

Move Parameter-types doc from reST to MySTdenuowebbefore adoptiond502a68

line 29why, one step back
29

rename extension from .rst to .mddenuowebbefore adoption868230d

lines 30–31why, one step back
30Example:
31

Move Parameter-types doc from reST to MySTdenuowebbefore adoptiond502a68

line 32why, one step back
32```{eval-rst}

rename extension from .rst to .mddenuowebbefore adoption868230d

lines 33–57why, one step back
33.. 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 back
58```

rename extension from .rst to .mddenuowebbefore adoption868230d

line 59why, one step back
59

Move Parameter-types doc from reST to MySTdenuowebbefore adoptiond502a68

line 60why, one step back
60Any iterable may be passed to {class}`Choice`. If an `Enum` is passed, the names of the enum members will be used as

rename extension from .rst to .mddenuowebbefore adoption868230d

lines 61–62why, one step back
61valid choices.
62

Move Parameter-types doc from reST to MySTdenuowebbefore adoptiond502a68

lines 63–64why, one step back
63Choices work with options that have `multiple=True`. If a `default` value is given with `multiple=True`, it should be a
64list or tuple of valid choices.

rename extension from .rst to .mddenuowebbefore adoption868230d

line 65why, one step back
65

Move Parameter-types doc from reST to MySTdenuowebbefore adoptiond502a68

line 66why, one step back
66Choices should be unique after normalization, see {meth}`Choice.normalize_choice` for more info.

rename extension from .rst to .mddenuowebbefore adoption868230d

line 67why, one step back
67

Move Parameter-types doc from reST to MySTdenuowebbefore adoptiond502a68

lines 68–71why, one step back
68```{versionchanged} 7.1
69The 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 back
72

Move Parameter-types doc from reST to MySTdenuowebbefore adoptiond502a68

line 73why, one step back
73(ranges)=

rename extension from .rst to .mddenuowebbefore adoption868230d

line 74why, one step back
74

Move Parameter-types doc from reST to MySTdenuowebbefore adoptiond502a68

line 75why, one step back
75### Int and Float Ranges

rename extension from .rst to .mddenuowebbefore adoption868230d

line 76why, one step back
76

Move Parameter-types doc from reST to MySTdenuowebbefore adoptiond502a68

lines 77–78why, one step back
77The {class}`IntRange` type extends the {data}`INT` type to ensure the value is contained in the given range. The
78{class}`FloatRange` type does the same for {data}`FLOAT`.

rename extension from .rst to .mddenuowebbefore adoption868230d

line 79why, one step back
79

Move Parameter-types doc from reST to MySTdenuowebbefore adoptiond502a68

lines 80–82why, one step back
80If `min` or `max` is omitted, that side is *unbounded*. Any value in that direction is accepted. By default, both bounds
81are *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 back
83

Move Parameter-types doc from reST to MySTdenuowebbefore adoptiond502a68

lines 84–88why, one step back
84If `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 back
89.. 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 back
101```

rename extension from .rst to .mddenuowebbefore adoption868230d

line 102why, one step back
102

Move Parameter-types doc from reST to MySTdenuowebbefore adoptiond502a68

line 103why, one step back
103## Built-in Types Listing

rename extension from .rst to .mddenuowebbefore adoption868230d

line 104why, one step back
104

Move Parameter-types doc from reST to MySTdenuowebbefore adoptiond502a68

line 105why, one step back
105The supported parameter {ref}`click-api-types` are

rename extension from .rst to .mddenuowebbefore adoption868230d

line 106why, one step back
106

Move Parameter-types doc from reST to MySTdenuowebbefore adoptiond502a68

line 107why, one step back
107- `str` / {data}`click.STRING`: The default parameter type which indicates unicode strings.

rename extension from .rst to .mddenuowebbefore adoption868230d

line 108why, one step back
108

Move Parameter-types doc from reST to MySTdenuowebbefore adoptiond502a68

line 109why, one step back
109- `int` / {data}`click.INT`: A parameter that only accepts integers.

rename extension from .rst to .mddenuowebbefore adoption868230d

line 110why, one step back
110

Move Parameter-types doc from reST to MySTdenuowebbefore adoptiond502a68

line 111why, one step back
111- `float` / {data}`click.FLOAT`: A parameter that only accepts floating point values.

rename extension from .rst to .mddenuowebbefore adoption868230d

line 112why, one step back
112

Move Parameter-types doc from reST to MySTdenuowebbefore adoptiond502a68

lines 113–115why, one step back
113- `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 back
116

Move Parameter-types doc from reST to MySTdenuowebbefore adoptiond502a68

lines 117–118why, one step back
117- {data}`click.UUID`: A parameter that accepts UUID values. This is not automatically guessed but represented as
118 {class}`uuid.UUID`.

rename extension from .rst to .mddenuowebbefore adoption868230d

line 119why, one step back
119

Move Parameter-types doc from reST to MySTdenuowebbefore adoptiond502a68

line 120why, one step back
120```{eval-rst}

rename extension from .rst to .mddenuowebbefore adoption868230d

lines 121–122why, one step back
121* .. autoclass:: Choice
122 :noindex:

Move Parameter-types doc from reST to MySTdenuowebbefore adoptiond502a68

line 123why, one step back
123```

rename extension from .rst to .mddenuowebbefore adoption868230d

line 124why, one step back
124

Move Parameter-types doc from reST to MySTdenuowebbefore adoptiond502a68

line 125why, one step back
125```{eval-rst}

rename extension from .rst to .mddenuowebbefore adoption868230d

lines 126–127why, one step back
126* .. autoclass:: DateTime
127 :noindex:

Move Parameter-types doc from reST to MySTdenuowebbefore adoptiond502a68

line 128why, one step back
128```

rename extension from .rst to .mddenuowebbefore adoption868230d

line 129why, one step back
129

Move Parameter-types doc from reST to MySTdenuowebbefore adoptiond502a68

line 130why, one step back
130```{eval-rst}

rename extension from .rst to .mddenuowebbefore adoption868230d

lines 131–132why, one step back
131* .. autoclass:: File
132 :noindex:

Move Parameter-types doc from reST to MySTdenuowebbefore adoptiond502a68

line 133why, one step back
133```

rename extension from .rst to .mddenuowebbefore adoption868230d

line 134why, one step back
134

Move Parameter-types doc from reST to MySTdenuowebbefore adoptiond502a68

line 135why, one step back
135```{eval-rst}

rename extension from .rst to .mddenuowebbefore adoption868230d

lines 136–137why, one step back
136* .. autoclass:: FloatRange
137 :noindex:

Move Parameter-types doc from reST to MySTdenuowebbefore adoptiond502a68

line 138why, one step back
138```

rename extension from .rst to .mddenuowebbefore adoption868230d

line 139why, one step back
139

Move Parameter-types doc from reST to MySTdenuowebbefore adoptiond502a68

line 140why, one step back
140```{eval-rst}

rename extension from .rst to .mddenuowebbefore adoption868230d

lines 141–142why, one step back
141* .. autoclass:: IntRange
142 :noindex:

Move Parameter-types doc from reST to MySTdenuowebbefore adoptiond502a68

line 143why, one step back
143```

rename extension from .rst to .mddenuowebbefore adoption868230d

line 144why, one step back
144

Move Parameter-types doc from reST to MySTdenuowebbefore adoptiond502a68

line 145why, one step back
145```{eval-rst}

rename extension from .rst to .mddenuowebbefore adoption868230d

lines 146–147why, one step back
146* .. autoclass:: Path
147 :noindex:

Move Parameter-types doc from reST to MySTdenuowebbefore adoptiond502a68

line 148why, one step back
148```

rename extension from .rst to .mddenuowebbefore adoption868230d

line 149why, one step back
149

Move Parameter-types doc from reST to MySTdenuowebbefore adoptiond502a68

line 150why, one step back
150## How to Implement Custom Types

rename extension from .rst to .mddenuowebbefore adoption868230d

line 151why, one step back
151

Move Parameter-types doc from reST to MySTdenuowebbefore adoptiond502a68

lines 152–154why, one step back
152To implement a custom type, you need to subclass the {class}`ParamType` class. For simple cases, passing a Python
153function 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 back
155

Move Parameter-types doc from reST to MySTdenuowebbefore adoptiond502a68

lines 156–157why, one step back
156The following code implements an integer type that accepts hex and octal numbers in addition to normal integers, and
157converts them into regular integers.

rename extension from .rst to .mddenuowebbefore adoption868230d

line 158why, one step back
158

Move Parameter-types doc from reST to MySTdenuowebbefore adoptiond502a68

lines 159–160why, one step back
159```python
160import click

rename extension from .rst to .mddenuowebbefore adoption868230d

line 161why, one step back
161

Move Parameter-types doc from reST to MySTdenuowebbefore adoptiond502a68

lines 162–163why, one step back
162class BasedIntParamType(click.ParamType):
163 name = "integer"

rename extension from .rst to .mddenuowebbefore adoption868230d

line 164why, one step back
164

Move Parameter-types doc from reST to MySTdenuowebbefore adoptiond502a68

lines 165–167why, one step back
165 def convert(self, value, param, ctx):
166 if isinstance(value, int):167 return value

rename extension from .rst to .mddenuowebbefore adoption868230d

line 168why, one step back
168

Move Parameter-types doc from reST to MySTdenuowebbefore adoptiond502a68

lines 169–176why, one step back
169 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 back
177

Move Parameter-types doc from reST to MySTdenuowebbefore adoptiond502a68

lines 178–179why, one step back
178BASED_INT = BasedIntParamType()
179```

rename extension from .rst to .mddenuowebbefore adoption868230d

line 180why, one step back
180

Move Parameter-types doc from reST to MySTdenuowebbefore adoptiond502a68

lines 181–182why, one step back
181The {attr}`~ParamType.name` attribute is optional and is used for documentation. Call {meth}`~ParamType.fail` if
182conversion 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 back
183

Move Parameter-types doc from reST to MySTdenuowebbefore adoptiond502a68

lines 184–186why, one step back
184Values from user input or the command line will be strings, but default values and Python arguments may already be the
185correct type. The custom type should check at the top if the value is already valid and pass it through to support those186cases.