Skip to content
Myr

test_options.py

This file

Path

tests/test_options.py

3,945 lines · 131.1 KiB · Python

Last changed by

landed by oscabriel2247b35

History of this file

Take it

The walk stopped after 200 commits: lines older than 9ce34f2 point at it.

Fix rendering of Enum choices' default (#3004)Rowlando13before adoptioneaf33a9

line 1why, one step back
1import enum

Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2

lines 2–3why, one step back
2import os
3import re

Fix rendering of Enum choices' default (#3004)Rowlando13before adoptioneaf33a9

line 4why, one step back
4import sys

Merge branch 'main' into stableDavid Lordbefore adoption81a482f

line 5why, one step back
5import tempfile

Deprecate parameter names that are: not a Python identifier, or a Python Keyword, or not lower-cased (#3866)Rowlando13before adoption06b2a67

lines 6–7why, one step back
6import unicodedata
7import warnings

Merge branch 'main' into stableDavid Lordbefore adoption81a482f

lines 8–10why, one step back
8from contextlib import nullcontext
9from typing import Literal10

Fix rendering of Enum choices' default (#3004)Rowlando13before adoptioneaf33a9

lines 11–12why, one step back
11if sys.version_info < (3, 11):
12 enum.StrEnum = enum.Enum # type: ignore[assignment]

Merge branch 'main' into stableDavid Lordbefore adoption81a482f

line 13why, one step back
13

Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2

line 14why, one step back
14import pytest

Merge branch 'main' into stableDavid Lordbefore adoption81a482f

line 15why, one step back
15

Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2

lines 16–17why, one step back
16import click
17from click import Option

Merge branch 'main' into stableDavid Lordbefore adoption81a482f

lines 18–19why, one step back
18from click import UNPROCESSED
19from click._utils import UNSET20from click.parser import _split_opt

Fix handling of options which set both is_flag=False and flag_value=... (#3152)Rowlando13before adoptionebcd548

line 21why, one step back
21from click.testing import CliRunner

Merge branch 'main' into stableDavid Lordbefore adoption81a482f

lines 22–23why, one step back
22
23

Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2

line 24why, one step back
24def test_prefixes(runner):

Merge branch 'main' into stableDavid Lordbefore adoption81a482f

line 25why, one step back
25 @click.command()

Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2

lines 26–29why, one step back
26 @click.option("++foo", is_flag=True, help="das foo")
27 @click.option("--bar", is_flag=True, help="das bar")28 def cli(foo, bar):29 click.echo(f"foo={foo} bar={bar}")

Merge branch 'main' into stableDavid Lordbefore adoption81a482f

line 30why, one step back
30

Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2

line 31why, one step back
31 result = runner.invoke(cli, ["++foo", "--bar"])

Merge branch 'main' into stableDavid Lordbefore adoption81a482f

line 32why, one step back
32 assert not result.exception

Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2

line 33why, one step back
33 assert result.output == "foo=True bar=True\n"

Merge branch 'main' into stableDavid Lordbefore adoption81a482f

lines 34–35why, one step back
34
35 result = runner.invoke(cli, ["--help"])

Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2

lines 36–37why, one step back
36 assert re.search(r"\+\+foo\s+das foo", result.output) is not None
37 assert re.search(r"--bar\s+das bar", result.output) is not None

Merge branch 'main' into stableDavid Lordbefore adoption81a482f

lines 38–39why, one step back
38
39

Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2

lines 40–42why, one step back
40def test_invalid_option(runner):
41 with pytest.raises(TypeError, match="name was passed") as exc_info:42 click.Option(["foo"])

Merge branch 'main' into stableDavid Lordbefore adoption81a482f

line 43why, one step back
43

Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2

lines 44–47why, one step back
44 message = str(exc_info.value)
45 assert "name was passed (foo)" in message46 assert "declare an argument" in message47 assert "'--foo'" in message

Merge branch 'main' into stableDavid Lordbefore adoption81a482f

lines 48–50why, one step back
48
4950@pytest.mark.parametrize("deprecated", [True, "USE B INSTEAD"])

Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2

line 51why, one step back
51def test_deprecated_usage(runner, deprecated):

Merge branch 'main' into stableDavid Lordbefore adoption81a482f

line 52why, one step back
52 @click.command()

Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2

line 53why, one step back
53 @click.option("--foo", default="bar", deprecated=deprecated)

Merge branch 'main' into stableDavid Lordbefore adoption81a482f

lines 54–57why, one step back
54 def cmd(foo):
55 click.echo(foo)5657 result = runner.invoke(cmd, ["--help"])

Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2

line 58why, one step back
58 assert "(DEPRECATED" in result.output

Merge branch 'main' into stableDavid Lordbefore adoption81a482f

lines 59–63why, one step back
59
60 if isinstance(deprecated, str):61 assert deprecated in result.output6263

Merge stable into main (#3521)Rowlando13before adoption1a994ad

lines 64–77why, one step back
64@pytest.mark.parametrize(
65 ("deprecated", "expected"),66 [(True, "(DEPRECATED)"), ("USE B INSTEAD", "(DEPRECATED: USE B INSTEAD)")],67)68@pytest.mark.parametrize("help_text", ["", None])69def test_deprecated_empty_help_no_leading_space(help_text, deprecated, expected):70 """An option with empty or missing help text must not gain a stray leading71 space before the deprecation label.72 """73 opt = click.Option(["--foo"], help=help_text, deprecated=deprecated)74 ctx = click.Context(click.Command("cli"))75 assert opt.get_help_record(ctx)[1] == expected7677

Stable (#3851)Rowlando13before adoption6aabf09

lines 78–101why, one step back
78@pytest.mark.parametrize(
79 ("param_decls", "kwargs", "expected"),80 [81 (["-v", "--verbose"], {"is_flag": True}, "-v, --verbose"),82 (["--config"], {}, "--config TEXT"),83 (["--color/--no-color"], {}, "--color / --no-color"),84 (["/debug;/no-debug"], {}, "/debug; /no-debug"),85 ],86)87@pytest.mark.parametrize("hidden", [False, True])88def test_help_spec(param_decls, kwargs, hidden, expected):89 """A hidden option still produces its spec via ``get_help_spec()``, unlike90 ``get_help_record()``.91 """92 opt = click.Option(param_decls, hidden=hidden, **kwargs)93 ctx = click.Context(click.Command("cli"))94 assert opt.get_help_spec(ctx) == expected9596 if hidden:97 assert opt.get_help_record(ctx) is None98 else:99 assert opt.get_help_record(ctx)[0] == expected100101102@pytest.mark.parametrize(103 ("param_decls", "expected_spec", "expected_name"),104 [105 (["-t", "--times"], "--times", "times"),106 (["--times", "-t"], "--times", "times"),107 (["-t"], "-t", "t"),108 (["-t", "--times", "--repeat"], "--times", "times"),109 (["--color/--no-color"], "--color", "color"),110 (["/debug;/no-debug"], "/debug", "debug"),111 ],112)113def test_spec(param_decls, expected_spec, expected_name):114 """The spec is the declaration the name was derived from, so the longest115 prefix wins over declaration order.116 """117 opt = click.Option(param_decls)118 assert opt.spec == expected_spec119 assert opt.name == expected_name120 assert _split_opt(opt.spec)[1].replace("-", "_").lower() == opt.name121122123def test_spec_with_explicit_name():124 """An explicit name declaration leaves the spec to the remaining flags."""125 opt = click.Option(["--times", "repeat_count"])126 assert opt.spec == "--times"127 assert opt.name == "repeat_count"128129

Merge branch 'main' into stableDavid Lordbefore adoption81a482f

line 130why, one step back
130@pytest.mark.parametrize("deprecated", [True, "USE B INSTEAD"])

Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2

line 131why, one step back
131def test_deprecated_warning(runner, deprecated):

Merge branch 'main' into stableDavid Lordbefore adoption81a482f

lines 132–133why, one step back
132 @click.command()
133 @click.option(

Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2

line 134why, one step back
134 "--my-option", required=False, deprecated=deprecated, default="default option"

Merge branch 'main' into stableDavid Lordbefore adoption81a482f

line 135why, one step back
135 )

Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2

lines 136–137why, one step back
136 def cli(my_option: str):
137 click.echo(f"{my_option}")

Merge branch 'main' into stableDavid Lordbefore adoption81a482f

line 138why, one step back
138

Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2

line 139why, one step back
139 # defaults should not give a deprecated warning

Merge branch 'main' into stableDavid Lordbefore adoption81a482f

lines 140–141why, one step back
140 result = runner.invoke(cli, [])
141 assert result.exit_code == 0, result.output

Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2

line 142why, one step back
142 assert "is deprecated" not in result.output

Merge branch 'main' into stableDavid Lordbefore adoption81a482f

line 143why, one step back
143

Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2

line 144why, one step back
144 result = runner.invoke(cli, ["--my-option", "hello"])

Merge branch 'main' into stableDavid Lordbefore adoption81a482f

line 145why, one step back
145 assert result.exit_code == 0, result.output
146 assert "option '--my-option' is deprecated" in result.output

Merge branch 'main' into stableDavid Lordbefore adoption81a482f

lines 147–151why, one step back
147
148 if isinstance(deprecated, str):149 assert deprecated in result.output150151

Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2

lines 152–154why, one step back
152def test_deprecated_required(runner):
153 with pytest.raises(ValueError, match="is deprecated and still required"):154 click.Option(["--a"], required=True, deprecated=True)

Merge branch 'main' into stableDavid Lordbefore adoption81a482f

lines 155–156why, one step back
155
156

Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2

lines 157–159why, one step back
157def test_deprecated_prompt(runner):
158 with pytest.raises(ValueError, match="`deprecated` options cannot use `prompt`"):159 click.Option(["--a"], prompt=True, deprecated=True)

Merge branch 'main' into stableDavid Lordbefore adoption81a482f

lines 160–161why, one step back
160
161

Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2

line 162why, one step back
162def test_invalid_nargs(runner):

Merge branch 'main' into stableDavid Lordbefore adoption81a482f

lines 163–165why, one step back
163 with pytest.raises(TypeError, match="nargs=-1 is not supported for options."):
164165 @click.command()

Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2

lines 166–168why, one step back
166 @click.option("--foo", nargs=-1)
167 def cli(foo):168 pass

Merge branch 'main' into stableDavid Lordbefore adoption81a482f

lines 169–170why, one step back
169
170

Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2

line 171why, one step back
171def test_nargs_tup_composite_mult(runner):

Merge branch 'main' into stableDavid Lordbefore adoption81a482f

line 172why, one step back
172 @click.command()

Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2

lines 173–176why, one step back
173 @click.option("--item", type=(str, int), multiple=True)
174 def copy(item):175 for name, id in item:176 click.echo(f"name={name} id={id:d}")

Merge branch 'main' into stableDavid Lordbefore adoption81a482f

line 177why, one step back
177

Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2

line 178why, one step back
178 result = runner.invoke(copy, ["--item", "peter", "1", "--item", "max", "2"])

Merge branch 'main' into stableDavid Lordbefore adoption81a482f

line 179why, one step back
179 assert not result.exception

Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2

line 180why, one step back
180 assert result.output.splitlines() == ["name=peter id=1", "name=max id=2"]

Merge branch 'main' into stableDavid Lordbefore adoption81a482f

lines 181–182why, one step back
181
182

Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2

line 183why, one step back
183def test_counting(runner):

Merge branch 'main' into stableDavid Lordbefore adoption81a482f

line 184why, one step back
184 @click.command()

Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2

lines 185–187why, one step back
185 @click.option("-v", count=True, help="Verbosity", type=click.IntRange(0, 3))
186 def cli(v):187 click.echo(f"verbosity={v:d}")

Merge branch 'main' into stableDavid Lordbefore adoption81a482f

line 188why, one step back
188

Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2

line 189why, one step back
189 result = runner.invoke(cli, ["-vvv"])

Merge branch 'main' into stableDavid Lordbefore adoption81a482f

line 190why, one step back
190 assert not result.exception

Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2

line 191why, one step back
191 assert result.output == "verbosity=3\n"

Merge branch 'main' into stableDavid Lordbefore adoption81a482f

line 192why, one step back
192

Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2

line 193why, one step back
193 result = runner.invoke(cli, ["-vvvv"])

Merge branch 'main' into stableDavid Lordbefore adoption81a482f

line 194why, one step back
194 assert result.exception

Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2

line 195why, one step back
195 assert "Invalid value for '-v': 4 is not in the range 0<=x<=3." in result.output

Merge branch 'main' into stableDavid Lordbefore adoption81a482f

lines 196–198why, one step back
196
197 result = runner.invoke(cli, [])198 assert not result.exception

Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2

line 199why, one step back
199 assert result.output == "verbosity=0\n"

Merge branch 'main' into stableDavid Lordbefore adoption81a482f

lines 200–201why, one step back
200
201 result = runner.invoke(cli, ["--help"])

Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2

line 202why, one step back
202 assert re.search(r"-v\s+Verbosity", result.output) is not None

Merge branch 'main' into stableDavid Lordbefore adoption81a482f

lines 203–204why, one step back
203
204

Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2

lines 205–206why, one step back
205@pytest.mark.parametrize("unknown_flag", ["--foo", "-f"])
206def test_unknown_options(runner, unknown_flag):

Merge branch 'main' into stableDavid Lordbefore adoption81a482f

lines 207–210why, one step back
207 @click.command()
208 def cli():209 pass210

Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2

line 211why, one step back
211 result = runner.invoke(cli, [unknown_flag])

Merge branch 'main' into stableDavid Lordbefore adoption81a482f

line 212why, one step back
212 assert result.exception

Add NoSuchCommand exception with suggestions for misspelled commands (#3228)RC2215before adoption831c8f0

line 213why, one step back
213 assert f"No such option '{unknown_flag}'." in result.output

Merge branch 'main' into stableDavid Lordbefore adoption81a482f

lines 214–216why, one step back
214
215216@pytest.mark.parametrize(

Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2

line 217why, one step back
217 ("value", "expect"),

Merge branch 'main' into stableDavid Lordbefore adoption81a482f

line 218why, one step back
218 [

Add NoSuchCommand exception with suggestions for misspelled commands (#3228)RC2215before adoption831c8f0

lines 219–221why, one step back
219 ("--cat", "Did you mean '--count'?"),
220 ("--bounds", "(Did you mean one of: '--bound', '--count'?)"),221 ("--bount", "(Did you mean one of: '--bound', '--count'?)"),

Merge branch 'main' into stableDavid Lordbefore adoption81a482f

lines 222–223why, one step back
222 ],
223)

Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2

line 224why, one step back
224def test_suggest_possible_options(runner, value, expect):

Merge branch 'main' into stableDavid Lordbefore adoption81a482f

line 225why, one step back
225 cli = click.Command(

Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2

line 226why, one step back
226 "cli", params=[click.Option(["--bound"]), click.Option(["--count"])]

Merge branch 'main' into stableDavid Lordbefore adoption81a482f

line 227why, one step back
227 )

Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2

lines 228–229why, one step back
228 result = runner.invoke(cli, [value])
229 assert expect in result.output

Merge branch 'main' into stableDavid Lordbefore adoption81a482f

lines 230–231why, one step back
230
231

Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2

line 232why, one step back
232def test_multiple_required(runner):

Merge branch 'main' into stableDavid Lordbefore adoption81a482f

line 233why, one step back
233 @click.command()

Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2

lines 234–236why, one step back
234 @click.option("-m", "--message", multiple=True, required=True)
235 def cli(message):236 click.echo("\n".join(message))

Merge branch 'main' into stableDavid Lordbefore adoption81a482f

line 237why, one step back
237

Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2

line 238why, one step back
238 result = runner.invoke(cli, ["-m", "foo", "-mbar"])

Merge branch 'main' into stableDavid Lordbefore adoption81a482f

line 239why, one step back
239 assert not result.exception

Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2

line 240why, one step back
240 assert result.output == "foo\nbar\n"

Merge branch 'main' into stableDavid Lordbefore adoption81a482f

lines 241–243why, one step back
241
242 result = runner.invoke(cli, [])243 assert result.exception

Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2

line 244why, one step back
244 assert "Error: Missing option '-m' / '--message'." in result.output

Merge branch 'main' into stableDavid Lordbefore adoption81a482f

lines 245–294why, one step back
245
246247@pytest.mark.parametrize(248 ("multiple", "nargs", "default", "expected"),249 [250 # If multiple values are allowed, defaults should be iterable.251 (True, 1, [], ()),252 (True, 1, (), ()),253 (True, 1, tuple(), ()),254 (True, 1, set(), ()),255 (True, 1, frozenset(), ()),256 (True, 1, {}, ()),257 # Special values.258 (True, 1, None, ()),259 (True, 1, UNSET, ()),260 # Number of values are kept as-is in the default.261 (True, 1, [1], (1,)),262 (True, 1, [1, 2], (1, 2)),263 (True, 1, [1, 2, 3], (1, 2, 3)),264 (True, 1, [1.1, 2.2], (1.1, 2.2)),265 (True, 1, ["1", "2"], ("1", "2")),266 (True, 1, [None, None], (None, None)),267 # Contrary to list or tuples, native Python types not supported by Click are268 # not recognized and are converted to the default format: tuple of strings.269 # Refs: https://github.com/pallets/click/issues/3036270 (True, 1, {1, 2}, ("1", "2")),271 (True, 1, frozenset([1, 2]), ("1", "2")),272 (True, 1, {1: "a", 2: "b"}, ("1", "2")),273 # Multiple values with nargs > 1.274 (True, 2, [], ()),275 (True, 2, (), ()),276 (True, 2, tuple(), ()),277 (True, 2, set(), ()),278 (True, 2, frozenset(), ()),279 (True, 2, {}, ()),280 (True, 2, None, ()),281 (True, 2, UNSET, ()),282 (True, 2, [[1, 2]], ((1, 2),)),283 (True, 2, [[1, 2], [3, 4]], ((1, 2), (3, 4))),284 (True, 2, [[1, 2], [3, 4], [5, 6]], ((1, 2), (3, 4), (5, 6))),285 (True, 2, [[1.1, 2.2], [3.3, 4.4]], ((1.1, 2.2), (3.3, 4.4))),286 (True, 2, [["1", "2"], ["3", "4"]], (("1", "2"), ("3", "4"))),287 (True, 2, [[None, None], [None, None]], ((None, None), (None, None))),288 (True, 2, [[1, 2.2], ["3", None]], ((1, 2.2), (3, None))),289 (True, 2, [[1, 2.2], None], ((1, 2.2), None)),290 # Default of the right length works for non-multiples.291 (False, 2, [1, 2], (1, 2)),292 ],293)294def test_good_defaults_for_multiple(runner, multiple, nargs, default, expected):

Add cross-reference to tests from `test_defaults.py` (#3223)Rowlando13before adoptionfa7f035

lines 295–306why, one step back
295 """Comprehensive check of default-value processing for options with
296 ``multiple=True`` and/or ``nargs > 1``.297298 .. hint::299 An argument-specific equivalent is in300 ``test_arguments.py::test_good_defaults_for_nargs``.301302 Smoke tests are in ``test_defaults.py``:303 ``test_multiple_defaults`` (explicit ``type=FLOAT``)304 and ``test_nargs_plus_multiple`` (``nargs=2``).305 """306

Merge branch 'main' into stableDavid Lordbefore adoption81a482f

lines 307–463why, one step back
307 @click.command()
308 @click.option("-a", multiple=multiple, nargs=nargs, default=default)309 def cmd(a):310 click.echo(repr(a), nl=False)311312 result = runner.invoke(cmd)313 assert result.output == repr(expected)314315316@pytest.mark.parametrize(317 ("multiple", "nargs", "default", "exception", "message"),318 [319 # Non-iterables defaults.320 (321 True,322 1,323 "Yo",324 None,325 "Error: Invalid value for '-a': Value must be an iterable.",326 ),327 (328 True,329 1,330 "",331 None,332 "Error: Invalid value for '-a': Value must be an iterable.",333 ),334 (335 True,336 1,337 True,338 None,339 "Error: Invalid value for '-a': Value must be an iterable.",340 ),341 (342 True,343 1,344 False,345 None,346 "Error: Invalid value for '-a': Value must be an iterable.",347 ),348 (349 True,350 1,351 12,352 None,353 "Error: Invalid value for '-a': Value must be an iterable.",354 ),355 (356 True,357 1,358 7.9,359 None,360 "Error: Invalid value for '-a': Value must be an iterable.",361 ),362 (363 False,364 2,365 42,366 None,367 "Error: Invalid value for '-a': Value must be an iterable.",368 ),369 (370 True,371 2,372 ["test string which is not a list in the list"],373 None,374 "Error: Invalid value for '-a': Value must be an iterable.",375 ),376 # Multiple options, each with 2 args, but with wrong length.377 (378 True,379 2,380 (1,),381 None,382 "Error: Invalid value for '-a': Value must be an iterable.",383 ),384 (385 True,386 2,387 (1, 2, 3),388 None,389 "Error: Invalid value for '-a': Value must be an iterable.",390 ),391 (392 True,393 2,394 [tuple()],395 ValueError,396 r"'nargs' must be 0 \(or None\) for type <click\.types\.Tuple object at "397 r"0x[0-9A-Fa-f]+>, but it was 2\.",398 ),399 (400 True,401 2,402 [(1,)],403 ValueError,404 r"'nargs' must be 1 \(or None\) for type <click\.types\.Tuple object at "405 r"0x[0-9A-Fa-f]+>, but it was 2\.",406 ),407 (408 True,409 2,410 [(1, 2, 3)],411 ValueError,412 r"'nargs' must be 3 \(or None\) for type <click\.types\.Tuple object at "413 r"0x[0-9A-Fa-f]+>, but it was 2\.",414 ),415 # A mix of valid and invalid defaults.416 (417 True,418 2,419 [[1, 2.2], []],420 None,421 "Error: Invalid value for '-a': 2 values are required, but 0 were given.",422 ),423 # Default values that are iterable but not of the right length.424 (425 False,426 2,427 [1],428 None,429 "Error: Invalid value for '-a': Takes 2 values but 1 was given.",430 ),431 (432 True,433 2,434 [[1]],435 ValueError,436 r"'nargs' must be 1 \(or None\) for type <click\.types\.Tuple object at "437 r"0x[0-9A-Fa-f]+>, but it was 2\.",438 ),439 ],440)441def test_bad_defaults_for_multiple(442 runner, multiple, nargs, default, exception, message443):444 if exception:445 assert issubclass(exception, Exception)446 else:447 assert exception is None448449 with (450 pytest.raises(exception, match=re.compile(message))451 if exception452 else nullcontext()453 ):454455 @click.command()456 @click.option("-a", multiple=multiple, nargs=nargs, default=default)457 def cmd(a):458 click.echo(repr(a))459460 result = runner.invoke(cmd)461 assert message in result.stderr462463

Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2

lines 464–465why, one step back
464@pytest.mark.parametrize("env_key", ["MYPATH", "AUTO_MYPATH"])
465def test_empty_envvar(runner, env_key):

Merge branch 'main' into stableDavid Lordbefore adoption81a482f

line 466why, one step back
466 @click.command()

Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2

lines 467–469why, one step back
467 @click.option("--mypath", type=click.Path(exists=True), envvar="MYPATH")
468 def cli(mypath):469 click.echo(f"mypath: {mypath}")

Merge branch 'main' into stableDavid Lordbefore adoption81a482f

line 470why, one step back
470

Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2

lines 471–473why, one step back
471 result = runner.invoke(cli, env={env_key: ""}, auto_envvar_prefix="AUTO")
472 assert result.exception is None473 assert result.output == "mypath: None\n"

Merge branch 'main' into stableDavid Lordbefore adoption81a482f

lines 474–475why, one step back
474
475

Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2

line 476why, one step back
476def test_multiple_envvar(runner):

Merge branch 'main' into stableDavid Lordbefore adoption81a482f

line 477why, one step back
477 @click.command()

Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2

line 478why, one step back
478 @click.option("--arg", multiple=True)

Merge branch 'main' into stableDavid Lordbefore adoption81a482f

lines 479–482why, one step back
479 def cmd(arg):
480 click.echo("|".join(arg))481482 result = runner.invoke(

Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2

line 483why, one step back
483 cmd, [], auto_envvar_prefix="TEST", env={"TEST_ARG": "foo bar baz"}

Merge branch 'main' into stableDavid Lordbefore adoption81a482f

lines 484–487why, one step back
484 )
485 assert not result.exception486 assert result.output == "foo|bar|baz\n"487

Stable (#3851)Rowlando13before adoption6aabf09

lines 488–494why, one step back
488
489def test_envvar_container_type(runner):490 """Env vars are plain strings, so a container type splits them character-wise.491492 Refs: https://github.com/pallets/click/issues/3036493 """494

Merge branch 'main' into stableDavid Lordbefore adoption81a482f

line 495why, one step back
495 @click.command()

Stable (#3851)Rowlando13before adoption6aabf09

lines 496–505why, one step back
496 @click.option("--x", envvar="X", type=frozenset)
497 def cli(x):498 assert isinstance(x, frozenset)499 click.echo("".join(sorted(x)))500501 result = runner.invoke(cli, [], env={"X": "git"})502 assert not result.exception503 assert result.output == "git\n"504505 @click.command()

Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2

line 506why, one step back
506 @click.option("--arg", multiple=True, envvar="X")

Merge branch 'main' into stableDavid Lordbefore adoption81a482f

lines 507–509why, one step back
507 def cmd(arg):
508 click.echo("|".join(arg))509

Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2

line 510why, one step back
510 result = runner.invoke(cmd, [], env={"X": "foo bar baz"})

Merge branch 'main' into stableDavid Lordbefore adoption81a482f

lines 511–514why, one step back
511 assert not result.exception
512 assert result.output == "foo|bar|baz\n"513514 @click.command()

Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2

line 515why, one step back
515 @click.option("--arg", multiple=True, type=click.Path())

Merge branch 'main' into stableDavid Lordbefore adoption81a482f

lines 516–519why, one step back
516 def cmd(arg):
517 click.echo("|".join(arg))518519 result = runner.invoke(

Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2

lines 520–523why, one step back
520 cmd,
521 [],522 auto_envvar_prefix="TEST",523 env={"TEST_ARG": f"foo{os.path.pathsep}bar"},

Merge branch 'main' into stableDavid Lordbefore adoption81a482f

lines 524–531why, one step back
524 )
525 assert not result.exception526 assert result.output == "foo|bar\n"527528529@pytest.mark.parametrize(530 ("envvar_name", "envvar_value", "expected"),531 (

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

lines 532–546why, one step back
532 # Lower-cased variations of value.
533 ("SHOUT", "true", True),534 ("SHOUT", "false", False),535 # Title-cased variations of value.536 ("SHOUT", "True", True),537 ("SHOUT", "False", False),538 # Upper-cased variations of value.539 ("SHOUT", "TRUE", True),540 ("SHOUT", "FALSE", False),541 # Random-cased variations of value.542 ("SHOUT", "TruE", True),543 ("SHOUT", "truE", True),544 ("SHOUT", "FaLsE", False),545 ("SHOUT", "falsE", False),546 # Extra spaces around the value.

Merge branch 'main' into stableDavid Lordbefore adoption81a482f

lines 547–552why, one step back
547 ("SHOUT", "true ", True),
548 ("SHOUT", " true ", True),549 ("SHOUT", " true", True),550 ("SHOUT", "false ", False),551 ("SHOUT", " false ", False),552 ("SHOUT", " false", False),

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

lines 553–557why, one step back
553 # Integer variations.
554 ("SHOUT", "1", True),555 ("SHOUT", "0", False),556 # Alternative states.557 ("SHOUT", "t", True),

Merge branch 'main' into stableDavid Lordbefore adoption81a482f

lines 558–559why, one step back
558 ("SHOUT", "T", True),
559 ("SHOUT", " T ", True),

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

lines 560–573why, one step back
560 ("SHOUT", "f", False),
561 ("SHOUT", "y", True),562 ("SHOUT", "n", False),563 ("SHOUT", "yes", True),564 ("SHOUT", "no", False),565 ("SHOUT", "on", True),566 ("SHOUT", "off", False),567 # Blank value variations.568 ("SHOUT", None, False),569 ("SHOUT", "", False),570 ("SHOUT", " ", False),571 ("SHOUT", " ", False),572 # Variable names are not stripped of spaces and so don't match the573 # flag, which then naturraly takes its default value.

Merge branch 'main' into stableDavid Lordbefore adoption81a482f

lines 574–575why, one step back
574 ("SHOUT ", "True", False),
575 ("SHOUT ", "False", False),

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

lines 576–577why, one step back
576 (" SHOUT ", "True", False),
577 (" SHOUT ", "False", False),

Merge branch 'main' into stableDavid Lordbefore adoption81a482f

lines 578–581why, one step back
578 (" SHOUT", "True", False),
579 (" SHOUT", "False", False),580 ("SH OUT", "True", False),581 ("SH OUT", "False", False),

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

lines 582–586why, one step back
582 # Same for random and reverse environment variable names: they are not
583 # recognized by the option.584 ("RANDOM", "True", False),585 ("NO_SHOUT", "True", False),586 ("NO_SHOUT", "False", False),

Merge branch 'main' into stableDavid Lordbefore adoption81a482f

lines 587–608why, one step back
587 ("NOSHOUT", "True", False),
588 ("NOSHOUT", "False", False),589 ),590)591def test_boolean_flag_envvar(runner, envvar_name, envvar_value, expected):592 assert isinstance(envvar_name, str)593 assert isinstance(envvar_value, str) or envvar_value is None594595 @click.command()596 @click.option("--shout/--no-shout", envvar="SHOUT")597 def cli(shout):598 click.echo(repr(shout), nl=False)599600 result = runner.invoke(cli, [], env={envvar_name: envvar_value})601 assert result.exit_code == 0602 assert result.output == repr(expected)603604605@pytest.mark.parametrize(606 "value",607 (608 # Extra spaces inside the value.

add codespell pre-commit hook (#3373)David Lordbefore adoption8bd8b4a

line 609why, one step back
609 "tr ue", # codespell:ignore ue

Merge branch 'main' into stableDavid Lordbefore adoption81a482f

lines 610–638why, one step back
610 "fa lse",
611 # Numbers.612 "10",613 "01",614 "00",615 "11",616 "0.0",617 "1.1",618 # Random strings.619 "randomstring",620 "None",621 "'None'",622 "A B",623 " 1 2 ",624 "9.3",625 "a;n",626 "x:y",627 "i/o",628 ),629)630def test_boolean_envvar_bad_values(runner, value):631 @click.command()632 @click.option("--shout/--no-shout", envvar="SHOUT")633 def cli(shout):634 click.echo(shout)635636 result = runner.invoke(cli, [], env={"SHOUT": value})637 assert result.exit_code == 2638 assert (

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

line 639why, one step back
639 f"Invalid value for '--shout': {value!r} is not a valid boolean."

Merge branch 'main' into stableDavid Lordbefore adoption81a482f

lines 640–643why, one step back
640 in result.output
641 )642643

Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2

line 644why, one step back
644def test_multiple_default_help(runner):

Merge branch 'main' into stableDavid Lordbefore adoption81a482f

line 645why, one step back
645 @click.command()

Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2

lines 646–648why, one step back
646 @click.option("--arg1", multiple=True, default=("foo", "bar"), show_default=True)
647 @click.option("--arg2", multiple=True, default=(1, 2), type=int, show_default=True)648 def cmd(arg, arg2):

Merge branch 'main' into stableDavid Lordbefore adoption81a482f

lines 649–652why, one step back
649 pass
650651 result = runner.invoke(cmd, ["--help"])652 assert not result.exception

Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2

lines 653–654why, one step back
653 assert "foo, bar" in result.output
654 assert "1, 2" in result.output

Merge branch 'main' into stableDavid Lordbefore adoption81a482f

lines 655–656why, one step back
655
656

Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2

line 657why, one step back
657def test_show_default_default_map(runner):

Merge branch 'main' into stableDavid Lordbefore adoption81a482f

line 658why, one step back
658 @click.command()

Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2

line 659why, one step back
659 @click.option("--arg", default="a", show_default=True)

Merge branch 'main' into stableDavid Lordbefore adoption81a482f

lines 660–662why, one step back
660 def cmd(arg):
661 click.echo(arg)662

Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2

line 663why, one step back
663 result = runner.invoke(cmd, ["--help"], default_map={"arg": "b"})

Merge branch 'main' into stableDavid Lordbefore adoption81a482f

lines 664–665why, one step back
664
665 assert not result.exception

Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2

line 666why, one step back
666 assert "[default: b]" in result.output

Merge branch 'main' into stableDavid Lordbefore adoption81a482f

lines 667–668why, one step back
667
668

Stable (#3851)Rowlando13before adoption6aabf09

lines 669–687why, one step back
669def test_show_default_container_defaults(runner):
670 """Container defaults are rendered through ``str()`` in the help output.671672 Refs: https://github.com/pallets/click/issues/3036673 """674675 @click.command()676 @click.option("--a", default=frozenset(["git"]), type=frozenset, show_default=True)677 @click.option("--b", default={"a": 1}, type=dict, show_default=True)678 @click.option("--c", default={"git"}, type=set, show_default=True)679 def cli(a, b, c):680 pass681682 result = runner.invoke(cli, ["--help"])683 assert "[default: frozenset({'git'})]" in result.output684 assert "[default: {'a': 1}]" in result.output685 assert "[default: {'git'}]" in result.output686687

Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2

lines 688–690why, one step back
688def test_multiple_default_type():
689 opt = click.Option(["-a"], multiple=True, default=(1, 2))690 assert opt.nargs == 1

Merge branch 'main' into stableDavid Lordbefore adoption81a482f

line 691why, one step back
691 assert opt.multiple

Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2

line 692why, one step back
692 assert opt.type is click.INT

Merge branch 'main' into stableDavid Lordbefore adoption81a482f

line 693why, one step back
693 ctx = click.Context(click.Command("test"))

Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2

line 694why, one step back
694 assert opt.get_default(ctx) == (1, 2)

Merge branch 'main' into stableDavid Lordbefore adoption81a482f

lines 695–696why, one step back
695
696

Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2

lines 697–699why, one step back
697def test_multiple_default_composite_type():
698 opt = click.Option(["-a"], multiple=True, default=[(1, "a")])699 assert opt.nargs == 2

Merge branch 'main' into stableDavid Lordbefore adoption81a482f

line 700why, one step back
700 assert opt.multiple

Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2

lines 701–702why, one step back
701 assert isinstance(opt.type, click.Tuple)
702 assert opt.type.types == [click.INT, click.STRING]

Merge branch 'main' into stableDavid Lordbefore adoption81a482f

line 703why, one step back
703 ctx = click.Context(click.Command("test"))

Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2

line 704why, one step back
704 assert opt.type_cast_value(ctx, opt.get_default(ctx)) == ((1, "a"),)

Merge branch 'main' into stableDavid Lordbefore adoption81a482f

lines 705–706why, one step back
705
706

Stable (#3851)Rowlando13before adoption6aabf09

lines 707–729why, one step back
707@pytest.mark.parametrize(
708 "default",709 [710 set(),711 {1, 2},712 frozenset(),713 frozenset(["git"]),714 {},715 {"a": 1},716 ],717)718def test_type_from_default_container(default):719 """Container defaults have no native Click type and fall back to STRING.720721 The documented "if no type is provided, the type of the default value is722 used" rule does not apply to ``set``, ``frozenset`` or ``dict`` defaults.723724 Refs: https://github.com/pallets/click/issues/3036725 """726 opt = click.Option(["-a"], default=default)727 assert opt.type is click.STRING728729

Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2

line 730why, one step back
730def test_parse_multiple_default_composite_type(runner):

Merge branch 'main' into stableDavid Lordbefore adoption81a482f

line 731why, one step back
731 @click.command()

Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2

lines 732–734why, one step back
732 @click.option("-a", multiple=True, default=("a", "b"))
733 @click.option("-b", multiple=True, default=[(1, "a")])734 def cmd(a, b):

Merge branch 'main' into stableDavid Lordbefore adoption81a482f

line 735why, one step back
735 click.echo(a)

Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2

line 736why, one step back
736 click.echo(b)

Merge branch 'main' into stableDavid Lordbefore adoption81a482f

line 737why, one step back
737

Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2

lines 738–739why, one step back
738 # result = runner.invoke(cmd, "-a c -a 1 -a d -b 2 two -b 4 four".split())
739 # assert result.output == "('c', '1', 'd')\n((2, 'two'), (4, 'four'))\n"

Merge branch 'main' into stableDavid Lordbefore adoption81a482f

line 740why, one step back
740 result = runner.invoke(cmd)

Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2

line 741why, one step back
741 assert result.output == "('a', 'b')\n((1, 'a'),)\n"

Merge branch 'main' into stableDavid Lordbefore adoption81a482f

lines 742–743why, one step back
742
743

Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2

line 744why, one step back
744def test_dynamic_default_help_unset(runner):

Merge branch 'main' into stableDavid Lordbefore adoption81a482f

lines 745–758why, one step back
745 @click.command()
746 @click.option(747 "--username",748 prompt=True,749 default=lambda: os.environ.get("USER", ""),750 show_default=True,751 )752 def cmd(username):753 print("Hello,", username)754755 result = runner.invoke(cmd, ["--help"])756 assert result.exit_code == 0757 assert "--username" in result.output758 assert "lambda" not in result.output

Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2

line 759why, one step back
759 assert "(dynamic)" in result.output

Merge branch 'main' into stableDavid Lordbefore adoption81a482f

lines 760–761why, one step back
760
761

Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2

line 762why, one step back
762def test_dynamic_default_help_text(runner):

Merge branch 'main' into stableDavid Lordbefore adoption81a482f

lines 763–767why, one step back
763 @click.command()
764 @click.option(765 "--username",766 prompt=True,767 default=lambda: os.environ.get("USER", ""),

Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2

line 768why, one step back
768 show_default="current user",

Merge branch 'main' into stableDavid Lordbefore adoption81a482f

lines 769–776why, one step back
769 )
770 def cmd(username):771 print("Hello,", username)772773 result = runner.invoke(cmd, ["--help"])774 assert result.exit_code == 0775 assert "--username" in result.output776 assert "lambda" not in result.output

Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2

line 777why, one step back
777 assert "(current user)" in result.output

Merge branch 'main' into stableDavid Lordbefore adoption81a482f

lines 778–779why, one step back
778
779

Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2

lines 780–783why, one step back
780def test_dynamic_default_help_special_method(runner):
781 class Value:782 def __call__(self):783 return 42

Merge branch 'main' into stableDavid Lordbefore adoption81a482f

line 784why, one step back
784

Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2

lines 785–786why, one step back
785 def __str__(self):
786 return "special value"

Merge branch 'main' into stableDavid Lordbefore adoption81a482f

line 787why, one step back
787

Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2

line 788why, one step back
788 opt = click.Option(["-a"], default=Value(), show_default=True)

Merge branch 'main' into stableDavid Lordbefore adoption81a482f

line 789why, one step back
789 ctx = click.Context(click.Command("cli"))

Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2

lines 790–791why, one step back
790 assert opt.get_help_extra(ctx) == {"default": "special value"}
791 assert "special value" in opt.get_help_record(ctx)[1]

Merge branch 'main' into stableDavid Lordbefore adoption81a482f

lines 792–794why, one step back
792
793794@pytest.mark.parametrize(

Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2

line 795why, one step back
795 ("type", "expect"),

Merge branch 'main' into stableDavid Lordbefore adoption81a482f

line 796why, one step back
796 [

Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2

lines 797–800why, one step back
797 (click.IntRange(1, 32), "1<=x<=32"),
798 (click.IntRange(1, 32, min_open=True, max_open=True), "1<x<32"),799 (click.IntRange(1), "x>=1"),800 (click.IntRange(max=32), "x<=32"),

Merge branch 'main' into stableDavid Lordbefore adoption81a482f

lines 801–802why, one step back
801 ],
802)

Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2

lines 803–804why, one step back
803def test_intrange_default_help_text(type, expect):
804 option = click.Option(["--num"], type=type, show_default=True, default=2)

Merge branch 'main' into stableDavid Lordbefore adoption81a482f

line 805why, one step back
805 context = click.Context(click.Command("test"))

Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2

line 806why, one step back
806 assert option.get_help_extra(context) == {"default": "2", "range": expect}

Merge branch 'main' into stableDavid Lordbefore adoption81a482f

line 807why, one step back
807 result = option.get_help_record(context)[1]

Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2

line 808why, one step back
808 assert expect in result

Merge branch 'main' into stableDavid Lordbefore adoption81a482f

lines 809–810why, one step back
809
810

Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2

lines 811–813why, one step back
811def test_count_default_type_help():
812 """A count option with the default type should not show >=0 in help."""813 option = click.Option(["--count"], count=True, help="some words")

Merge branch 'main' into stableDavid Lordbefore adoption81a482f

line 814why, one step back
814 context = click.Context(click.Command("test"))

Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2

line 815why, one step back
815 assert option.get_help_extra(context) == {}

Merge branch 'main' into stableDavid Lordbefore adoption81a482f

line 816why, one step back
816 result = option.get_help_record(context)[1]

Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2

line 817why, one step back
817 assert result == "some words"

Merge branch 'main' into stableDavid Lordbefore adoption81a482f

lines 818–819why, one step back
818
819

Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2

lines 820–822why, one step back
820def test_file_type_help_default():
821 """The default for a File type is a filename string. The string822 should be displayed in help, not an open file object.

Merge branch 'main' into stableDavid Lordbefore adoption81a482f

line 823why, one step back
823

Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2

lines 824–825why, one step back
824 Type casting is only applied to defaults in processing, not when
825 getting the default value.

Merge branch 'main' into stableDavid Lordbefore adoption81a482f

line 826why, one step back
826 """

Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2

line 827why, one step back
827 option = click.Option(

Deprecate parameter names that are: not a Python identifier, or a Python Keyword, or not lower-cased (#3866)Rowlando13before adoption06b2a67

line 828why, one step back
828 ["--infile"], type=click.File(), default=__file__, show_default=True

Merge branch 'main' into stableDavid Lordbefore adoption81a482f

lines 829–830why, one step back
829 )
830 context = click.Context(click.Command("test"))

Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2

line 831why, one step back
831 assert option.get_help_extra(context) == {"default": __file__}

Merge branch 'main' into stableDavid Lordbefore adoption81a482f

line 832why, one step back
832 result = option.get_help_record(context)[1]

Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2

line 833why, one step back
833 assert __file__ in result

Merge branch 'main' into stableDavid Lordbefore adoption81a482f

lines 834–835why, one step back
834
835

Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2

line 836why, one step back
836def test_toupper_envvar_prefix(runner):

Merge branch 'main' into stableDavid Lordbefore adoption81a482f

line 837why, one step back
837 @click.command()

Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2

line 838why, one step back
838 @click.option("--arg")

Merge branch 'main' into stableDavid Lordbefore adoption81a482f

lines 839–841why, one step back
839 def cmd(arg):
840 click.echo(arg)841

Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2

line 842why, one step back
842 result = runner.invoke(cmd, [], auto_envvar_prefix="test", env={"TEST_ARG": "foo"})

Merge branch 'main' into stableDavid Lordbefore adoption81a482f

line 843why, one step back
843 assert not result.exception

Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2

line 844why, one step back
844 assert result.output == "foo\n"

Merge branch 'main' into stableDavid Lordbefore adoption81a482f

lines 845–846why, one step back
845
846

Deprecate parameter names that are: not a Python identifier, or a Python Keyword, or not lower-cased (#3866)Rowlando13before adoption06b2a67

lines 847–972why, one step back
847# CPython upper-cases every key of ``os.environ`` when ``os.name == "nt"``,
848# see ``os._createenviron``. ``CliRunner`` writes the ``env`` mapping through849# ``os.environ`` and ``Parameter.resolve_envvar_value`` reads it back the same850# way, so on Windows a variable answers to every spelling of its name and851# elsewhere to exactly one. Both halves are asserted rather than skipped: the852# difference is the behaviour being pinned. See pallets/click#2483.853ENV_NAMES_ARE_CASE_INSENSITIVE = sys.platform == "win32"854855856def test_auto_envvar_uses_the_transformed_name(runner):857 """The auto envvar is built from the name, which the transform lower-cased."""858859 @click.command()860 @click.option("--Foo-Bar")861 def cmd(foo_bar):862 click.echo(foo_bar)863864 result = runner.invoke(865 cmd, [], auto_envvar_prefix="TEST", env={"TEST_FOO_BAR": "foo"}866 )867 assert not result.exception868 assert result.output == "foo\n"869870871def test_auto_envvar_ignores_decl_case(runner):872 """The case written in the declaration never reaches the auto envvar."""873874 @click.command()875 @click.option("--Foo-Bar")876 def cmd(foo_bar):877 click.echo(repr(foo_bar))878879 result = runner.invoke(880 cmd, [], auto_envvar_prefix="TEST", env={"TEST_Foo_Bar": "foo"}881 )882 assert not result.exception883 expect = "'foo'" if ENV_NAMES_ARE_CASE_INSENSITIVE else "None"884 assert result.output == f"{expect}\n"885886887def test_auto_envvar_prefix_is_upper_cased(runner):888 """A lower-case prefix reaches an upper-case variable, and only that one."""889890 @click.command()891 @click.option("--flag/--no-flag")892 def cmd(flag):893 click.echo(repr(flag))894895 result = runner.invoke(cmd, [], auto_envvar_prefix="yo", env={"YO_FLAG": "1"})896 assert not result.exception897 assert result.output == "True\n"898899 result = runner.invoke(cmd, [], auto_envvar_prefix="yo", env={"yo_FLAG": "1"})900 assert not result.exception901 assert result.output == f"{ENV_NAMES_ARE_CASE_INSENSITIVE}\n"902903904def test_auto_envvar_flattens_name_case(runner):905 """Two names differing only by case share one auto envvar."""906 with pytest.warns(DeprecationWarning, match="lower cases an explicit name"):907908 @click.command()909 @click.option("--foo-bar")910 @click.option("--other", "Foo_Bar")911 def cmd(**kwargs):912 click.echo(repr(sorted(kwargs.items())))913914 assert [p.name for p in cmd.params if p.name] == ["foo_bar", "Foo_Bar"]915916 result = runner.invoke(917 cmd, [], auto_envvar_prefix="TEST", env={"TEST_FOO_BAR": "foo"}918 )919 assert not result.exception920 assert result.output == "[('Foo_Bar', 'foo'), ('foo_bar', 'foo')]\n"921922923def test_auto_envvar_upper_can_change_length(runner):924 """Deriving the envvar is not the inverse of deriving the name."""925926 @click.command()927 @click.option("--ẞ")928 def cmd(**kwargs):929 click.echo(repr(kwargs))930931 result = runner.invoke(cmd, [], auto_envvar_prefix="TEST", env={"TEST_SS": "foo"})932 assert not result.exception933 assert result.output == "{'ß': 'foo'}\n"934935936@pytest.mark.parametrize(937 ("env", "expect"),938 [939 pytest.param({"ArG": "foo"}, "'foo'", id="exact"),940 pytest.param({"ARG": "foo"}, "None", id="upper"),941 pytest.param({"arg": "foo"}, "None", id="lower"),942 ],943)944def test_explicit_envvar_case_sensitivity(runner, env, expect):945 """An explicitly named envvar keeps the case it was registered with."""946947 @click.command()948 @click.option("--arg", envvar="ArG")949 def cmd(arg):950 click.echo(repr(arg))951952 result = runner.invoke(cmd, [], env=env)953 assert not result.exception954 if ENV_NAMES_ARE_CASE_INSENSITIVE:955 expect = "'foo'"956 assert result.output == f"{expect}\n"957958959@pytest.mark.parametrize("name", ("FlAg", "sUper"))960def test_explicit_envvar_list_keeps_each_spelling(runner, name):961 """Every name of an envvar list is matched with its own case."""962963 @click.command()964 @click.option("--flag/--no-flag", envvar=["FlAg", "sUper"])965 def cmd(flag):966 click.echo(repr(flag))967968 result = runner.invoke(cmd, [], env={name: "1"})969 assert not result.exception970 assert result.output == "True\n"971972

Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2

line 973why, one step back
973def test_nargs_envvar(runner):

Merge branch 'main' into stableDavid Lordbefore adoption81a482f

line 974why, one step back
974 @click.command()

Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2

line 975why, one step back
975 @click.option("--arg", nargs=2)

Merge branch 'main' into stableDavid Lordbefore adoption81a482f

lines 976–979why, one step back
976 def cmd(arg):
977 click.echo("|".join(arg))978979 result = runner.invoke(

Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2

line 980why, one step back
980 cmd, [], auto_envvar_prefix="TEST", env={"TEST_ARG": "foo bar"}

Merge branch 'main' into stableDavid Lordbefore adoption81a482f

lines 981–985why, one step back
981 )
982 assert not result.exception983 assert result.output == "foo|bar\n"984985 @click.command()

Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2

line 986why, one step back
986 @click.option("--arg", nargs=2, multiple=True)

Merge branch 'main' into stableDavid Lordbefore adoption81a482f

line 987why, one step back
987 def cmd(arg):

Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2

lines 988–989why, one step back
988 for item in arg:
989 click.echo("|".join(item))

Merge branch 'main' into stableDavid Lordbefore adoption81a482f

lines 990–991why, one step back
990
991 result = runner.invoke(

Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2

line 992why, one step back
992 cmd, [], auto_envvar_prefix="TEST", env={"TEST_ARG": "x 1 y 2"}

Merge branch 'main' into stableDavid Lordbefore adoption81a482f

lines 993–994why, one step back
993 )
994 assert not result.exception

Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2

line 995why, one step back
995 assert result.output == "x|1\ny|2\n"

Merge branch 'main' into stableDavid Lordbefore adoption81a482f

lines 996–997why, one step back
996
997

Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2

line 998why, one step back
998def test_show_envvar(runner):

Merge branch 'main' into stableDavid Lordbefore adoption81a482f

line 999why, one step back
999 @click.command()

Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2

line 1000why, one step back
1000 @click.option("--arg1", envvar="ARG1", show_envvar=True)

Merge branch 'main' into stableDavid Lordbefore adoption81a482f

lines 1001–1005why, one step back
1001 def cmd(arg):
1002 pass10031004 result = runner.invoke(cmd, ["--help"])1005 assert not result.exception

Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2

line 1006why, one step back
1006 assert "ARG1" in result.output

Merge branch 'main' into stableDavid Lordbefore adoption81a482f

lines 1007–1008why, one step back
1007
1008

Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2

line 1009why, one step back
1009def test_show_envvar_auto_prefix(runner):

Merge branch 'main' into stableDavid Lordbefore adoption81a482f

line 1010why, one step back
1010 @click.command()

Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2

line 1011why, one step back
1011 @click.option("--arg1", show_envvar=True)

Merge branch 'main' into stableDavid Lordbefore adoption81a482f

lines 1012–1014why, one step back
1012 def cmd(arg):
1013 pass1014

Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2

line 1015why, one step back
1015 result = runner.invoke(cmd, ["--help"], auto_envvar_prefix="TEST")

Merge branch 'main' into stableDavid Lordbefore adoption81a482f

line 1016why, one step back
1016 assert not result.exception

Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2

line 1017why, one step back
1017 assert "TEST_ARG1" in result.output

Merge branch 'main' into stableDavid Lordbefore adoption81a482f

lines 1018–1019why, one step back
1018
1019

Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2

lines 1020–1021why, one step back
1020def test_show_envvar_auto_prefix_dash_in_command(runner):
1021 @click.group()

Merge branch 'main' into stableDavid Lordbefore adoption81a482f

lines 1022–1024why, one step back
1022 def cli():
1023 pass1024

Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2

lines 1025–1027why, one step back
1025 @cli.command()
1026 @click.option("--baz", show_envvar=True)1027 def foo_bar(baz):

Merge branch 'main' into stableDavid Lordbefore adoption81a482f

lines 1028–1029why, one step back
1028 pass
1029

Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2

line 1030why, one step back
1030 result = runner.invoke(cli, ["foo-bar", "--help"], auto_envvar_prefix="TEST")

Merge branch 'main' into stableDavid Lordbefore adoption81a482f

line 1031why, one step back
1031 assert not result.exception

Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2

line 1032why, one step back
1032 assert "TEST_FOO_BAR_BAZ" in result.output

Merge branch 'main' into stableDavid Lordbefore adoption81a482f

lines 1033–1034why, one step back
1033
1034

Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2

lines 1035–1036why, one step back
1035def test_custom_validation(runner):
1036 def validate_pos_int(ctx, param, value):

Merge branch 'main' into stableDavid Lordbefore adoption81a482f

line 1037why, one step back
1037 if value < 0:

Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2

line 1038why, one step back
1038 raise click.BadParameter("Value needs to be positive")

Merge branch 'main' into stableDavid Lordbefore adoption81a482f

lines 1039–1041why, one step back
1039 return value
10401041 @click.command()

Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2

line 1042why, one step back
1042 @click.option("--foo", callback=validate_pos_int, default=1)

Merge branch 'main' into stableDavid Lordbefore adoption81a482f

lines 1043–1045why, one step back
1043 def cmd(foo):
1044 click.echo(foo)1045

Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2

lines 1046–1047why, one step back
1046 result = runner.invoke(cmd, ["--foo", "-1"])
1047 assert "Invalid value for '--foo': Value needs to be positive" in result.output

Merge branch 'main' into stableDavid Lordbefore adoption81a482f

line 1048why, one step back
1048

Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2

line 1049why, one step back
1049 result = runner.invoke(cmd, ["--foo", "42"])

Merge branch 'main' into stableDavid Lordbefore adoption81a482f

lines 1050–1052why, one step back
1050 assert result.output == "42\n"
10511052

Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2

lines 1053–1054why, one step back
1053def test_callback_validates_prompt(runner, monkeypatch):
1054 def validate(ctx, param, value):

Merge branch 'main' into stableDavid Lordbefore adoption81a482f

line 1055why, one step back
1055 if value < 0:

Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2

line 1056why, one step back
1056 raise click.BadParameter("should be positive")

Merge branch 'main' into stableDavid Lordbefore adoption81a482f

lines 1057–1060why, one step back
1057
1058 return value10591060 @click.command()

Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2

lines 1061–1062why, one step back
1061 @click.option("-a", type=int, callback=validate, prompt=True)
1062 def cli(a):

Merge branch 'main' into stableDavid Lordbefore adoption81a482f

lines 1063–1064why, one step back
1063 click.echo(a)
1064

Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2

lines 1065–1066why, one step back
1065 result = runner.invoke(cli, input="-12\n60\n")
1066 assert result.output == "A: -12\nError: should be positive\nA: 60\n60\n"

Merge branch 'main' into stableDavid Lordbefore adoption81a482f

lines 1067–1068why, one step back
1067
1068

Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2

line 1069why, one step back
1069def test_winstyle_options(runner):

Merge branch 'main' into stableDavid Lordbefore adoption81a482f

line 1070why, one step back
1070 @click.command()

Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2

lines 1071–1073why, one step back
1071 @click.option("/debug;/no-debug", help="Enables or disables debug mode.")
1072 def cmd(debug):1073 click.echo(debug)

Merge branch 'main' into stableDavid Lordbefore adoption81a482f

line 1074why, one step back
1074

Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2

line 1075why, one step back
1075 result = runner.invoke(cmd, ["/debug"], help_option_names=["/?"])

Merge branch 'main' into stableDavid Lordbefore adoption81a482f

line 1076why, one step back
1076 assert result.output == "True\n"

Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2

line 1077why, one step back
1077 result = runner.invoke(cmd, ["/no-debug"], help_option_names=["/?"])

Merge branch 'main' into stableDavid Lordbefore adoption81a482f

line 1078why, one step back
1078 assert result.output == "False\n"

Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2

line 1079why, one step back
1079 result = runner.invoke(cmd, [], help_option_names=["/?"])

Merge branch 'main' into stableDavid Lordbefore adoption81a482f

line 1080why, one step back
1080 assert result.output == "False\n"

Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2

lines 1081–1083why, one step back
1081 result = runner.invoke(cmd, ["/?"], help_option_names=["/?"])
1082 assert "/debug; /no-debug Enables or disables debug mode." in result.output1083 assert "/? Show this message and exit." in result.output

Merge branch 'main' into stableDavid Lordbefore adoption81a482f

lines 1084–1085why, one step back
1084
1085

Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2

line 1086why, one step back
1086def test_legacy_options(runner):

Merge branch 'main' into stableDavid Lordbefore adoption81a482f

line 1087why, one step back
1087 @click.command()

Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2

lines 1088–1090why, one step back
1088 @click.option("-whatever")
1089 def cmd(whatever):1090 click.echo(whatever)

Merge branch 'main' into stableDavid Lordbefore adoption81a482f

line 1091why, one step back
1091

Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2

line 1092why, one step back
1092 result = runner.invoke(cmd, ["-whatever", "42"])

Merge branch 'main' into stableDavid Lordbefore adoption81a482f

line 1093why, one step back
1093 assert result.output == "42\n"

Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2

lines 1094–1095why, one step back
1094 result = runner.invoke(cmd, ["-whatever=23"])
1095 assert result.output == "23\n"

Merge branch 'main' into stableDavid Lordbefore adoption81a482f

lines 1096–1117why, one step back
1096
10971098@pytest.mark.parametrize(1099 ("value", "expect_missing", "processed_value"),1100 [1101 (UNSET, True, None),1102 (None, False, None),1103 # Default type of the argument is str, so everything is processed as strings.1104 ("", False, ""),1105 (" ", False, " "),1106 ("foo", False, "foo"),1107 ("12", False, "12"),1108 (12, False, "12"),1109 (12.1, False, "12.1"),1110 (list(), False, "[]"),1111 (tuple(), False, "()"),1112 (set(), False, "set()"),1113 (frozenset(), False, "frozenset()"),1114 (dict(), False, "{}"),1115 ],1116)1117def test_required_option(value, expect_missing, processed_value):

Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2

line 1118why, one step back
1118 ctx = click.Context(click.Command(""))

Merge branch 'main' into stableDavid Lordbefore adoption81a482f

lines 1119–1130why, one step back
1119 argument = click.Option(["-a"], required=True)
11201121 if expect_missing:1122 with pytest.raises(click.MissingParameter) as excinfo:1123 argument.process_value(ctx, value)1124 assert str(excinfo.value) == "Missing parameter: a"11251126 else:1127 value = argument.process_value(ctx, value)1128 assert value == processed_value11291130

Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2

line 1131why, one step back
1131def test_missing_required_flag(runner):

Merge branch 'main' into stableDavid Lordbefore adoption81a482f

line 1132why, one step back
1132 cli = click.Command(

Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2

line 1133why, one step back
1133 "cli", params=[click.Option(["--on/--off"], is_flag=True, required=True)]

Merge branch 'main' into stableDavid Lordbefore adoption81a482f

lines 1134–1136why, one step back
1134 )
1135 result = runner.invoke(cli)1136 assert result.exit_code == 2

Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2

line 1137why, one step back
1137 assert "Error: Missing option '--on'." in result.output

Merge branch 'main' into stableDavid Lordbefore adoption81a482f

lines 1138–1139why, one step back
1138
1139

Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2

line 1140why, one step back
1140def test_missing_choice(runner):

Merge branch 'main' into stableDavid Lordbefore adoption81a482f

line 1141why, one step back
1141 @click.command()

Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2

line 1142why, one step back
1142 @click.option("--foo", type=click.Choice(["foo", "bar"]), required=True)

Merge branch 'main' into stableDavid Lordbefore adoption81a482f

lines 1143–1147why, one step back
1143 def cmd(foo):
1144 click.echo(foo)11451146 result = runner.invoke(cmd)1147 assert result.exit_code == 2

Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2

lines 1148–1152why, one step back
1148 error, separator, choices = result.output.partition("Choose from")
1149 assert "Error: Missing option '--foo'. " in error1150 assert "Choose from" in separator1151 assert "foo" in choices1152 assert "bar" in choices

Merge branch 'main' into stableDavid Lordbefore adoption81a482f

lines 1153–1154why, one step back
1153
1154

Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2

line 1155why, one step back
1155def test_missing_envvar(runner):

Merge branch 'main' into stableDavid Lordbefore adoption81a482f

line 1156why, one step back
1156 cli = click.Command(

Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2

line 1157why, one step back
1157 "cli", params=[click.Option(["--foo"], envvar="bar", required=True)]

Merge branch 'main' into stableDavid Lordbefore adoption81a482f

lines 1158–1163why, one step back
1158 )
1159 result = runner.invoke(cli)1160 assert result.exit_code == 21161 assert "Error: Missing option '--foo'." in result.output1162 cli = click.Command(1163 "cli",

Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2

line 1164why, one step back
1164 params=[click.Option(["--foo"], envvar="bar", show_envvar=True, required=True)],

Merge branch 'main' into stableDavid Lordbefore adoption81a482f

lines 1165–1167why, one step back
1165 )
1166 result = runner.invoke(cli)1167 assert result.exit_code == 2

Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2

line 1168why, one step back
1168 assert "Error: Missing option '--foo' (env var: 'bar')." in result.output

Merge branch 'main' into stableDavid Lordbefore adoption81a482f

lines 1169–1170why, one step back
1169
1170 cli = click.Command(

Don't include envvar in error hint when envvar not configured (#2972)Rowlando13before adoption0be2bd3

line 1171why, one step back
1171 "cli", params=[click.Option(["--foo"], show_envvar=True, required=True)]

Merge branch 'main' into stableDavid Lordbefore adoption81a482f

lines 1172–1177why, one step back
1172 )
1173 result = runner.invoke(cli)1174 assert result.exit_code == 21175 assert "Error: Missing option '--foo'." in result.output11761177

Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2

line 1178why, one step back
1178def test_case_insensitive_choice(runner):

Merge branch 'main' into stableDavid Lordbefore adoption81a482f

lines 1179–1187why, one step back
1179 @click.command()
1180 @click.option("--foo", type=click.Choice(["Orange", "Apple"], case_sensitive=False))1181 def cmd(foo):1182 click.echo(foo)11831184 result = runner.invoke(cmd, ["--foo", "apple"])1185 assert result.exit_code == 01186 assert result.output == "Apple\n"1187

Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2

line 1188why, one step back
1188 result = runner.invoke(cmd, ["--foo", "oRANGe"])

Merge branch 'main' into stableDavid Lordbefore adoption81a482f

line 1189why, one step back
1189 assert result.exit_code == 0

Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2

line 1190why, one step back
1190 assert result.output == "Orange\n"

Merge branch 'main' into stableDavid Lordbefore adoption81a482f

line 1191why, one step back
1191

Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2

line 1192why, one step back
1192 result = runner.invoke(cmd, ["--foo", "Apple"])

Merge branch 'main' into stableDavid Lordbefore adoption81a482f

lines 1193–1196why, one step back
1193 assert result.exit_code == 0
1194 assert result.output == "Apple\n"11951196 @click.command()

Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2

lines 1197–1198why, one step back
1197 @click.option("--foo", type=click.Choice(["Orange", "Apple"]))
1198 def cmd2(foo):

Merge branch 'main' into stableDavid Lordbefore adoption81a482f

lines 1199–1200why, one step back
1199 click.echo(foo)
1200

Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2

line 1201why, one step back
1201 result = runner.invoke(cmd2, ["--foo", "apple"])

Merge branch 'main' into stableDavid Lordbefore adoption81a482f

lines 1202–1203why, one step back
1202 assert result.exit_code == 2
1203

Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2

line 1204why, one step back
1204 result = runner.invoke(cmd2, ["--foo", "oRANGe"])

Merge branch 'main' into stableDavid Lordbefore adoption81a482f

lines 1205–1206why, one step back
1205 assert result.exit_code == 2
1206

Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2

line 1207why, one step back
1207 result = runner.invoke(cmd2, ["--foo", "Apple"])

Merge branch 'main' into stableDavid Lordbefore adoption81a482f

lines 1208–1210why, one step back
1208 assert result.exit_code == 0
12091210

Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2

line 1211why, one step back
1211def test_case_insensitive_choice_returned_exactly(runner):

Merge branch 'main' into stableDavid Lordbefore adoption81a482f

lines 1212–1221why, one step back
1212 @click.command()
1213 @click.option("--foo", type=click.Choice(["Orange", "Apple"], case_sensitive=False))1214 def cmd(foo):1215 click.echo(foo)12161217 result = runner.invoke(cmd, ["--foo", "apple"])1218 assert result.exit_code == 01219 assert result.output == "Apple\n"12201221

Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2

line 1222why, one step back
1222def test_option_help_preserve_paragraphs(runner):

Merge branch 'main' into stableDavid Lordbefore adoption81a482f

lines 1223–1224why, one step back
1223 @click.command()
1224 @click.option(

Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2

lines 1225–1228why, one step back
1225 "-C",
1226 "--config",1227 type=click.Path(),1228 help="""Configuration file to use.

Merge branch 'main' into stableDavid Lordbefore adoption81a482f

line 1229why, one step back
1229

Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2

lines 1230–1232why, one step back
1230 If not given, the environment variable CONFIG_FILE is consulted
1231 and used if set. If neither are given, a default configuration1232 file is loaded.""",

Merge branch 'main' into stableDavid Lordbefore adoption81a482f

line 1233why, one step back
1233 )

Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2

line 1234why, one step back
1234 def cmd(config):

Merge branch 'main' into stableDavid Lordbefore adoption81a482f

lines 1235–1238why, one step back
1235 pass
12361237 result = runner.invoke(cmd, ["--help"])1238 assert result.exit_code == 0

Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2

line 1239why, one step back
1239 i = " " * 21

Merge branch 'main' into stableDavid Lordbefore adoption81a482f

line 1240why, one step back
1240 assert (

Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2

lines 1241–1246why, one step back
1241 " -C, --config PATH Configuration file to use.\n"
1242 f"{i}\n"1243 f"{i}If not given, the environment variable CONFIG_FILE is\n"1244 f"{i}consulted and used if set. If neither are given, a default\n"1245 f"{i}configuration file is loaded."1246 ) in result.output

Merge branch 'main' into stableDavid Lordbefore adoption81a482f

lines 1247–1248why, one step back
1247
1248

Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2

lines 1249–1253why, one step back
1249def test_argument_custom_class(runner):
1250 class CustomArgument(click.Argument):1251 def get_default(self, ctx, call=True):1252 """a dumb override of a default value for testing"""1253 return "I am a default"

Merge branch 'main' into stableDavid Lordbefore adoption81a482f

lines 1254–1255why, one step back
1254
1255 @click.command()

add codespell pre-commit hook (#3373)David Lordbefore adoption8bd8b4a

line 1256why, one step back
1256 @click.argument("testarg", cls=CustomArgument, default="you won't see me")

Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2

lines 1257–1258why, one step back
1257 def cmd(testarg):
1258 click.echo(testarg)

Merge branch 'main' into stableDavid Lordbefore adoption81a482f

lines 1259–1260why, one step back
1259
1260 result = runner.invoke(cmd)

Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2

line 1261why, one step back
1261 assert "I am a default" in result.output

add codespell pre-commit hook (#3373)David Lordbefore adoption8bd8b4a

line 1262why, one step back
1262 assert "you won't see me" not in result.output

Merge branch 'main' into stableDavid Lordbefore adoption81a482f

lines 1263–1264why, one step back
1263
1264

Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2

line 1265why, one step back
1265def test_option_custom_class(runner):

Merge branch 'main' into stableDavid Lordbefore adoption81a482f

lines 1266–1271why, one step back
1266 class CustomOption(click.Option):
1267 def get_help_record(self, ctx):1268 """a dumb override of a help text for testing"""1269 return ("--help", "I am a help text")12701271 @click.command()

add codespell pre-commit hook (#3373)David Lordbefore adoption8bd8b4a

line 1272why, one step back
1272 @click.option("--testoption", cls=CustomOption, help="you won't see me")

Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2

line 1273why, one step back
1273 def cmd(testoption):

Merge branch 'main' into stableDavid Lordbefore adoption81a482f

lines 1274–1276why, one step back
1274 click.echo(testoption)
12751276 result = runner.invoke(cmd, ["--help"])

Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2

line 1277why, one step back
1277 assert "I am a help text" in result.output

add codespell pre-commit hook (#3373)David Lordbefore adoption8bd8b4a

line 1278why, one step back
1278 assert "you won't see me" not in result.output

Merge branch 'main' into stableDavid Lordbefore adoption81a482f

lines 1279–1280why, one step back
1279
1280

Ensure that `type_cast_value` never gets `UNSET` (#3090)Rowlando13before adoption8d4051e

lines 1281–1316why, one step back
1281@pytest.mark.parametrize(
1282 ("param_decl", "option_kwargs", "pass_argv"),1283 (1284 # there is a large potential parameter space to explore here1285 # this is just a very small sample of it1286 ("--opt", {}, []),1287 ("--opt", {"multiple": True}, []),1288 ("--opt", {"is_flag": True}, []),1289 ("--opt/--no-opt", {"is_flag": True, "default": None}, []),1290 ("--req", {"is_flag": True, "required": True}, ["--req"]),1291 ),1292)1293def test_option_custom_class_can_override_type_cast_value_and_never_sees_unset(1294 runner, param_decl, option_kwargs, pass_argv1295):1296 """1297 Test that overriding type_cast_value is supported12981299 In particular, the option is never passed an UNSET sentinel value.1300 """13011302 class CustomOption(click.Option):1303 def type_cast_value(self, ctx, value):1304 assert value is not UNSET1305 return value13061307 @click.command()1308 @click.option("myparam", param_decl, **option_kwargs, cls=CustomOption)1309 def cmd(myparam):1310 click.echo("ok")13111312 result = runner.invoke(cmd, pass_argv)1313 assert not result.exception1314 assert result.exit_code == 013151316

Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2

lines 1317–1318why, one step back
1317def test_option_custom_class_reusable(runner):
1318 """Ensure we can reuse a custom class option. See Issue #926"""

Merge branch 'main' into stableDavid Lordbefore adoption81a482f

lines 1319–1324why, one step back
1319
1320 class CustomOption(click.Option):1321 def get_help_record(self, ctx):1322 """a dumb override of a help text for testing"""1323 return ("--help", "I am a help text")1324

add codespell pre-commit hook (#3373)David Lordbefore adoption8bd8b4a

lines 1325–1326why, one step back
1325 # Assign to a variable to reuse the decorator.
1326 testoption = click.option("--testoption", cls=CustomOption, help="you won't see me")

Merge branch 'main' into stableDavid Lordbefore adoption81a482f

lines 1327–1329why, one step back
1327
1328 @click.command()1329 @testoption

Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2

line 1330why, one step back
1330 def cmd1(testoption):

Merge branch 'main' into stableDavid Lordbefore adoption81a482f

lines 1331–1334why, one step back
1331 click.echo(testoption)
13321333 @click.command()1334 @testoption

Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2

line 1335why, one step back
1335 def cmd2(testoption):

Merge branch 'main' into stableDavid Lordbefore adoption81a482f

lines 1336–1337why, one step back
1336 click.echo(testoption)
1337

Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2

lines 1338–1341why, one step back
1338 # Both of the commands should have the --help option now.
1339 for cmd in (cmd1, cmd2):1340 result = runner.invoke(cmd, ["--help"])1341 assert "I am a help text" in result.output

add codespell pre-commit hook (#3373)David Lordbefore adoption8bd8b4a

line 1342why, one step back
1342 assert "you won't see me" not in result.output

Merge branch 'main' into stableDavid Lordbefore adoption81a482f

lines 1343–1344why, one step back
1343
1344

Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2

line 1345why, one step back
1345@pytest.mark.parametrize("custom_class", (True, False))

Merge branch 'main' into stableDavid Lordbefore adoption81a482f

line 1346why, one step back
1346@pytest.mark.parametrize(

Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2

line 1347why, one step back
1347 ("name_specs", "expected"),

Merge branch 'main' into stableDavid Lordbefore adoption81a482f

lines 1348–1349why, one step back
1348 (
1349 (

Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2

lines 1350–1351why, one step back
1350 ("-h", "--help"),
1351 " -h, --help Show this message and exit.\n",

Merge branch 'main' into stableDavid Lordbefore adoption81a482f

lines 1352–1353why, one step back
1352 ),
1353 (

Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2

lines 1354–1355why, one step back
1354 ("-h",),
1355 " -h Show this message and exit.\n"

Merge branch 'main' into stableDavid Lordbefore adoption81a482f

lines 1356–1358why, one step back
1356 " --help Show this message and exit.\n",
1357 ),1358 (

Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2

line 1359why, one step back
1359 ("--help",),

Merge branch 'main' into stableDavid Lordbefore adoption81a482f

lines 1360–1363why, one step back
1360 " --help Show this message and exit.\n",
1361 ),1362 ),1363)

Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2

lines 1364–1365why, one step back
1364def test_help_option_custom_names_and_class(runner, custom_class, name_specs, expected):
1365 class CustomHelpOption(click.Option):

Merge branch 'main' into stableDavid Lordbefore adoption81a482f

lines 1366–1367why, one step back
1366 pass
1367

Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2

lines 1368–1370why, one step back
1368 option_attrs = {}
1369 if custom_class:1370 option_attrs["cls"] = CustomHelpOption

Merge branch 'main' into stableDavid Lordbefore adoption81a482f

lines 1371–1372why, one step back
1371
1372 @click.command()

Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2

lines 1373–1374why, one step back
1373 @click.help_option(*name_specs, **option_attrs)
1374 def cmd():

Merge branch 'main' into stableDavid Lordbefore adoption81a482f

lines 1375–1376why, one step back
1375 pass
1376

Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2

lines 1377–1381why, one step back
1377 for arg in name_specs:
1378 result = runner.invoke(cmd, [arg])1379 assert not result.exception1380 assert result.exit_code == 01381 assert expected in result.output

Merge branch 'main' into stableDavid Lordbefore adoption81a482f

lines 1382–1383why, one step back
1382
1383

Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2

line 1384why, one step back
1384def test_bool_flag_with_type(runner):

Merge branch 'main' into stableDavid Lordbefore adoption81a482f

line 1385why, one step back
1385 @click.command()

Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2

lines 1386–1387why, one step back
1386 @click.option("--shout/--no-shout", default=False, type=bool)
1387 def cmd(shout):

Merge branch 'main' into stableDavid Lordbefore adoption81a482f

lines 1388–1393why, one step back
1388 pass
13891390 result = runner.invoke(cmd)1391 assert not result.exception13921393

Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2

line 1394why, one step back
1394def test_aliases_for_flags(runner):

Merge branch 'main' into stableDavid Lordbefore adoption81a482f

line 1395why, one step back
1395 @click.command()

Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2

lines 1396–1397why, one step back
1396 @click.option("--warnings/--no-warnings", " /-W", default=True)
1397 def cli(warnings):

Merge branch 'main' into stableDavid Lordbefore adoption81a482f

lines 1398–1399why, one step back
1398 click.echo(warnings)
1399

Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2

line 1400why, one step back
1400 result = runner.invoke(cli, ["--warnings"])

Merge branch 'main' into stableDavid Lordbefore adoption81a482f

line 1401why, one step back
1401 assert result.output == "True\n"

Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2

line 1402why, one step back
1402 result = runner.invoke(cli, ["--no-warnings"])

Merge branch 'main' into stableDavid Lordbefore adoption81a482f

line 1403why, one step back
1403 assert result.output == "False\n"

Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2

line 1404why, one step back
1404 result = runner.invoke(cli, ["-W"])

Merge branch 'main' into stableDavid Lordbefore adoption81a482f

lines 1405–1407why, one step back
1405 assert result.output == "False\n"
14061407 @click.command()

Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2

lines 1408–1409why, one step back
1408 @click.option("--warnings/--no-warnings", "-w", default=True)
1409 def cli_alt(warnings):

Merge branch 'main' into stableDavid Lordbefore adoption81a482f

lines 1410–1411why, one step back
1410 click.echo(warnings)
1411

Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2

line 1412why, one step back
1412 result = runner.invoke(cli_alt, ["--warnings"])

Merge branch 'main' into stableDavid Lordbefore adoption81a482f

line 1413why, one step back
1413 assert result.output == "True\n"

Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2

line 1414why, one step back
1414 result = runner.invoke(cli_alt, ["--no-warnings"])

Merge branch 'main' into stableDavid Lordbefore adoption81a482f

line 1415why, one step back
1415 assert result.output == "False\n"

Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2

line 1416why, one step back
1416 result = runner.invoke(cli_alt, ["-w"])

Merge branch 'main' into stableDavid Lordbefore adoption81a482f

lines 1417–1420why, one step back
1417 assert result.output == "True\n"
141814191420@pytest.mark.parametrize(

Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2

line 1421why, one step back
1421 "option_args,expected",

Merge branch 'main' into stableDavid Lordbefore adoption81a482f

line 1422why, one step back
1422 [

Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2

lines 1423–1432why, one step back
1423 (["--aggressive", "--all", "-a"], "aggressive"),
1424 (["--first", "--second", "--third", "-a", "-b", "-c"], "first"),1425 (["--apple", "--banana", "--cantaloupe", "-a", "-b", "-c"], "apple"),1426 (["--cantaloupe", "--banana", "--apple", "-c", "-b", "-a"], "cantaloupe"),1427 (["-a", "-b", "-c"], "a"),1428 (["-c", "-b", "-a"], "c"),1429 (["-a", "--apple", "-b", "--banana", "-c", "--cantaloupe"], "apple"),1430 (["-c", "-a", "--cantaloupe", "-b", "--banana", "--apple"], "cantaloupe"),1431 (["--from", "-f", "_from"], "_from"),1432 (["--return", "-r", "_ret"], "_ret"),

Deprecate parameter names that are: not a Python identifier, or a Python Keyword, or not lower-cased (#3866)Rowlando13before adoption06b2a67

lines 1433–1446why, one step back
1433 # A name derived from an option string is lower-cased.
1434 (["--Foo-Bar"], "foo_bar"),1435 (["--FOO-BAR", "-F"], "foo_bar"),1436 # An identifier declaration goes through the same transform.1437 (["--foo-bar", "-f", "explicit_name"], "explicit_name"),1438 # Underscores survive, and every dash past the prefix becomes one.1439 (["--foo__bar"], "foo__bar"),1440 (["--foo--bar"], "foo__bar"),1441 (["--_foo"], "_foo"),1442 (["--__foo"], "__foo"),1443 (["---foo"], "_foo"),1444 (["-_"], "_"),1445 # A digit is only refused in the leading position.1446 (["--foo-0"], "foo_0"),

Merge branch 'main' into stableDavid Lordbefore adoption81a482f

lines 1447–1448why, one step back
1447 ],
1448)

Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2

line 1449why, one step back
1449def test_option_names(runner, option_args, expected):

Merge branch 'main' into stableDavid Lordbefore adoption81a482f

line 1450why, one step back
1450 @click.command()

Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2

lines 1451–1453why, one step back
1451 @click.option(*option_args, is_flag=True)
1452 def cmd(**kwargs):1453 click.echo(str(kwargs[expected]))

Merge branch 'main' into stableDavid Lordbefore adoption81a482f

line 1454why, one step back
1454

Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2

line 1455why, one step back
1455 assert cmd.params[0].name == expected

Merge branch 'main' into stableDavid Lordbefore adoption81a482f

line 1456why, one step back
1456

Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2

lines 1457–1460why, one step back
1457 for form in option_args:
1458 if form.startswith("-"):1459 result = runner.invoke(cmd, [form])1460 assert result.output == "True\n"

Deprecate parameter names that are: not a Python identifier, or a Python Keyword, or not lower-cased (#3866)Rowlando13before adoption06b2a67

lines 1461–1579why, one step back
1461
14621463def test_option_name_case_transform_can_collide(runner):1464 """Two declarations that differ can transform to one name, with no warning."""14651466 @click.command()1467 @click.option("--\N{KELVIN SIGN}")1468 @click.option("--k")1469 def cmd(**kwargs):1470 click.echo(repr(kwargs))14711472 assert [p.name for p in cmd.params] == ["k", "k"]14731474 result = runner.invoke(cmd, ["--\N{KELVIN SIGN}", "kelvin", "--k", "ascii"])1475 assert not result.exception1476 assert result.output == "{'k': 'ascii'}\n"147714781479def test_option_name_case_variants_share_one_parameter(runner):1480 """Case variants of one option collapse onto a single parameter."""14811482 @click.command()1483 @click.option("--foo-bar")1484 @click.option("--Foo-Bar")1485 @click.option("--FOO-BAR")1486 def cmd(**kwargs):1487 click.echo(repr(kwargs))14881489 assert [p.name for p in cmd.params] == ["foo_bar"] * 314901491 with warnings.catch_warnings(record=True) as caught:1492 warnings.simplefilter("always")1493 result = runner.invoke(cmd, ["--help"], catch_exceptions=False)14941495 assert not [w for w in caught if issubclass(w.category, UserWarning)]1496 for spelling in ("--foo-bar", "--Foo-Bar", "--FOO-BAR"):1497 assert spelling in result.output14981499 for spelling, value in (("--foo-bar", "a"), ("--Foo-Bar", "b"), ("--FOO-BAR", "c")):1500 result = runner.invoke(cmd, [spelling, value])1501 assert not result.exception1502 assert result.output == f"{{'foo_bar': {value!r}}}\n"15031504 # Sharing one slot, the last spelling on the command line wins.1505 result = runner.invoke(cmd, ["--foo-bar", "a", "--FOO-BAR", "c"])1506 assert result.output == "{'foo_bar': 'c'}\n"150715081509def test_option_name_keeps_its_normalization_form(runner):1510 """A composed and a decomposed declaration are two distinct parameters."""1511 decomposed = "cafe\N{COMBINING ACUTE ACCENT}"1512 composed = unicodedata.normalize("NFC", decomposed)15131514 @click.command()1515 @click.option(f"--{composed}")1516 @click.option(f"--{decomposed}")1517 def cmd(**kwargs):1518 click.echo(repr(sorted(kwargs)))15191520 assert [p.name for p in cmd.params] == [composed, decomposed]15211522 result = runner.invoke(cmd, [])1523 assert not result.exception1524 assert result.output == f"['{decomposed}', '{composed}']\n"152515261527def test_option_name_must_be_an_identifier():1528 """A short option is the one refused shape an argument cannot be written as."""1529 with pytest.raises(TypeError, match="Could not determine name"):1530 click.Option(["-0"])153115321533def test_option_prompt_still_guards_against_a_nameless_option():1534 """An unexposed option can still be named ``""``, which ``prompt=True`` refuses."""1535 with pytest.warns(DeprecationWarning, match="not a valid Python identifier"):1536 with pytest.raises(TypeError, match="'name' is required with 'prompt=True'"):1537 click.Option(["--0-file"], expose_value=False, prompt=True)153815391540def test_option_name_check_applies_when_not_exposed():1541 """An unexposed option is named ``""``, and says Click 9.0 will refuse it."""1542 with pytest.warns(DeprecationWarning, match="not a valid Python identifier"):1543 assert click.Option(["--0foo"], expose_value=False).name == ""154415451546def test_option_explicit_name_carries_a_refused_declaration(runner):1547 """An explicit name reaches a declaration the transform cannot name."""1548 seen = []15491550 def record(ctx, param, value):1551 seen.append(value)15521553 @click.command()1554 @click.option("--0foo", "zero_foo", expose_value=False, callback=record)1555 def cmd(**kwargs):1556 click.echo(repr(kwargs))15571558 assert cmd.params[0].name == "zero_foo"15591560 result = runner.invoke(cmd, ["--0foo", "value"])1561 assert not result.exception1562 assert result.output == "{}\n"1563 assert seen == ["value"]156415651566def test_option_name_may_be_a_python_keyword(runner):1567 """``str.isidentifier()`` accepts a keyword, so the check lets one through."""1568 with pytest.warns(DeprecationWarning, match="is a Python keyword"):15691570 @click.command()1571 @click.option("--from")1572 def cmd(**kwargs):1573 click.echo(repr(kwargs))15741575 assert cmd.params[0].name == "from"15761577 result = runner.invoke(cmd, ["--from", "here"])1578 assert not result.exception1579 assert result.output == "{'from': 'here'}\n"

Merge branch 'main' into stableDavid Lordbefore adoption81a482f

lines 1580–1581why, one step back
1580
1581

Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2

lines 1582–1584why, one step back
1582def test_flag_duplicate_names(runner):
1583 with pytest.raises(ValueError, match="cannot use the same flag for true/false"):1584 click.Option(["--foo/--foo"], default=False)

Merge branch 'main' into stableDavid Lordbefore adoption81a482f

lines 1585–1586why, one step back
1585
1586

Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2

lines 1587–1591why, one step back
1587@pytest.mark.parametrize(("default", "expect"), [(False, "no-cache"), (True, "cache")])
1588def test_show_default_boolean_flag_name(runner, default, expect):1589 """When a boolean flag has distinct True/False opts, it should show1590 the default opt name instead of the default value. It should only1591 show one name even if multiple are declared.

Merge branch 'main' into stableDavid Lordbefore adoption81a482f

lines 1592–1593why, one step back
1592 """
1593 opt = click.Option(

Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2

line 1594why, one step back
1594 ("--cache/--no-cache", "--c/--nc"),

Merge branch 'main' into stableDavid Lordbefore adoption81a482f

lines 1595–1596why, one step back
1595 default=default,
1596 show_default=True,

Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2

line 1597why, one step back
1597 help="Enable/Disable the cache.",

Merge branch 'main' into stableDavid Lordbefore adoption81a482f

lines 1598–1599why, one step back
1598 )
1599 ctx = click.Context(click.Command("test"))

Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2

line 1600why, one step back
1600 assert opt.get_help_extra(ctx) == {"default": expect}

Merge branch 'main' into stableDavid Lordbefore adoption81a482f

line 1601why, one step back
1601 message = opt.get_help_record(ctx)[1]

Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2

line 1602why, one step back
1602 assert f"[default: {expect}]" in message

Merge branch 'main' into stableDavid Lordbefore adoption81a482f

lines 1603–1604why, one step back
1603
1604

Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2

lines 1605–1607why, one step back
1605def test_show_true_default_boolean_flag_value(runner):
1606 """When a boolean flag only has one opt and its default is True,1607 it will show the default value, not the opt name.

Merge branch 'main' into stableDavid Lordbefore adoption81a482f

lines 1608–1612why, one step back
1608 """
1609 opt = click.Option(1610 ("--cache",),1611 is_flag=True,1612 show_default=True,

Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2

line 1613why, one step back
1613 default=True,

Merge branch 'main' into stableDavid Lordbefore adoption81a482f

lines 1614–1616why, one step back
1614 help="Enable the cache.",
1615 )1616 ctx = click.Context(click.Command("test"))

Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2

line 1617why, one step back
1617 assert opt.get_help_extra(ctx) == {"default": "True"}

Merge branch 'main' into stableDavid Lordbefore adoption81a482f

line 1618why, one step back
1618 message = opt.get_help_record(ctx)[1]

Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2

line 1619why, one step back
1619 assert "[default: True]" in message

Merge branch 'main' into stableDavid Lordbefore adoption81a482f

lines 1620–1621why, one step back
1620
1621

Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2

lines 1622–1625why, one step back
1622@pytest.mark.parametrize("default", [False, None])
1623def test_hide_false_default_boolean_flag_value(runner, default):1624 """When a boolean flag only has one opt and its default is False or1625 None, it will not show the default

Merge branch 'main' into stableDavid Lordbefore adoption81a482f

lines 1626–1636why, one step back
1626 """
1627 opt = click.Option(1628 ("--cache",),1629 is_flag=True,1630 show_default=True,1631 default=default,1632 help="Enable the cache.",1633 )1634 ctx = click.Context(click.Command("test"))1635 assert opt.get_help_extra(ctx) == {}1636 message = opt.get_help_record(ctx)[1]

Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2

line 1637why, one step back
1637 assert "[default: " not in message

Merge branch 'main' into stableDavid Lordbefore adoption81a482f

lines 1638–1639why, one step back
1638
1639

Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2

lines 1640–1642why, one step back
1640def test_show_default_string(runner):
1641 """When show_default is a string show that value as default."""1642 opt = click.Option(["--limit"], show_default="unlimited")

Merge branch 'main' into stableDavid Lordbefore adoption81a482f

line 1643why, one step back
1643 ctx = click.Context(click.Command("cli"))

Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2

line 1644why, one step back
1644 assert opt.get_help_extra(ctx) == {"default": "(unlimited)"}

Merge branch 'main' into stableDavid Lordbefore adoption81a482f

line 1645why, one step back
1645 message = opt.get_help_record(ctx)[1]

Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2

line 1646why, one step back
1646 assert "[default: (unlimited)]" in message

Merge branch 'main' into stableDavid Lordbefore adoption81a482f

lines 1647–1648why, one step back
1647
1648

Show default string in prompt (#3328)Rowlando13before adoption76552ff

lines 1649–1661why, one step back
1649def test_string_show_default_shows_custom_string_in_prompt(runner):
1650 @click.command()1651 @click.option(1652 "--arg1", show_default="custom", prompt=True, default="my-default-value"1653 )1654 def cmd(arg1):1655 pass16561657 result = runner.invoke(cmd, input="my-input", standalone_mode=False)1658 assert "(custom)" in result.output1659 assert "my-default-value" not in result.output16601661

Fix speculative empty string check (#3299)Rowlando13before adoption1458800

lines 1662–1688why, one step back
1662class _StrictEq:
1663 """Object whose ``__eq__`` raises on string comparison (like semver.Version)."""16641665 def __eq__(self, other):1666 if isinstance(other, str):1667 raise ValueError("cannot compare to string")1668 return NotImplemented16691670 def __str__(self):1671 return "strict"167216731674@pytest.mark.parametrize(1675 ("default", "expected"),1676 [1677 ("", '[default: ""]'),1678 (_StrictEq(), "[default: strict]"),1679 ],1680 ids=["empty-string", "non-string-comparable-object"],1681)1682def test_show_default_with_empty_string(runner, default, expected):1683 """The empty-string check in help rendering must not break on objects1684 whose ``__eq__`` raises for string operands.16851686 Regression test for https://github.com/pallets/click/issues/3298.1687 """1688 opt = click.Option(["--limit"], default=default, show_default=True)

Merge branch 'main' into stableDavid Lordbefore adoption81a482f

lines 1689–1690why, one step back
1689 ctx = click.Context(click.Command("cli"))
1690 message = opt.get_help_record(ctx)[1]

Fix speculative empty string check (#3299)Rowlando13before adoption1458800

line 1691why, one step back
1691 assert expected in message

Merge branch 'main' into stableDavid Lordbefore adoption81a482f

lines 1692–1693why, one step back
1692
1693

Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2

lines 1694–1696why, one step back
1694def test_do_not_show_no_default(runner):
1695 """When show_default is True and no default is set do not show None."""1696 opt = click.Option(["--limit"], show_default=True)

Merge branch 'main' into stableDavid Lordbefore adoption81a482f

lines 1697–1699why, one step back
1697 ctx = click.Context(click.Command("cli"))
1698 assert opt.get_help_extra(ctx) == {}1699 message = opt.get_help_record(ctx)[1]

Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2

line 1700why, one step back
1700 assert "[default: None]" not in message

Merge branch 'main' into stableDavid Lordbefore adoption81a482f

lines 1701–1702why, one step back
1701
1702

Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2

lines 1703–1705why, one step back
1703def test_do_not_show_default_empty_multiple():
1704 """When show_default is True and multiple=True is set, it should not1705 print empty default value in --help output.

Merge branch 'main' into stableDavid Lordbefore adoption81a482f

line 1706why, one step back
1706 """

Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2

line 1707why, one step back
1707 opt = click.Option(["-a"], multiple=True, help="values", show_default=True)

Merge branch 'main' into stableDavid Lordbefore adoption81a482f

lines 1708–1710why, one step back
1708 ctx = click.Context(click.Command("cli"))
1709 assert opt.get_help_extra(ctx) == {}1710 message = opt.get_help_record(ctx)[1]

Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2

line 1711why, one step back
1711 assert message == "values"

Merge branch 'main' into stableDavid Lordbefore adoption81a482f

lines 1712–1714why, one step back
1712
17131714@pytest.mark.parametrize(

Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2

line 1715why, one step back
1715 ("ctx_value", "opt_value", "extra_value", "expect"),

Merge branch 'main' into stableDavid Lordbefore adoption81a482f

line 1716why, one step back
1716 [

Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2

lines 1717–1726why, one step back
1717 (None, None, {}, False),
1718 (None, False, {}, False),1719 (None, True, {"default": "1"}, True),1720 (False, None, {}, False),1721 (False, False, {}, False),1722 (False, True, {"default": "1"}, True),1723 (True, None, {"default": "1"}, True),1724 (True, False, {}, False),1725 (True, True, {"default": "1"}, True),1726 (False, "one", {"default": "(one)"}, True),

Merge branch 'main' into stableDavid Lordbefore adoption81a482f

lines 1727–1728why, one step back
1727 ],
1728)

Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2

lines 1729–1734why, one step back
1729def test_show_default_precedence(ctx_value, opt_value, extra_value, expect):
1730 ctx = click.Context(click.Command("test"), show_default=ctx_value)1731 opt = click.Option("-a", default=1, help="value", show_default=opt_value)1732 assert opt.get_help_extra(ctx) == extra_value1733 help = opt.get_help_record(ctx)[1]1734 assert ("default:" in help) is expect

Merge branch 'main' into stableDavid Lordbefore adoption81a482f

lines 1735–1737why, one step back
1735
17361737@pytest.mark.parametrize(

Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2

line 1738why, one step back
1738 ("args", "expect"),

Merge branch 'main' into stableDavid Lordbefore adoption81a482f

line 1739why, one step back
1739 [

Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2

lines 1740–1753why, one step back
1740 (None, (None, None, ())),
1741 (["--opt"], ("flag", None, ())),1742 (["--opt", "-a", 42], ("flag", "42", ())),1743 (["--opt", "test", "-a", 42], ("test", "42", ())),1744 (["--opt=test", "-a", 42], ("test", "42", ())),1745 (["-o"], ("flag", None, ())),1746 (["-o", "-a", 42], ("flag", "42", ())),1747 (["-o", "test", "-a", 42], ("test", "42", ())),1748 (["-otest", "-a", 42], ("test", "42", ())),1749 (["a", "b", "c"], (None, None, ("a", "b", "c"))),1750 (["--opt", "a", "b", "c"], ("a", None, ("b", "c"))),1751 (["--opt", "test"], ("test", None, ())),1752 (["-otest", "a", "b", "c"], ("test", None, ("a", "b", "c"))),1753 (["--opt=test", "a", "b", "c"], ("test", None, ("a", "b", "c"))),

Merge branch 'main' into stableDavid Lordbefore adoption81a482f

lines 1754–1755why, one step back
1754 ],
1755)

Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2

line 1756why, one step back
1756def test_option_with_optional_value(runner, args, expect):

Merge branch 'main' into stableDavid Lordbefore adoption81a482f

line 1757why, one step back
1757 @click.command()

Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2

lines 1758–1762why, one step back
1758 @click.option("-o", "--opt", is_flag=False, flag_value="flag")
1759 @click.option("-a")1760 @click.argument("b", nargs=-1)1761 def cli(opt, a, b):1762 return opt, a, b

Merge branch 'main' into stableDavid Lordbefore adoption81a482f

line 1763why, one step back
1763

Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2

lines 1764–1765why, one step back
1764 result = runner.invoke(cli, args, standalone_mode=False, catch_exceptions=False)
1765 assert result.return_value == expect

Merge branch 'main' into stableDavid Lordbefore adoption81a482f

lines 1766–1767why, one step back
1766
1767

Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2

line 1768why, one step back
1768def test_multiple_option_with_optional_value(runner):

Merge branch 'main' into stableDavid Lordbefore adoption81a482f

lines 1769–1770why, one step back
1769 cli = click.Command(
1770 "cli",

Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2

lines 1771–1776why, one step back
1771 params=[
1772 click.Option(["-f"], is_flag=False, flag_value="flag", multiple=True),1773 click.Option(["-a"]),1774 click.Argument(["b"], nargs=-1),1775 ],1776 callback=lambda **kwargs: kwargs,

Merge branch 'main' into stableDavid Lordbefore adoption81a482f

lines 1777–1778why, one step back
1777 )
1778 result = runner.invoke(

Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2

lines 1779–1782why, one step back
1779 cli,
1780 ["-f", "-f", "other", "-f", "-a", "1", "a", "b"],1781 standalone_mode=False,1782 catch_exceptions=False,

Merge branch 'main' into stableDavid Lordbefore adoption81a482f

line 1783why, one step back
1783 )

Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2

lines 1784–1788why, one step back
1784 assert result.return_value == {
1785 "f": ("flag", "other", "flag"),1786 "a": "1",1787 "b": ("a", "b"),1788 }

Merge branch 'main' into stableDavid Lordbefore adoption81a482f

lines 1789–1790why, one step back
1789
1790

Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2

lines 1791–1792why, one step back
1791def test_type_from_flag_value():
1792 param = click.Option(["-a", "x"], default=True, flag_value=4)

Merge branch 'main' into stableDavid Lordbefore adoption81a482f

line 1793why, one step back
1793 assert param.type is click.INT

Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2

line 1794why, one step back
1794 param = click.Option(["-b", "x"], flag_value=8)

Merge branch 'main' into stableDavid Lordbefore adoption81a482f

line 1795why, one step back
1795 assert param.type is click.INT

Auto-detect `type=UNPROCESSED` when `flag_value` has non-basic types (#3363)Rowlando13before adoptionb03f211

lines 1796–1800why, one step back
1796 # Non-basic types auto-detect as UNPROCESSED to avoid stringification.
1797 param = click.Option(["-c", "x"], flag_value=EngineType.OSS)1798 assert param.type is click.UNPROCESSED1799 param = click.Option(["-d", "x"], flag_value=frozenset())1800 assert param.type is click.UNPROCESSED

Stable (#3851)Rowlando13before adoption6aabf09

lines 1801–1804why, one step back
1801 param = click.Option(["-e", "x"], flag_value=set())
1802 assert param.type is click.UNPROCESSED1803 param = click.Option(["-f", "x"], flag_value={})1804 assert param.type is click.UNPROCESSED

Merge branch 'main' into stableDavid Lordbefore adoption81a482f

lines 1805–1868why, one step back
1805
18061807@pytest.mark.parametrize(1808 ("opt_params", "args", "expected"),1809 [1810 # The type passed to the option is responsible to converting the value, whether1811 # we pass the option flag or not.1812 ({"type": bool}, [], False),1813 ({"type": bool}, ["--foo"], True),1814 ({"type": click.BOOL}, [], False),1815 ({"type": click.BOOL}, ["--foo"], True),1816 ({"type": str}, [], None),1817 ({"type": str}, ["--foo"], "True"),1818 # Default value is given as-is to the --foo option when it is not passed,1819 # whatever the type. Now if --foo is passed, the value is always True, whatever1820 # the type. In both case the type of the option is responsible for the1821 # conversion of the value.1822 ({"type": bool, "default": True}, [], True),1823 ({"type": bool, "default": True}, ["--foo"], True),1824 ({"type": bool, "default": False}, [], False),1825 ({"type": bool, "default": False}, ["--foo"], True),1826 # ({"type": bool, "default": "foo"}, [], "foo"),1827 ({"type": bool, "default": "foo"}, ["--foo"], True),1828 ({"type": bool, "default": None}, [], None),1829 ({"type": bool, "default": None}, ["--foo"], True),1830 ({"type": click.BOOL, "default": True}, [], True),1831 ({"type": click.BOOL, "default": True}, ["--foo"], True),1832 ({"type": click.BOOL, "default": False}, [], False),1833 ({"type": click.BOOL, "default": False}, ["--foo"], True),1834 # ({"type": click.BOOL, "default": "foo"}, [], "foo"),1835 ({"type": click.BOOL, "default": "foo"}, ["--foo"], True),1836 ({"type": click.BOOL, "default": None}, [], None),1837 ({"type": click.BOOL, "default": None}, ["--foo"], True),1838 ({"type": str, "default": True}, [], "True"),1839 ({"type": str, "default": True}, ["--foo"], "True"),1840 ({"type": str, "default": False}, [], "False"),1841 ({"type": str, "default": False}, ["--foo"], "True"),1842 ({"type": str, "default": "foo"}, [], "foo"),1843 ({"type": str, "default": "foo"}, ["--foo"], "True"),1844 ({"type": str, "default": None}, [], None),1845 ({"type": str, "default": None}, ["--foo"], "True"),1846 # Flag value is given as-is to the --foo option when it is passed, then1847 # converted by the option type.1848 ({"type": bool, "flag_value": True}, [], False),1849 ({"type": bool, "flag_value": True}, ["--foo"], True),1850 ({"type": bool, "flag_value": False}, [], False),1851 ({"type": bool, "flag_value": False}, ["--foo"], False),1852 ({"type": bool, "flag_value": None}, [], False),1853 ({"type": bool, "flag_value": None}, ["--foo"], None),1854 ({"type": click.BOOL, "flag_value": True}, [], False),1855 ({"type": click.BOOL, "flag_value": True}, ["--foo"], True),1856 ({"type": click.BOOL, "flag_value": False}, [], False),1857 ({"type": click.BOOL, "flag_value": False}, ["--foo"], False),1858 ({"type": click.BOOL, "flag_value": None}, [], False),1859 ({"type": click.BOOL, "flag_value": None}, ["--foo"], None),1860 ({"type": str, "flag_value": True}, [], None),1861 ({"type": str, "flag_value": True}, ["--foo"], "True"),1862 ({"type": str, "flag_value": False}, [], None),1863 ({"type": str, "flag_value": False}, ["--foo"], "False"),1864 ({"type": str, "flag_value": "foo"}, [], None),1865 ({"type": str, "flag_value": "foo"}, ["--foo"], "foo"),1866 ({"type": str, "flag_value": None}, [], None),1867 ({"type": str, "flag_value": None}, ["--foo"], None),1868 # Not passing --foo returns the default value as-is, in its Python type, then

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

lines 1869–1872why, one step back
1869 # converted by the option type. For boolean flags, default=True is a literal
1870 # value, not a sentinel meaning "activate flag". So it is NOT substituted with1871 # flag_value. See: https://github.com/pallets/click/issues/31111872 # https://github.com/pallets/click/pull/3239

Merge branch 'main' into stableDavid Lordbefore adoption81a482f

line 1873why, one step back
1873 ({"type": bool, "default": True, "flag_value": True}, [], True),

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

line 1874why, one step back
1874 ({"type": bool, "default": True, "flag_value": False}, [], True),

Merge branch 'main' into stableDavid Lordbefore adoption81a482f

lines 1875–1910why, one step back
1875 ({"type": bool, "default": False, "flag_value": True}, [], False),
1876 ({"type": bool, "default": False, "flag_value": False}, [], False),1877 ({"type": bool, "default": None, "flag_value": True}, [], None),1878 ({"type": bool, "default": None, "flag_value": False}, [], None),1879 ({"type": str, "default": True, "flag_value": True}, [], "True"),1880 ({"type": str, "default": True, "flag_value": False}, [], "False"),1881 ({"type": str, "default": False, "flag_value": True}, [], "False"),1882 ({"type": str, "default": False, "flag_value": False}, [], "False"),1883 ({"type": str, "default": "foo", "flag_value": True}, [], "foo"),1884 ({"type": str, "default": "foo", "flag_value": False}, [], "foo"),1885 ({"type": str, "default": "foo", "flag_value": "bar"}, [], "foo"),1886 ({"type": str, "default": "foo", "flag_value": None}, [], "foo"),1887 ({"type": str, "default": None, "flag_value": True}, [], None),1888 ({"type": str, "default": None, "flag_value": False}, [], None),1889 # Passing --foo returns the flag_value that was explicitly set by the user,1890 # with its Python type, but still converted by the option type.1891 ({"type": bool, "default": True, "flag_value": True}, ["--foo"], True),1892 ({"type": bool, "default": True, "flag_value": False}, ["--foo"], False),1893 ({"type": bool, "default": False, "flag_value": True}, ["--foo"], True),1894 ({"type": bool, "default": False, "flag_value": False}, ["--foo"], False),1895 ({"type": bool, "default": None, "flag_value": True}, ["--foo"], True),1896 ({"type": bool, "default": None, "flag_value": False}, ["--foo"], False),1897 ({"type": str, "default": True, "flag_value": True}, ["--foo"], "True"),1898 ({"type": str, "default": True, "flag_value": False}, ["--foo"], "False"),1899 ({"type": str, "default": False, "flag_value": True}, ["--foo"], "True"),1900 ({"type": str, "default": False, "flag_value": False}, ["--foo"], "False"),1901 ({"type": str, "default": "foo", "flag_value": True}, ["--foo"], "True"),1902 ({"type": str, "default": "foo", "flag_value": False}, ["--foo"], "False"),1903 ({"type": str, "default": "foo", "flag_value": "bar"}, ["--foo"], "bar"),1904 ({"type": str, "default": "foo", "flag_value": None}, ["--foo"], None),1905 ({"type": str, "default": None, "flag_value": True}, ["--foo"], "True"),1906 ({"type": str, "default": None, "flag_value": False}, ["--foo"], "False"),1907 ],1908)1909def test_flag_value_and_default(runner, opt_params, args, expected):1910 @click.command()

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

line 1911why, one step back
1911 @click.option("--foo", is_flag=True, **opt_params)

Merge branch 'main' into stableDavid Lordbefore adoption81a482f

lines 1912–1929why, one step back
1912 def cmd(foo):
1913 click.echo(repr(foo), nl=False)19141915 result = runner.invoke(cmd, args)1916 assert result.output == repr(expected)191719181919@pytest.mark.parametrize(1920 ("args", "opts"),1921 [1922 ([], {"type": bool, "default": "foo"}),1923 ([], {"type": click.BOOL, "default": "foo"}),1924 (["--foo"], {"type": bool, "flag_value": "foo"}),1925 (["--foo"], {"type": click.BOOL, "flag_value": "foo"}),1926 ],1927)1928def test_invalid_flag_definition(runner, args, opts):1929 @click.command()

Fix flag value set when is_flag=True and type is provided (#2930)David Lordbefore adoption884af5c

line 1930why, one step back
1930 @click.option("--foo", is_flag=True, **opts)

Merge branch 'main' into stableDavid Lordbefore adoption81a482f

lines 1931–1935why, one step back
1931 def cmd(foo):
1932 click.echo(foo)19331934 result = runner.invoke(cmd, args)1935 assert (

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

line 1936why, one step back
1936 "Error: Invalid value for '--foo': 'foo' is not a valid boolean"

Merge branch 'main' into stableDavid Lordbefore adoption81a482f

lines 1937–1982why, one step back
1937 in result.output
1938 )193919401941@pytest.mark.parametrize(1942 ("default", "args", "expected"),1943 # These test cases are similar to the ones in1944 # tests/test_basic.py::test_flag_value_dual_options, so keep them in sync.1945 (1946 # Each option is returning its own flag_value, whatever the default is.1947 (True, ["--js"], "js"),1948 (True, ["--xml"], "xml"),1949 (False, ["--js"], "js"),1950 (False, ["--xml"], "xml"),1951 (None, ["--js"], "js"),1952 (None, ["--xml"], "xml"),1953 (UNSET, ["--js"], "js"),1954 (UNSET, ["--xml"], "xml"),1955 # Check that the last option wins when both are specified.1956 (True, ["--js", "--xml"], "xml"),1957 (True, ["--xml", "--js"], "js"),1958 # Check that the default is returned as-is when no option is specified.1959 ("js", [], "js"),1960 ("xml", [], "xml"),1961 ("jS", [], "jS"),1962 ("xMl", [], "xMl"),1963 (" ᕕ( ᐛ )ᕗ ", [], " ᕕ( ᐛ )ᕗ "),1964 (None, [], None),1965 # Special case: UNSET is not provided as-is to the callback, but normalized to1966 # None.1967 (UNSET, [], None),1968 # Special case: if default=True and flag_value is set, the value returned is the1969 # flag_value, not the True Python value itself.1970 (True, [], "js"),1971 # Non-string defaults are process as strings by the default Parameter's type.1972 (False, [], "False"),1973 (42, [], "42"),1974 (12.3, [], "12.3"),1975 ),1976)1977def test_default_dual_option_callback(runner, default, args, expected):1978 """Check how default is processed by the callback when options compete for the same1979 variable name.19801981 Reproduction of the issue reported in1982 https://github.com/pallets/click/pull/3030#discussion_r2271571819

Add cross-reference to tests from `test_defaults.py` (#3223)Rowlando13before adoptionfa7f035

lines 1983–1988why, one step back
1983
1984 .. hint::1985 Similar to ``test_basic.py::test_flag_value_dual_options``.19861987 ``test_defaults.py::test_shared_param_prefers_first_default``1988 is a smoke-test complement that exercises both default placements.

Merge branch 'main' into stableDavid Lordbefore adoption81a482f

lines 1989–2006why, one step back
1989 """
19901991 def _my_func(ctx, param, value):1992 # Print the value received by the callback as-is, so we can check for it.1993 return f"Callback value: {value!r}"19941995 @click.command()1996 @click.option("--js", "fmt", flag_value="js", callback=_my_func, default=default)1997 @click.option("--xml", "fmt", flag_value="xml", callback=_my_func)1998 def main(fmt):1999 click.secho(fmt, nl=False)2000

The first 2,000 of 3,945 lines.