test_options.py
This file
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 back1import enumMake faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2
lines 2–3why, one step back2import os3import reFix rendering of Enum choices' default (#3004)Rowlando13before adoptioneaf33a9
line 4why, one step back4import sysMerge branch 'main' into stableDavid Lordbefore adoption81a482f
line 5why, one step back5import tempfileDeprecate parameter names that are: not a Python identifier, or a Python Keyword, or not lower-cased (#3866)Rowlando13before adoption06b2a67
lines 6–7why, one step back6import unicodedata7import warningsMerge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 8–10why, one step back8from contextlib import nullcontext9from typing import Literal10Fix rendering of Enum choices' default (#3004)Rowlando13before adoptioneaf33a9
lines 11–12why, one step back11if sys.version_info < (3, 11):12 enum.StrEnum = enum.Enum # type: ignore[assignment]Merge branch 'main' into stableDavid Lordbefore adoption81a482f
line 13why, one step back13Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2
line 14why, one step back14import pytestMerge branch 'main' into stableDavid Lordbefore adoption81a482f
line 15why, one step back15Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2
lines 16–17why, one step back16import click17from click import OptionMerge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 18–19why, one step back18from click import UNPROCESSED19from click._utils import UNSET20from click.parser import _split_optFix handling of options which set both is_flag=False and flag_value=... (#3152)Rowlando13before adoptionebcd548
line 21why, one step back21from click.testing import CliRunnerMerge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 22–23why, one step back2223Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2
line 24why, one step back24def test_prefixes(runner):Merge branch 'main' into stableDavid Lordbefore adoption81a482f
line 25why, one step back25 @click.command()Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2
lines 26–29why, one step back26 @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 back30Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2
line 31why, one step back31 result = runner.invoke(cli, ["++foo", "--bar"])Merge branch 'main' into stableDavid Lordbefore adoption81a482f
line 32why, one step back32 assert not result.exceptionMake faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2
line 33why, one step back33 assert result.output == "foo=True bar=True\n"Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 34–35why, one step back3435 result = runner.invoke(cli, ["--help"])Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2
lines 36–37why, one step back36 assert re.search(r"\+\+foo\s+das foo", result.output) is not None37 assert re.search(r"--bar\s+das bar", result.output) is not NoneMerge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 38–39why, one step back3839Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2
lines 40–42why, one step back40def 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 back43Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2
lines 44–47why, one step back44 message = str(exc_info.value)45 assert "name was passed (foo)" in message46 assert "declare an argument" in message47 assert "'--foo'" in messageMerge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 48–50why, one step back484950@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 back51def test_deprecated_usage(runner, deprecated):Merge branch 'main' into stableDavid Lordbefore adoption81a482f
line 52why, one step back52 @click.command()Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2
line 53why, one step back53 @click.option("--foo", default="bar", deprecated=deprecated)Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 54–57why, one step back54 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 back58 assert "(DEPRECATED" in result.outputMerge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 59–63why, one step back5960 if isinstance(deprecated, str):61 assert deprecated in result.output6263Merge stable into main (#3521)Rowlando13before adoption1a994ad
lines 64–77why, one step back64@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] == expected7677Stable (#3851)Rowlando13before adoption6aabf09
lines 78–101why, one step back78@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"128129Merge branch 'main' into stableDavid Lordbefore adoption81a482f
line 130why, one step back130@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 back131def test_deprecated_warning(runner, deprecated):Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 132–133why, one step back132 @click.command()133 @click.option(Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2
line 134why, one step back134 "--my-option", required=False, deprecated=deprecated, default="default option"Merge branch 'main' into stableDavid Lordbefore adoption81a482f
line 135why, one step back135 )Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2
lines 136–137why, one step back136 def cli(my_option: str):137 click.echo(f"{my_option}")Merge branch 'main' into stableDavid Lordbefore adoption81a482f
line 138why, one step back138Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2
line 139why, one step back139 # defaults should not give a deprecated warningMerge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 140–141why, one step back140 result = runner.invoke(cli, [])141 assert result.exit_code == 0, result.outputMake faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2
line 142why, one step back142 assert "is deprecated" not in result.outputMerge branch 'main' into stableDavid Lordbefore adoption81a482f
line 143why, one step back143Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2
line 144why, one step back144 result = runner.invoke(cli, ["--my-option", "hello"])Merge branch 'main' into stableDavid Lordbefore adoption81a482f
line 145why, one step back145 assert result.exit_code == 0, result.output146 assert "option '--my-option' is deprecated" in result.outputMerge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 147–151why, one step back147148 if isinstance(deprecated, str):149 assert deprecated in result.output150151Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2
lines 152–154why, one step back152def 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 back155156Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2
lines 157–159why, one step back157def 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 back160161Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2
line 162why, one step back162def test_invalid_nargs(runner):Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 163–165why, one step back163 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 back166 @click.option("--foo", nargs=-1)167 def cli(foo):168 passMerge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 169–170why, one step back169170Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2
line 171why, one step back171def test_nargs_tup_composite_mult(runner):Merge branch 'main' into stableDavid Lordbefore adoption81a482f
line 172why, one step back172 @click.command()Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2
lines 173–176why, one step back173 @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 back177Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2
line 178why, one step back178 result = runner.invoke(copy, ["--item", "peter", "1", "--item", "max", "2"])Merge branch 'main' into stableDavid Lordbefore adoption81a482f
line 179why, one step back179 assert not result.exceptionMake faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2
line 180why, one step back180 assert result.output.splitlines() == ["name=peter id=1", "name=max id=2"]Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 181–182why, one step back181182Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2
line 183why, one step back183def test_counting(runner):Merge branch 'main' into stableDavid Lordbefore adoption81a482f
line 184why, one step back184 @click.command()Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2
lines 185–187why, one step back185 @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 back188Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2
line 189why, one step back189 result = runner.invoke(cli, ["-vvv"])Merge branch 'main' into stableDavid Lordbefore adoption81a482f
line 190why, one step back190 assert not result.exceptionMake faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2
line 191why, one step back191 assert result.output == "verbosity=3\n"Merge branch 'main' into stableDavid Lordbefore adoption81a482f
line 192why, one step back192Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2
line 193why, one step back193 result = runner.invoke(cli, ["-vvvv"])Merge branch 'main' into stableDavid Lordbefore adoption81a482f
line 194why, one step back194 assert result.exceptionMake faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2
line 195why, one step back195 assert "Invalid value for '-v': 4 is not in the range 0<=x<=3." in result.outputMerge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 196–198why, one step back196197 result = runner.invoke(cli, [])198 assert not result.exceptionMake faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2
line 199why, one step back199 assert result.output == "verbosity=0\n"Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 200–201why, one step back200201 result = runner.invoke(cli, ["--help"])Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2
line 202why, one step back202 assert re.search(r"-v\s+Verbosity", result.output) is not NoneMerge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 203–204why, one step back203204Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2
lines 205–206why, one step back205@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 back207 @click.command()208 def cli():209 pass210Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2
line 211why, one step back211 result = runner.invoke(cli, [unknown_flag])Merge branch 'main' into stableDavid Lordbefore adoption81a482f
line 212why, one step back212 assert result.exceptionAdd NoSuchCommand exception with suggestions for misspelled commands (#3228)RC2215before adoption831c8f0
line 213why, one step back213 assert f"No such option '{unknown_flag}'." in result.outputMerge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 214–216why, one step back214215216@pytest.mark.parametrize(Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2
line 217why, one step back217 ("value", "expect"),Merge branch 'main' into stableDavid Lordbefore adoption81a482f
line 218why, one step back218 [Add NoSuchCommand exception with suggestions for misspelled commands (#3228)RC2215before adoption831c8f0
lines 219–221why, one step back219 ("--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 back222 ],223)Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2
line 224why, one step back224def test_suggest_possible_options(runner, value, expect):Merge branch 'main' into stableDavid Lordbefore adoption81a482f
line 225why, one step back225 cli = click.Command(Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2
line 226why, one step back226 "cli", params=[click.Option(["--bound"]), click.Option(["--count"])]Merge branch 'main' into stableDavid Lordbefore adoption81a482f
line 227why, one step back227 )Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2
lines 228–229why, one step back228 result = runner.invoke(cli, [value])229 assert expect in result.outputMerge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 230–231why, one step back230231Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2
line 232why, one step back232def test_multiple_required(runner):Merge branch 'main' into stableDavid Lordbefore adoption81a482f
line 233why, one step back233 @click.command()Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2
lines 234–236why, one step back234 @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 back237Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2
line 238why, one step back238 result = runner.invoke(cli, ["-m", "foo", "-mbar"])Merge branch 'main' into stableDavid Lordbefore adoption81a482f
line 239why, one step back239 assert not result.exceptionMake faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2
line 240why, one step back240 assert result.output == "foo\nbar\n"Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 241–243why, one step back241242 result = runner.invoke(cli, [])243 assert result.exceptionMake faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2
line 244why, one step back244 assert "Error: Missing option '-m' / '--message'." in result.outputMerge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 245–294why, one step back245246247@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 back295 """Comprehensive check of default-value processing for options with296 ``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 """306Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 307–463why, one step back307 @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.stderr462463Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2
lines 464–465why, one step back464@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 back466 @click.command()Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2
lines 467–469why, one step back467 @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 back470Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2
lines 471–473why, one step back471 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 back474475Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2
line 476why, one step back476def test_multiple_envvar(runner):Merge branch 'main' into stableDavid Lordbefore adoption81a482f
line 477why, one step back477 @click.command()Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2
line 478why, one step back478 @click.option("--arg", multiple=True)Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 479–482why, one step back479 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 back483 cmd, [], auto_envvar_prefix="TEST", env={"TEST_ARG": "foo bar baz"}Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 484–487why, one step back484 )485 assert not result.exception486 assert result.output == "foo|bar|baz\n"487Stable (#3851)Rowlando13before adoption6aabf09
lines 488–494why, one step back488489def 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 """494Merge branch 'main' into stableDavid Lordbefore adoption81a482f
line 495why, one step back495 @click.command()Stable (#3851)Rowlando13before adoption6aabf09
lines 496–505why, one step back496 @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 back506 @click.option("--arg", multiple=True, envvar="X")Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 507–509why, one step back507 def cmd(arg):508 click.echo("|".join(arg))509Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2
line 510why, one step back510 result = runner.invoke(cmd, [], env={"X": "foo bar baz"})Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 511–514why, one step back511 assert not result.exception512 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 back515 @click.option("--arg", multiple=True, type=click.Path())Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 516–519why, one step back516 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 back520 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 back524 )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 back532 # 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 back547 ("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 back553 # 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 back558 ("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 back560 ("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 back574 ("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 back576 (" SHOUT ", "True", False),577 (" SHOUT ", "False", False),Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 578–581why, one step back578 (" 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 back582 # Same for random and reverse environment variable names: they are not583 # 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 back587 ("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 back609 "tr ue", # codespell:ignore ueMerge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 610–638why, one step back610 "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 back639 f"Invalid value for '--shout': {value!r} is not a valid boolean."Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 640–643why, one step back640 in result.output641 )642643Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2
line 644why, one step back644def test_multiple_default_help(runner):Merge branch 'main' into stableDavid Lordbefore adoption81a482f
line 645why, one step back645 @click.command()Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2
lines 646–648why, one step back646 @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 back649 pass650651 result = runner.invoke(cmd, ["--help"])652 assert not result.exceptionMake faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2
lines 653–654why, one step back653 assert "foo, bar" in result.output654 assert "1, 2" in result.outputMerge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 655–656why, one step back655656Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2
line 657why, one step back657def test_show_default_default_map(runner):Merge branch 'main' into stableDavid Lordbefore adoption81a482f
line 658why, one step back658 @click.command()Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2
line 659why, one step back659 @click.option("--arg", default="a", show_default=True)Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 660–662why, one step back660 def cmd(arg):661 click.echo(arg)662Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2
line 663why, one step back663 result = runner.invoke(cmd, ["--help"], default_map={"arg": "b"})Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 664–665why, one step back664665 assert not result.exceptionMake faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2
line 666why, one step back666 assert "[default: b]" in result.outputMerge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 667–668why, one step back667668Stable (#3851)Rowlando13before adoption6aabf09
lines 669–687why, one step back669def 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.output686687Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2
lines 688–690why, one step back688def test_multiple_default_type():689 opt = click.Option(["-a"], multiple=True, default=(1, 2))690 assert opt.nargs == 1Merge branch 'main' into stableDavid Lordbefore adoption81a482f
line 691why, one step back691 assert opt.multipleMake faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2
line 692why, one step back692 assert opt.type is click.INTMerge branch 'main' into stableDavid Lordbefore adoption81a482f
line 693why, one step back693 ctx = click.Context(click.Command("test"))Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2
line 694why, one step back694 assert opt.get_default(ctx) == (1, 2)Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 695–696why, one step back695696Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2
lines 697–699why, one step back697def test_multiple_default_composite_type():698 opt = click.Option(["-a"], multiple=True, default=[(1, "a")])699 assert opt.nargs == 2Merge branch 'main' into stableDavid Lordbefore adoption81a482f
line 700why, one step back700 assert opt.multipleMake faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2
lines 701–702why, one step back701 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 back703 ctx = click.Context(click.Command("test"))Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2
line 704why, one step back704 assert opt.type_cast_value(ctx, opt.get_default(ctx)) == ((1, "a"),)Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 705–706why, one step back705706Stable (#3851)Rowlando13before adoption6aabf09
lines 707–729why, one step back707@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.STRING728729Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2
line 730why, one step back730def test_parse_multiple_default_composite_type(runner):Merge branch 'main' into stableDavid Lordbefore adoption81a482f
line 731why, one step back731 @click.command()Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2
lines 732–734why, one step back732 @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 back735 click.echo(a)Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2
line 736why, one step back736 click.echo(b)Merge branch 'main' into stableDavid Lordbefore adoption81a482f
line 737why, one step back737Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2
lines 738–739why, one step back738 # 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 back740 result = runner.invoke(cmd)Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2
line 741why, one step back741 assert result.output == "('a', 'b')\n((1, 'a'),)\n"Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 742–743why, one step back742743Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2
line 744why, one step back744def test_dynamic_default_help_unset(runner):Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 745–758why, one step back745 @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.outputMake faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2
line 759why, one step back759 assert "(dynamic)" in result.outputMerge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 760–761why, one step back760761Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2
line 762why, one step back762def test_dynamic_default_help_text(runner):Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 763–767why, one step back763 @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 back768 show_default="current user",Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 769–776why, one step back769 )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.outputMake faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2
line 777why, one step back777 assert "(current user)" in result.outputMerge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 778–779why, one step back778779Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2
lines 780–783why, one step back780def test_dynamic_default_help_special_method(runner):781 class Value:782 def __call__(self):783 return 42Merge branch 'main' into stableDavid Lordbefore adoption81a482f
line 784why, one step back784Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2
lines 785–786why, one step back785 def __str__(self):786 return "special value"Merge branch 'main' into stableDavid Lordbefore adoption81a482f
line 787why, one step back787Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2
line 788why, one step back788 opt = click.Option(["-a"], default=Value(), show_default=True)Merge branch 'main' into stableDavid Lordbefore adoption81a482f
line 789why, one step back789 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 back790 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 back792793794@pytest.mark.parametrize(Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2
line 795why, one step back795 ("type", "expect"),Merge branch 'main' into stableDavid Lordbefore adoption81a482f
line 796why, one step back796 [Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2
lines 797–800why, one step back797 (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 back801 ],802)Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2
lines 803–804why, one step back803def 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 back805 context = click.Context(click.Command("test"))Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2
line 806why, one step back806 assert option.get_help_extra(context) == {"default": "2", "range": expect}Merge branch 'main' into stableDavid Lordbefore adoption81a482f
line 807why, one step back807 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 back808 assert expect in resultMerge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 809–810why, one step back809810Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2
lines 811–813why, one step back811def 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 back814 context = click.Context(click.Command("test"))Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2
line 815why, one step back815 assert option.get_help_extra(context) == {}Merge branch 'main' into stableDavid Lordbefore adoption81a482f
line 816why, one step back816 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 back817 assert result == "some words"Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 818–819why, one step back818819Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2
lines 820–822why, one step back820def 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 back823Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2
lines 824–825why, one step back824 Type casting is only applied to defaults in processing, not when825 getting the default value.Merge branch 'main' into stableDavid Lordbefore adoption81a482f
line 826why, one step back826 """Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2
line 827why, one step back827 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 back828 ["--infile"], type=click.File(), default=__file__, show_default=TrueMerge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 829–830why, one step back829 )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 back831 assert option.get_help_extra(context) == {"default": __file__}Merge branch 'main' into stableDavid Lordbefore adoption81a482f
line 832why, one step back832 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 back833 assert __file__ in resultMerge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 834–835why, one step back834835Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2
line 836why, one step back836def test_toupper_envvar_prefix(runner):Merge branch 'main' into stableDavid Lordbefore adoption81a482f
line 837why, one step back837 @click.command()Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2
line 838why, one step back838 @click.option("--arg")Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 839–841why, one step back839 def cmd(arg):840 click.echo(arg)841Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2
line 842why, one step back842 result = runner.invoke(cmd, [], auto_envvar_prefix="test", env={"TEST_ARG": "foo"})Merge branch 'main' into stableDavid Lordbefore adoption81a482f
line 843why, one step back843 assert not result.exceptionMake faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2
line 844why, one step back844 assert result.output == "foo\n"Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 845–846why, one step back845846Deprecate parameter names that are: not a Python identifier, or a Python Keyword, or not lower-cased (#3866)Rowlando13before adoption06b2a67
lines 847–972why, one step back847# 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"971972Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2
line 973why, one step back973def test_nargs_envvar(runner):Merge branch 'main' into stableDavid Lordbefore adoption81a482f
line 974why, one step back974 @click.command()Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2
line 975why, one step back975 @click.option("--arg", nargs=2)Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 976–979why, one step back976 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 back980 cmd, [], auto_envvar_prefix="TEST", env={"TEST_ARG": "foo bar"}Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 981–985why, one step back981 )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 back986 @click.option("--arg", nargs=2, multiple=True)Merge branch 'main' into stableDavid Lordbefore adoption81a482f
line 987why, one step back987 def cmd(arg):Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2
lines 988–989why, one step back988 for item in arg:989 click.echo("|".join(item))Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 990–991why, one step back990991 result = runner.invoke(Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2
line 992why, one step back992 cmd, [], auto_envvar_prefix="TEST", env={"TEST_ARG": "x 1 y 2"}Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 993–994why, one step back993 )994 assert not result.exceptionMake faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2
line 995why, one step back995 assert result.output == "x|1\ny|2\n"Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 996–997why, one step back996997Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2
line 998why, one step back998def test_show_envvar(runner):Merge branch 'main' into stableDavid Lordbefore adoption81a482f
line 999why, one step back999 @click.command()Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2
line 1000why, one step back1000 @click.option("--arg1", envvar="ARG1", show_envvar=True)Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 1001–1005why, one step back1001 def cmd(arg):1002 pass10031004 result = runner.invoke(cmd, ["--help"])1005 assert not result.exceptionMake faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2
line 1006why, one step back1006 assert "ARG1" in result.outputMerge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 1007–1008why, one step back10071008Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2
line 1009why, one step back1009def test_show_envvar_auto_prefix(runner):Merge branch 'main' into stableDavid Lordbefore adoption81a482f
line 1010why, one step back1010 @click.command()Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2
line 1011why, one step back1011 @click.option("--arg1", show_envvar=True)Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 1012–1014why, one step back1012 def cmd(arg):1013 pass1014Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2
line 1015why, one step back1015 result = runner.invoke(cmd, ["--help"], auto_envvar_prefix="TEST")Merge branch 'main' into stableDavid Lordbefore adoption81a482f
line 1016why, one step back1016 assert not result.exceptionMake faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2
line 1017why, one step back1017 assert "TEST_ARG1" in result.outputMerge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 1018–1019why, one step back10181019Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2
lines 1020–1021why, one step back1020def test_show_envvar_auto_prefix_dash_in_command(runner):1021 @click.group()Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 1022–1024why, one step back1022 def cli():1023 pass1024Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2
lines 1025–1027why, one step back1025 @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 back1028 pass1029Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2
line 1030why, one step back1030 result = runner.invoke(cli, ["foo-bar", "--help"], auto_envvar_prefix="TEST")Merge branch 'main' into stableDavid Lordbefore adoption81a482f
line 1031why, one step back1031 assert not result.exceptionMake faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2
line 1032why, one step back1032 assert "TEST_FOO_BAR_BAZ" in result.outputMerge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 1033–1034why, one step back10331034Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2
lines 1035–1036why, one step back1035def test_custom_validation(runner):1036 def validate_pos_int(ctx, param, value):Merge branch 'main' into stableDavid Lordbefore adoption81a482f
line 1037why, one step back1037 if value < 0:Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2
line 1038why, one step back1038 raise click.BadParameter("Value needs to be positive")Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 1039–1041why, one step back1039 return value10401041 @click.command()Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2
line 1042why, one step back1042 @click.option("--foo", callback=validate_pos_int, default=1)Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 1043–1045why, one step back1043 def cmd(foo):1044 click.echo(foo)1045Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2
lines 1046–1047why, one step back1046 result = runner.invoke(cmd, ["--foo", "-1"])1047 assert "Invalid value for '--foo': Value needs to be positive" in result.outputMerge branch 'main' into stableDavid Lordbefore adoption81a482f
line 1048why, one step back1048Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2
line 1049why, one step back1049 result = runner.invoke(cmd, ["--foo", "42"])Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 1050–1052why, one step back1050 assert result.output == "42\n"10511052Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2
lines 1053–1054why, one step back1053def test_callback_validates_prompt(runner, monkeypatch):1054 def validate(ctx, param, value):Merge branch 'main' into stableDavid Lordbefore adoption81a482f
line 1055why, one step back1055 if value < 0:Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2
line 1056why, one step back1056 raise click.BadParameter("should be positive")Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 1057–1060why, one step back10571058 return value10591060 @click.command()Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2
lines 1061–1062why, one step back1061 @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 back1063 click.echo(a)1064Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2
lines 1065–1066why, one step back1065 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 back10671068Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2
line 1069why, one step back1069def test_winstyle_options(runner):Merge branch 'main' into stableDavid Lordbefore adoption81a482f
line 1070why, one step back1070 @click.command()Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2
lines 1071–1073why, one step back1071 @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 back1074Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2
line 1075why, one step back1075 result = runner.invoke(cmd, ["/debug"], help_option_names=["/?"])Merge branch 'main' into stableDavid Lordbefore adoption81a482f
line 1076why, one step back1076 assert result.output == "True\n"Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2
line 1077why, one step back1077 result = runner.invoke(cmd, ["/no-debug"], help_option_names=["/?"])Merge branch 'main' into stableDavid Lordbefore adoption81a482f
line 1078why, one step back1078 assert result.output == "False\n"Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2
line 1079why, one step back1079 result = runner.invoke(cmd, [], help_option_names=["/?"])Merge branch 'main' into stableDavid Lordbefore adoption81a482f
line 1080why, one step back1080 assert result.output == "False\n"Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2
lines 1081–1083why, one step back1081 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.outputMerge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 1084–1085why, one step back10841085Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2
line 1086why, one step back1086def test_legacy_options(runner):Merge branch 'main' into stableDavid Lordbefore adoption81a482f
line 1087why, one step back1087 @click.command()Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2
lines 1088–1090why, one step back1088 @click.option("-whatever")1089 def cmd(whatever):1090 click.echo(whatever)Merge branch 'main' into stableDavid Lordbefore adoption81a482f
line 1091why, one step back1091Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2
line 1092why, one step back1092 result = runner.invoke(cmd, ["-whatever", "42"])Merge branch 'main' into stableDavid Lordbefore adoption81a482f
line 1093why, one step back1093 assert result.output == "42\n"Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2
lines 1094–1095why, one step back1094 result = runner.invoke(cmd, ["-whatever=23"])1095 assert result.output == "23\n"Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 1096–1117why, one step back109610971098@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 back1118 ctx = click.Context(click.Command(""))Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 1119–1130why, one step back1119 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_value11291130Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2
line 1131why, one step back1131def test_missing_required_flag(runner):Merge branch 'main' into stableDavid Lordbefore adoption81a482f
line 1132why, one step back1132 cli = click.Command(Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2
line 1133why, one step back1133 "cli", params=[click.Option(["--on/--off"], is_flag=True, required=True)]Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 1134–1136why, one step back1134 )1135 result = runner.invoke(cli)1136 assert result.exit_code == 2Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2
line 1137why, one step back1137 assert "Error: Missing option '--on'." in result.outputMerge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 1138–1139why, one step back11381139Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2
line 1140why, one step back1140def test_missing_choice(runner):Merge branch 'main' into stableDavid Lordbefore adoption81a482f
line 1141why, one step back1141 @click.command()Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2
line 1142why, one step back1142 @click.option("--foo", type=click.Choice(["foo", "bar"]), required=True)Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 1143–1147why, one step back1143 def cmd(foo):1144 click.echo(foo)11451146 result = runner.invoke(cmd)1147 assert result.exit_code == 2Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2
lines 1148–1152why, one step back1148 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 choicesMerge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 1153–1154why, one step back11531154Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2
line 1155why, one step back1155def test_missing_envvar(runner):Merge branch 'main' into stableDavid Lordbefore adoption81a482f
line 1156why, one step back1156 cli = click.Command(Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2
line 1157why, one step back1157 "cli", params=[click.Option(["--foo"], envvar="bar", required=True)]Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 1158–1163why, one step back1158 )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 back1164 params=[click.Option(["--foo"], envvar="bar", show_envvar=True, required=True)],Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 1165–1167why, one step back1165 )1166 result = runner.invoke(cli)1167 assert result.exit_code == 2Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2
line 1168why, one step back1168 assert "Error: Missing option '--foo' (env var: 'bar')." in result.outputMerge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 1169–1170why, one step back11691170 cli = click.Command(Don't include envvar in error hint when envvar not configured (#2972)Rowlando13before adoption0be2bd3
line 1171why, one step back1171 "cli", params=[click.Option(["--foo"], show_envvar=True, required=True)]Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 1172–1177why, one step back1172 )1173 result = runner.invoke(cli)1174 assert result.exit_code == 21175 assert "Error: Missing option '--foo'." in result.output11761177Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2
line 1178why, one step back1178def test_case_insensitive_choice(runner):Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 1179–1187why, one step back1179 @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"1187Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2
line 1188why, one step back1188 result = runner.invoke(cmd, ["--foo", "oRANGe"])Merge branch 'main' into stableDavid Lordbefore adoption81a482f
line 1189why, one step back1189 assert result.exit_code == 0Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2
line 1190why, one step back1190 assert result.output == "Orange\n"Merge branch 'main' into stableDavid Lordbefore adoption81a482f
line 1191why, one step back1191Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2
line 1192why, one step back1192 result = runner.invoke(cmd, ["--foo", "Apple"])Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 1193–1196why, one step back1193 assert result.exit_code == 01194 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 back1197 @click.option("--foo", type=click.Choice(["Orange", "Apple"]))1198 def cmd2(foo):Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 1199–1200why, one step back1199 click.echo(foo)1200Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2
line 1201why, one step back1201 result = runner.invoke(cmd2, ["--foo", "apple"])Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 1202–1203why, one step back1202 assert result.exit_code == 21203Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2
line 1204why, one step back1204 result = runner.invoke(cmd2, ["--foo", "oRANGe"])Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 1205–1206why, one step back1205 assert result.exit_code == 21206Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2
line 1207why, one step back1207 result = runner.invoke(cmd2, ["--foo", "Apple"])Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 1208–1210why, one step back1208 assert result.exit_code == 012091210Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2
line 1211why, one step back1211def test_case_insensitive_choice_returned_exactly(runner):Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 1212–1221why, one step back1212 @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"12201221Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2
line 1222why, one step back1222def test_option_help_preserve_paragraphs(runner):Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 1223–1224why, one step back1223 @click.command()1224 @click.option(Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2
lines 1225–1228why, one step back1225 "-C",1226 "--config",1227 type=click.Path(),1228 help="""Configuration file to use.Merge branch 'main' into stableDavid Lordbefore adoption81a482f
line 1229why, one step back1229Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2
lines 1230–1232why, one step back1230 If not given, the environment variable CONFIG_FILE is consulted1231 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 back1233 )Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2
line 1234why, one step back1234 def cmd(config):Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 1235–1238why, one step back1235 pass12361237 result = runner.invoke(cmd, ["--help"])1238 assert result.exit_code == 0Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2
line 1239why, one step back1239 i = " " * 21Merge branch 'main' into stableDavid Lordbefore adoption81a482f
line 1240why, one step back1240 assert (Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2
lines 1241–1246why, one step back1241 " -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.outputMerge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 1247–1248why, one step back12471248Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2
lines 1249–1253why, one step back1249def 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 back12541255 @click.command()add codespell pre-commit hook (#3373)David Lordbefore adoption8bd8b4a
line 1256why, one step back1256 @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 back1257 def cmd(testarg):1258 click.echo(testarg)Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 1259–1260why, one step back12591260 result = runner.invoke(cmd)Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2
line 1261why, one step back1261 assert "I am a default" in result.outputadd codespell pre-commit hook (#3373)David Lordbefore adoption8bd8b4a
line 1262why, one step back1262 assert "you won't see me" not in result.outputMerge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 1263–1264why, one step back12631264Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2
line 1265why, one step back1265def test_option_custom_class(runner):Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 1266–1271why, one step back1266 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 back1272 @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 back1273 def cmd(testoption):Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 1274–1276why, one step back1274 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 back1277 assert "I am a help text" in result.outputadd codespell pre-commit hook (#3373)David Lordbefore adoption8bd8b4a
line 1278why, one step back1278 assert "you won't see me" not in result.outputMerge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 1279–1280why, one step back12791280Ensure that `type_cast_value` never gets `UNSET` (#3090)Rowlando13before adoption8d4051e
lines 1281–1316why, one step back1281@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 == 013151316Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2
lines 1317–1318why, one step back1317def 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 back13191320 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")1324add codespell pre-commit hook (#3373)David Lordbefore adoption8bd8b4a
lines 1325–1326why, one step back1325 # 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 back13271328 @click.command()1329 @testoptionMake faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2
line 1330why, one step back1330 def cmd1(testoption):Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 1331–1334why, one step back1331 click.echo(testoption)13321333 @click.command()1334 @testoptionMake faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2
line 1335why, one step back1335 def cmd2(testoption):Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 1336–1337why, one step back1336 click.echo(testoption)1337Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2
lines 1338–1341why, one step back1338 # 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.outputadd codespell pre-commit hook (#3373)David Lordbefore adoption8bd8b4a
line 1342why, one step back1342 assert "you won't see me" not in result.outputMerge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 1343–1344why, one step back13431344Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2
line 1345why, one step back1345@pytest.mark.parametrize("custom_class", (True, False))Merge branch 'main' into stableDavid Lordbefore adoption81a482f
line 1346why, one step back1346@pytest.mark.parametrize(Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2
line 1347why, one step back1347 ("name_specs", "expected"),Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 1348–1349why, one step back1348 (1349 (Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2
lines 1350–1351why, one step back1350 ("-h", "--help"),1351 " -h, --help Show this message and exit.\n",Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 1352–1353why, one step back1352 ),1353 (Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2
lines 1354–1355why, one step back1354 ("-h",),1355 " -h Show this message and exit.\n"Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 1356–1358why, one step back1356 " --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 back1359 ("--help",),Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 1360–1363why, one step back1360 " --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 back1364def 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 back1366 pass1367Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2
lines 1368–1370why, one step back1368 option_attrs = {}1369 if custom_class:1370 option_attrs["cls"] = CustomHelpOptionMerge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 1371–1372why, one step back13711372 @click.command()Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2
lines 1373–1374why, one step back1373 @click.help_option(*name_specs, **option_attrs)1374 def cmd():Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 1375–1376why, one step back1375 pass1376Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2
lines 1377–1381why, one step back1377 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.outputMerge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 1382–1383why, one step back13821383Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2
line 1384why, one step back1384def test_bool_flag_with_type(runner):Merge branch 'main' into stableDavid Lordbefore adoption81a482f
line 1385why, one step back1385 @click.command()Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2
lines 1386–1387why, one step back1386 @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 back1388 pass13891390 result = runner.invoke(cmd)1391 assert not result.exception13921393Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2
line 1394why, one step back1394def test_aliases_for_flags(runner):Merge branch 'main' into stableDavid Lordbefore adoption81a482f
line 1395why, one step back1395 @click.command()Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2
lines 1396–1397why, one step back1396 @click.option("--warnings/--no-warnings", " /-W", default=True)1397 def cli(warnings):Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 1398–1399why, one step back1398 click.echo(warnings)1399Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2
line 1400why, one step back1400 result = runner.invoke(cli, ["--warnings"])Merge branch 'main' into stableDavid Lordbefore adoption81a482f
line 1401why, one step back1401 assert result.output == "True\n"Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2
line 1402why, one step back1402 result = runner.invoke(cli, ["--no-warnings"])Merge branch 'main' into stableDavid Lordbefore adoption81a482f
line 1403why, one step back1403 assert result.output == "False\n"Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2
line 1404why, one step back1404 result = runner.invoke(cli, ["-W"])Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 1405–1407why, one step back1405 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 back1408 @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 back1410 click.echo(warnings)1411Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2
line 1412why, one step back1412 result = runner.invoke(cli_alt, ["--warnings"])Merge branch 'main' into stableDavid Lordbefore adoption81a482f
line 1413why, one step back1413 assert result.output == "True\n"Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2
line 1414why, one step back1414 result = runner.invoke(cli_alt, ["--no-warnings"])Merge branch 'main' into stableDavid Lordbefore adoption81a482f
line 1415why, one step back1415 assert result.output == "False\n"Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2
line 1416why, one step back1416 result = runner.invoke(cli_alt, ["-w"])Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 1417–1420why, one step back1417 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 back1421 "option_args,expected",Merge branch 'main' into stableDavid Lordbefore adoption81a482f
line 1422why, one step back1422 [Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2
lines 1423–1432why, one step back1423 (["--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 back1433 # 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 back1447 ],1448)Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2
line 1449why, one step back1449def test_option_names(runner, option_args, expected):Merge branch 'main' into stableDavid Lordbefore adoption81a482f
line 1450why, one step back1450 @click.command()Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2
lines 1451–1453why, one step back1451 @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 back1454Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2
line 1455why, one step back1455 assert cmd.params[0].name == expectedMerge branch 'main' into stableDavid Lordbefore adoption81a482f
line 1456why, one step back1456Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2
lines 1457–1460why, one step back1457 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 back146114621463def 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 back15801581Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2
lines 1582–1584why, one step back1582def 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 back15851586Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2
lines 1587–1591why, one step back1587@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 back1592 """1593 opt = click.Option(Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2
line 1594why, one step back1594 ("--cache/--no-cache", "--c/--nc"),Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 1595–1596why, one step back1595 default=default,1596 show_default=True,Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2
line 1597why, one step back1597 help="Enable/Disable the cache.",Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 1598–1599why, one step back1598 )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 back1600 assert opt.get_help_extra(ctx) == {"default": expect}Merge branch 'main' into stableDavid Lordbefore adoption81a482f
line 1601why, one step back1601 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 back1602 assert f"[default: {expect}]" in messageMerge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 1603–1604why, one step back16031604Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2
lines 1605–1607why, one step back1605def 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 back1608 """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 back1613 default=True,Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 1614–1616why, one step back1614 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 back1617 assert opt.get_help_extra(ctx) == {"default": "True"}Merge branch 'main' into stableDavid Lordbefore adoption81a482f
line 1618why, one step back1618 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 back1619 assert "[default: True]" in messageMerge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 1620–1621why, one step back16201621Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2
lines 1622–1625why, one step back1622@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 defaultMerge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 1626–1636why, one step back1626 """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 back1637 assert "[default: " not in messageMerge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 1638–1639why, one step back16381639Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2
lines 1640–1642why, one step back1640def 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 back1643 ctx = click.Context(click.Command("cli"))Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2
line 1644why, one step back1644 assert opt.get_help_extra(ctx) == {"default": "(unlimited)"}Merge branch 'main' into stableDavid Lordbefore adoption81a482f
line 1645why, one step back1645 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 back1646 assert "[default: (unlimited)]" in messageMerge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 1647–1648why, one step back16471648Show default string in prompt (#3328)Rowlando13before adoption76552ff
lines 1649–1661why, one step back1649def 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.output16601661Fix speculative empty string check (#3299)Rowlando13before adoption1458800
lines 1662–1688why, one step back1662class _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 back1689 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 back1691 assert expected in messageMerge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 1692–1693why, one step back16921693Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2
lines 1694–1696why, one step back1694def 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 back1697 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 back1700 assert "[default: None]" not in messageMerge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 1701–1702why, one step back17011702Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2
lines 1703–1705why, one step back1703def 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 back1706 """Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2
line 1707why, one step back1707 opt = click.Option(["-a"], multiple=True, help="values", show_default=True)Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 1708–1710why, one step back1708 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 back1711 assert message == "values"Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 1712–1714why, one step back171217131714@pytest.mark.parametrize(Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2
line 1715why, one step back1715 ("ctx_value", "opt_value", "extra_value", "expect"),Merge branch 'main' into stableDavid Lordbefore adoption81a482f
line 1716why, one step back1716 [Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2
lines 1717–1726why, one step back1717 (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 back1727 ],1728)Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2
lines 1729–1734why, one step back1729def 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 expectMerge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 1735–1737why, one step back173517361737@pytest.mark.parametrize(Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2
line 1738why, one step back1738 ("args", "expect"),Merge branch 'main' into stableDavid Lordbefore adoption81a482f
line 1739why, one step back1739 [Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2
lines 1740–1753why, one step back1740 (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 back1754 ],1755)Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2
line 1756why, one step back1756def test_option_with_optional_value(runner, args, expect):Merge branch 'main' into stableDavid Lordbefore adoption81a482f
line 1757why, one step back1757 @click.command()Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2
lines 1758–1762why, one step back1758 @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, bMerge branch 'main' into stableDavid Lordbefore adoption81a482f
line 1763why, one step back1763Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2
lines 1764–1765why, one step back1764 result = runner.invoke(cli, args, standalone_mode=False, catch_exceptions=False)1765 assert result.return_value == expectMerge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 1766–1767why, one step back17661767Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2
line 1768why, one step back1768def test_multiple_option_with_optional_value(runner):Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 1769–1770why, one step back1769 cli = click.Command(1770 "cli",Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2
lines 1771–1776why, one step back1771 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 back1777 )1778 result = runner.invoke(Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2
lines 1779–1782why, one step back1779 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 back1783 )Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2
lines 1784–1788why, one step back1784 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 back17891790Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2
lines 1791–1792why, one step back1791def 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 back1793 assert param.type is click.INTMake faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2
line 1794why, one step back1794 param = click.Option(["-b", "x"], flag_value=8)Merge branch 'main' into stableDavid Lordbefore adoption81a482f
line 1795why, one step back1795 assert param.type is click.INTAuto-detect `type=UNPROCESSED` when `flag_value` has non-basic types (#3363)Rowlando13before adoptionb03f211
lines 1796–1800why, one step back1796 # 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.UNPROCESSEDStable (#3851)Rowlando13before adoption6aabf09
lines 1801–1804why, one step back1801 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.UNPROCESSEDMerge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 1805–1868why, one step back180518061807@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, thenReconcile default value passing and default activation (#3239)Rowlando13before adoption8c95c73
lines 1869–1872why, one step back1869 # converted by the option type. For boolean flags, default=True is a literal1870 # 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/3239Merge branch 'main' into stableDavid Lordbefore adoption81a482f
line 1873why, one step back1873 ({"type": bool, "default": True, "flag_value": True}, [], True),Reconcile default value passing and default activation (#3239)Rowlando13before adoption8c95c73
line 1874why, one step back1874 ({"type": bool, "default": True, "flag_value": False}, [], True),Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 1875–1910why, one step back1875 ({"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 back1911 @click.option("--foo", is_flag=True, **opt_params)Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 1912–1929why, one step back1912 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 back1930 @click.option("--foo", is_flag=True, **opts)Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 1931–1935why, one step back1931 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 back1936 "Error: Invalid value for '--foo': 'foo' is not a valid boolean"Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 1937–1982why, one step back1937 in result.output1938 )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_r2271571819Add cross-reference to tests from `test_defaults.py` (#3223)Rowlando13before adoptionfa7f035
lines 1983–1988why, one step back19831984 .. 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 back1989 """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)2000The first 2,000 of 3,945 lines.