test_options.py
This file
Path
tests/test_options.py
2,321 lines · 76.2 KiB · Python
Last changed by
Fix handling of options which set both is_flag=False and flag_value=... (#3152)Rowlando13before adoptionebcd548
History of this fileTake it
The walk stopped after 200 commits: lines older than 328ab1f point at it.
Fix rendering of Enum choices' default (#3004)Rowlando13before adoptioneaf33a9
line 1why, one step back1import enumBump actions/cache from 3.2.3 to 3.2.4 (#2446)David Lordbefore adoption328ab1f
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
lines 5–8why, one step back5import tempfile6from contextlib import nullcontext7from typing import Literal8Fix rendering of Enum choices' default (#3004)Rowlando13before adoptioneaf33a9
lines 9–10why, one step back9if sys.version_info < (3, 11):10 enum.StrEnum = enum.Enum # type: ignore[assignment]Merge branch 'main' into stableDavid Lordbefore adoption81a482f
line 11why, one step back11Bump actions/cache from 3.2.3 to 3.2.4 (#2446)David Lordbefore adoption328ab1f
line 12why, one step back12import pytestMerge branch 'main' into stableDavid Lordbefore adoption81a482f
line 13why, one step back13Bump actions/cache from 3.2.3 to 3.2.4 (#2446)David Lordbefore adoption328ab1f
lines 14–15why, one step back14import click15from click import OptionMerge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 16–17why, one step back16from click import UNPROCESSED17from click._utils import UNSETFix handling of options which set both is_flag=False and flag_value=... (#3152)Rowlando13before adoptionebcd548
line 18why, one step back18from click.testing import CliRunnerMerge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 19–20why, one step back1920Bump actions/cache from 3.2.3 to 3.2.4 (#2446)David Lordbefore adoption328ab1f
line 21why, one step back21def test_prefixes(runner):Merge branch 'main' into stableDavid Lordbefore adoption81a482f
line 22why, one step back22 @click.command()Bump actions/cache from 3.2.3 to 3.2.4 (#2446)David Lordbefore adoption328ab1f
lines 23–26why, one step back23 @click.option("++foo", is_flag=True, help="das foo")24 @click.option("--bar", is_flag=True, help="das bar")25 def cli(foo, bar):26 click.echo(f"foo={foo} bar={bar}")Merge branch 'main' into stableDavid Lordbefore adoption81a482f
line 27why, one step back27Bump actions/cache from 3.2.3 to 3.2.4 (#2446)David Lordbefore adoption328ab1f
line 28why, one step back28 result = runner.invoke(cli, ["++foo", "--bar"])Merge branch 'main' into stableDavid Lordbefore adoption81a482f
line 29why, one step back29 assert not result.exceptionBump actions/cache from 3.2.3 to 3.2.4 (#2446)David Lordbefore adoption328ab1f
line 30why, one step back30 assert result.output == "foo=True bar=True\n"Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 31–32why, one step back3132 result = runner.invoke(cli, ["--help"])Bump actions/cache from 3.2.3 to 3.2.4 (#2446)David Lordbefore adoption328ab1f
lines 33–34why, one step back33 assert re.search(r"\+\+foo\s+das foo", result.output) is not None34 assert re.search(r"--bar\s+das bar", result.output) is not NoneMerge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 35–36why, one step back3536Bump actions/cache from 3.2.3 to 3.2.4 (#2446)David Lordbefore adoption328ab1f
lines 37–39why, one step back37def test_invalid_option(runner):38 with pytest.raises(TypeError, match="name was passed") as exc_info:39 click.Option(["foo"])Merge branch 'main' into stableDavid Lordbefore adoption81a482f
line 40why, one step back40Bump actions/cache from 3.2.3 to 3.2.4 (#2446)David Lordbefore adoption328ab1f
lines 41–44why, one step back41 message = str(exc_info.value)42 assert "name was passed (foo)" in message43 assert "declare an argument" in message44 assert "'--foo'" in messageMerge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 45–47why, one step back454647@pytest.mark.parametrize("deprecated", [True, "USE B INSTEAD"])Introduce Parameter.deprecated + Command.deprecated message customization (#2271)Peacockbefore adoption5961d31
line 48why, one step back48def test_deprecated_usage(runner, deprecated):Merge branch 'main' into stableDavid Lordbefore adoption81a482f
line 49why, one step back49 @click.command()Introduce Parameter.deprecated + Command.deprecated message customization (#2271)Peacockbefore adoption5961d31
line 50why, one step back50 @click.option("--foo", default="bar", deprecated=deprecated)Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 51–54why, one step back51 def cmd(foo):52 click.echo(foo)5354 result = runner.invoke(cmd, ["--help"])Introduce Parameter.deprecated + Command.deprecated message customization (#2271)Peacockbefore adoption5961d31
line 55why, one step back55 assert "(DEPRECATED" in result.outputMerge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 56–61why, one step back5657 if isinstance(deprecated, str):58 assert deprecated in result.output596061@pytest.mark.parametrize("deprecated", [True, "USE B INSTEAD"])Introduce Parameter.deprecated + Command.deprecated message customization (#2271)Peacockbefore adoption5961d31
line 62why, one step back62def test_deprecated_warning(runner, deprecated):Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 63–64why, one step back63 @click.command()64 @click.option(Introduce Parameter.deprecated + Command.deprecated message customization (#2271)Peacockbefore adoption5961d31
line 65why, one step back65 "--my-option", required=False, deprecated=deprecated, default="default option"Merge branch 'main' into stableDavid Lordbefore adoption81a482f
line 66why, one step back66 )Introduce Parameter.deprecated + Command.deprecated message customization (#2271)Peacockbefore adoption5961d31
lines 67–68why, one step back67 def cli(my_option: str):68 click.echo(f"{my_option}")Merge branch 'main' into stableDavid Lordbefore adoption81a482f
line 69why, one step back69Introduce Parameter.deprecated + Command.deprecated message customization (#2271)Peacockbefore adoption5961d31
line 70why, one step back70 # defaults should not give a deprecated warningMerge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 71–72why, one step back71 result = runner.invoke(cli, [])72 assert result.exit_code == 0, result.outputIntroduce Parameter.deprecated + Command.deprecated message customization (#2271)Peacockbefore adoption5961d31
line 73why, one step back73 assert "is deprecated" not in result.outputMerge branch 'main' into stableDavid Lordbefore adoption81a482f
line 74why, one step back74Introduce Parameter.deprecated + Command.deprecated message customization (#2271)Peacockbefore adoption5961d31
line 75why, one step back75 result = runner.invoke(cli, ["--my-option", "hello"])Merge branch 'main' into stableDavid Lordbefore adoption81a482f
line 76why, one step back76 assert result.exit_code == 0, result.outputIntroduce Parameter.deprecated + Command.deprecated message customization (#2271)Peacockbefore adoption5961d31
line 77why, one step back77 assert "option 'my_option' is deprecated" in result.outputMerge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 78–82why, one step back7879 if isinstance(deprecated, str):80 assert deprecated in result.output8182Introduce Parameter.deprecated + Command.deprecated message customization (#2271)Peacockbefore adoption5961d31
lines 83–85why, one step back83def test_deprecated_required(runner):84 with pytest.raises(ValueError, match="is deprecated and still required"):85 click.Option(["--a"], required=True, deprecated=True)Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 86–87why, one step back8687Introduce Parameter.deprecated + Command.deprecated message customization (#2271)Peacockbefore adoption5961d31
lines 88–90why, one step back88def test_deprecated_prompt(runner):89 with pytest.raises(ValueError, match="`deprecated` options cannot use `prompt`"):90 click.Option(["--a"], prompt=True, deprecated=True)Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 91–92why, one step back9192Bump actions/cache from 3.2.3 to 3.2.4 (#2446)David Lordbefore adoption328ab1f
line 93why, one step back93def test_invalid_nargs(runner):Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 94–96why, one step back94 with pytest.raises(TypeError, match="nargs=-1 is not supported for options."):9596 @click.command()Bump actions/cache from 3.2.3 to 3.2.4 (#2446)David Lordbefore adoption328ab1f
lines 97–99why, one step back97 @click.option("--foo", nargs=-1)98 def cli(foo):99 passMerge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 100–101why, one step back100101Bump actions/cache from 3.2.3 to 3.2.4 (#2446)David Lordbefore adoption328ab1f
line 102why, one step back102def test_nargs_tup_composite_mult(runner):Merge branch 'main' into stableDavid Lordbefore adoption81a482f
line 103why, one step back103 @click.command()Bump actions/cache from 3.2.3 to 3.2.4 (#2446)David Lordbefore adoption328ab1f
lines 104–107why, one step back104 @click.option("--item", type=(str, int), multiple=True)105 def copy(item):106 for name, id in item:107 click.echo(f"name={name} id={id:d}")Merge branch 'main' into stableDavid Lordbefore adoption81a482f
line 108why, one step back108Bump actions/cache from 3.2.3 to 3.2.4 (#2446)David Lordbefore adoption328ab1f
line 109why, one step back109 result = runner.invoke(copy, ["--item", "peter", "1", "--item", "max", "2"])Merge branch 'main' into stableDavid Lordbefore adoption81a482f
line 110why, one step back110 assert not result.exceptionBump actions/cache from 3.2.3 to 3.2.4 (#2446)David Lordbefore adoption328ab1f
line 111why, one step back111 assert result.output.splitlines() == ["name=peter id=1", "name=max id=2"]Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 112–113why, one step back112113Bump actions/cache from 3.2.3 to 3.2.4 (#2446)David Lordbefore adoption328ab1f
line 114why, one step back114def test_counting(runner):Merge branch 'main' into stableDavid Lordbefore adoption81a482f
line 115why, one step back115 @click.command()Bump actions/cache from 3.2.3 to 3.2.4 (#2446)David Lordbefore adoption328ab1f
lines 116–118why, one step back116 @click.option("-v", count=True, help="Verbosity", type=click.IntRange(0, 3))117 def cli(v):118 click.echo(f"verbosity={v:d}")Merge branch 'main' into stableDavid Lordbefore adoption81a482f
line 119why, one step back119Bump actions/cache from 3.2.3 to 3.2.4 (#2446)David Lordbefore adoption328ab1f
line 120why, one step back120 result = runner.invoke(cli, ["-vvv"])Merge branch 'main' into stableDavid Lordbefore adoption81a482f
line 121why, one step back121 assert not result.exceptionBump actions/cache from 3.2.3 to 3.2.4 (#2446)David Lordbefore adoption328ab1f
line 122why, one step back122 assert result.output == "verbosity=3\n"Merge branch 'main' into stableDavid Lordbefore adoption81a482f
line 123why, one step back123Bump actions/cache from 3.2.3 to 3.2.4 (#2446)David Lordbefore adoption328ab1f
line 124why, one step back124 result = runner.invoke(cli, ["-vvvv"])Merge branch 'main' into stableDavid Lordbefore adoption81a482f
line 125why, one step back125 assert result.exceptionBump actions/cache from 3.2.3 to 3.2.4 (#2446)David Lordbefore adoption328ab1f
line 126why, one step back126 assert "Invalid value for '-v': 4 is not in the range 0<=x<=3." in result.outputMerge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 127–129why, one step back127128 result = runner.invoke(cli, [])129 assert not result.exceptionBump actions/cache from 3.2.3 to 3.2.4 (#2446)David Lordbefore adoption328ab1f
line 130why, one step back130 assert result.output == "verbosity=0\n"Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 131–132why, one step back131132 result = runner.invoke(cli, ["--help"])Bump actions/cache from 3.2.3 to 3.2.4 (#2446)David Lordbefore adoption328ab1f
line 133why, one step back133 assert re.search(r"-v\s+Verbosity", result.output) is not NoneMerge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 134–135why, one step back134135Bump actions/cache from 3.2.3 to 3.2.4 (#2446)David Lordbefore adoption328ab1f
lines 136–137why, one step back136@pytest.mark.parametrize("unknown_flag", ["--foo", "-f"])137def test_unknown_options(runner, unknown_flag):Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 138–141why, one step back138 @click.command()139 def cli():140 pass141Bump actions/cache from 3.2.3 to 3.2.4 (#2446)David Lordbefore adoption328ab1f
line 142why, one step back142 result = runner.invoke(cli, [unknown_flag])Merge branch 'main' into stableDavid Lordbefore adoption81a482f
line 143why, one step back143 assert result.exceptionBump actions/cache from 3.2.3 to 3.2.4 (#2446)David Lordbefore adoption328ab1f
line 144why, one step back144 assert f"No such option: {unknown_flag}" in result.outputMerge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 145–147why, one step back145146147@pytest.mark.parametrize(Bump actions/cache from 3.2.3 to 3.2.4 (#2446)David Lordbefore adoption328ab1f
line 148why, one step back148 ("value", "expect"),Merge branch 'main' into stableDavid Lordbefore adoption81a482f
line 149why, one step back149 [Bump actions/cache from 3.2.3 to 3.2.4 (#2446)David Lordbefore adoption328ab1f
lines 150–152why, one step back150 ("--cat", "Did you mean --count?"),151 ("--bounds", "(Possible options: --bound, --count)"),152 ("--bount", "(Possible options: --bound, --count)"),Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 153–154why, one step back153 ],154)Bump actions/cache from 3.2.3 to 3.2.4 (#2446)David Lordbefore adoption328ab1f
line 155why, one step back155def test_suggest_possible_options(runner, value, expect):Merge branch 'main' into stableDavid Lordbefore adoption81a482f
line 156why, one step back156 cli = click.Command(Bump actions/cache from 3.2.3 to 3.2.4 (#2446)David Lordbefore adoption328ab1f
line 157why, one step back157 "cli", params=[click.Option(["--bound"]), click.Option(["--count"])]Merge branch 'main' into stableDavid Lordbefore adoption81a482f
line 158why, one step back158 )Bump actions/cache from 3.2.3 to 3.2.4 (#2446)David Lordbefore adoption328ab1f
lines 159–160why, one step back159 result = runner.invoke(cli, [value])160 assert expect in result.outputMerge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 161–162why, one step back161162Bump actions/cache from 3.2.3 to 3.2.4 (#2446)David Lordbefore adoption328ab1f
line 163why, one step back163def test_multiple_required(runner):Merge branch 'main' into stableDavid Lordbefore adoption81a482f
line 164why, one step back164 @click.command()Bump actions/cache from 3.2.3 to 3.2.4 (#2446)David Lordbefore adoption328ab1f
lines 165–167why, one step back165 @click.option("-m", "--message", multiple=True, required=True)166 def cli(message):167 click.echo("\n".join(message))Merge branch 'main' into stableDavid Lordbefore adoption81a482f
line 168why, one step back168Bump actions/cache from 3.2.3 to 3.2.4 (#2446)David Lordbefore adoption328ab1f
line 169why, one step back169 result = runner.invoke(cli, ["-m", "foo", "-mbar"])Merge branch 'main' into stableDavid Lordbefore adoption81a482f
line 170why, one step back170 assert not result.exceptionBump actions/cache from 3.2.3 to 3.2.4 (#2446)David Lordbefore adoption328ab1f
line 171why, one step back171 assert result.output == "foo\nbar\n"Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 172–174why, one step back172173 result = runner.invoke(cli, [])174 assert result.exceptionBump actions/cache from 3.2.3 to 3.2.4 (#2446)David Lordbefore adoption328ab1f
line 175why, one step back175 assert "Error: Missing option '-m' / '--message'." in result.outputMerge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 176–383why, one step back176177178@pytest.mark.parametrize(179 ("multiple", "nargs", "default", "expected"),180 [181 # If multiple values are allowed, defaults should be iterable.182 (True, 1, [], ()),183 (True, 1, (), ()),184 (True, 1, tuple(), ()),185 (True, 1, set(), ()),186 (True, 1, frozenset(), ()),187 (True, 1, {}, ()),188 # Special values.189 (True, 1, None, ()),190 (True, 1, UNSET, ()),191 # Number of values are kept as-is in the default.192 (True, 1, [1], (1,)),193 (True, 1, [1, 2], (1, 2)),194 (True, 1, [1, 2, 3], (1, 2, 3)),195 (True, 1, [1.1, 2.2], (1.1, 2.2)),196 (True, 1, ["1", "2"], ("1", "2")),197 (True, 1, [None, None], (None, None)),198 # Contrary to list or tuples, native Python types not supported by Click are199 # not recognized and are converted to the default format: tuple of strings.200 # Refs: https://github.com/pallets/click/issues/3036201 (True, 1, {1, 2}, ("1", "2")),202 (True, 1, frozenset([1, 2]), ("1", "2")),203 (True, 1, {1: "a", 2: "b"}, ("1", "2")),204 # Multiple values with nargs > 1.205 (True, 2, [], ()),206 (True, 2, (), ()),207 (True, 2, tuple(), ()),208 (True, 2, set(), ()),209 (True, 2, frozenset(), ()),210 (True, 2, {}, ()),211 (True, 2, None, ()),212 (True, 2, UNSET, ()),213 (True, 2, [[1, 2]], ((1, 2),)),214 (True, 2, [[1, 2], [3, 4]], ((1, 2), (3, 4))),215 (True, 2, [[1, 2], [3, 4], [5, 6]], ((1, 2), (3, 4), (5, 6))),216 (True, 2, [[1.1, 2.2], [3.3, 4.4]], ((1.1, 2.2), (3.3, 4.4))),217 (True, 2, [["1", "2"], ["3", "4"]], (("1", "2"), ("3", "4"))),218 (True, 2, [[None, None], [None, None]], ((None, None), (None, None))),219 (True, 2, [[1, 2.2], ["3", None]], ((1, 2.2), (3, None))),220 (True, 2, [[1, 2.2], None], ((1, 2.2), None)),221 # Default of the right length works for non-multiples.222 (False, 2, [1, 2], (1, 2)),223 ],224)225def test_good_defaults_for_multiple(runner, multiple, nargs, default, expected):226 @click.command()227 @click.option("-a", multiple=multiple, nargs=nargs, default=default)228 def cmd(a):229 click.echo(repr(a), nl=False)230231 result = runner.invoke(cmd)232 assert result.output == repr(expected)233234235@pytest.mark.parametrize(236 ("multiple", "nargs", "default", "exception", "message"),237 [238 # Non-iterables defaults.239 (240 True,241 1,242 "Yo",243 None,244 "Error: Invalid value for '-a': Value must be an iterable.",245 ),246 (247 True,248 1,249 "",250 None,251 "Error: Invalid value for '-a': Value must be an iterable.",252 ),253 (254 True,255 1,256 True,257 None,258 "Error: Invalid value for '-a': Value must be an iterable.",259 ),260 (261 True,262 1,263 False,264 None,265 "Error: Invalid value for '-a': Value must be an iterable.",266 ),267 (268 True,269 1,270 12,271 None,272 "Error: Invalid value for '-a': Value must be an iterable.",273 ),274 (275 True,276 1,277 7.9,278 None,279 "Error: Invalid value for '-a': Value must be an iterable.",280 ),281 #282 (283 False,284 2,285 42,286 None,287 "Error: Invalid value for '-a': Value must be an iterable.",288 ),289 (290 True,291 2,292 ["test string which is not a list in the list"],293 None,294 "Error: Invalid value for '-a': Value must be an iterable.",295 ),296 # Multiple options, each with 2 args, but with wrong length.297 (298 True,299 2,300 (1,),301 None,302 "Error: Invalid value for '-a': Value must be an iterable.",303 ),304 (305 True,306 2,307 (1, 2, 3),308 None,309 "Error: Invalid value for '-a': Value must be an iterable.",310 ),311 (312 True,313 2,314 [tuple()],315 ValueError,316 r"'nargs' must be 0 \(or None\) for type <click\.types\.Tuple object at "317 r"0x[0-9A-Fa-f]+>, but it was 2\.",318 ),319 (320 True,321 2,322 [(1,)],323 ValueError,324 r"'nargs' must be 1 \(or None\) for type <click\.types\.Tuple object at "325 r"0x[0-9A-Fa-f]+>, but it was 2\.",326 ),327 (328 True,329 2,330 [(1, 2, 3)],331 ValueError,332 r"'nargs' must be 3 \(or None\) for type <click\.types\.Tuple object at "333 r"0x[0-9A-Fa-f]+>, but it was 2\.",334 ),335 # A mix of valid and invalid defaults.336 (337 True,338 2,339 [[1, 2.2], []],340 None,341 "Error: Invalid value for '-a': 2 values are required, but 0 were given.",342 ),343 # Default values that are iterable but not of the right length.344 (345 False,346 2,347 [1],348 None,349 "Error: Invalid value for '-a': Takes 2 values but 1 was given.",350 ),351 (352 True,353 2,354 [[1]],355 ValueError,356 r"'nargs' must be 1 \(or None\) for type <click\.types\.Tuple object at "357 r"0x[0-9A-Fa-f]+>, but it was 2\.",358 ),359 ],360)361def test_bad_defaults_for_multiple(362 runner, multiple, nargs, default, exception, message363):364 if exception:365 assert issubclass(exception, Exception)366 else:367 assert exception is None368369 with (370 pytest.raises(exception, match=re.compile(message))371 if exception372 else nullcontext()373 ):374375 @click.command()376 @click.option("-a", multiple=multiple, nargs=nargs, default=default)377 def cmd(a):378 click.echo(repr(a))379380 result = runner.invoke(cmd)381 assert message in result.stderr382383Bump actions/cache from 3.2.3 to 3.2.4 (#2446)David Lordbefore adoption328ab1f
lines 384–385why, one step back384@pytest.mark.parametrize("env_key", ["MYPATH", "AUTO_MYPATH"])385def test_empty_envvar(runner, env_key):Merge branch 'main' into stableDavid Lordbefore adoption81a482f
line 386why, one step back386 @click.command()Bump actions/cache from 3.2.3 to 3.2.4 (#2446)David Lordbefore adoption328ab1f
lines 387–389why, one step back387 @click.option("--mypath", type=click.Path(exists=True), envvar="MYPATH")388 def cli(mypath):389 click.echo(f"mypath: {mypath}")Merge branch 'main' into stableDavid Lordbefore adoption81a482f
line 390why, one step back390Bump actions/cache from 3.2.3 to 3.2.4 (#2446)David Lordbefore adoption328ab1f
lines 391–393why, one step back391 result = runner.invoke(cli, env={env_key: ""}, auto_envvar_prefix="AUTO")392 assert result.exception is None393 assert result.output == "mypath: None\n"Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 394–395why, one step back394395Bump actions/cache from 3.2.3 to 3.2.4 (#2446)David Lordbefore adoption328ab1f
line 396why, one step back396def test_multiple_envvar(runner):Merge branch 'main' into stableDavid Lordbefore adoption81a482f
line 397why, one step back397 @click.command()Bump actions/cache from 3.2.3 to 3.2.4 (#2446)David Lordbefore adoption328ab1f
line 398why, one step back398 @click.option("--arg", multiple=True)Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 399–402why, one step back399 def cmd(arg):400 click.echo("|".join(arg))401402 result = runner.invoke(Bump actions/cache from 3.2.3 to 3.2.4 (#2446)David Lordbefore adoption328ab1f
line 403why, one step back403 cmd, [], auto_envvar_prefix="TEST", env={"TEST_ARG": "foo bar baz"}Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 404–408why, one step back404 )405 assert not result.exception406 assert result.output == "foo|bar|baz\n"407408 @click.command()Bump actions/cache from 3.2.3 to 3.2.4 (#2446)David Lordbefore adoption328ab1f
line 409why, one step back409 @click.option("--arg", multiple=True, envvar="X")Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 410–412why, one step back410 def cmd(arg):411 click.echo("|".join(arg))412Bump actions/cache from 3.2.3 to 3.2.4 (#2446)David Lordbefore adoption328ab1f
line 413why, one step back413 result = runner.invoke(cmd, [], env={"X": "foo bar baz"})Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 414–417why, one step back414 assert not result.exception415 assert result.output == "foo|bar|baz\n"416417 @click.command()Bump actions/cache from 3.2.3 to 3.2.4 (#2446)David Lordbefore adoption328ab1f
line 418why, one step back418 @click.option("--arg", multiple=True, type=click.Path())Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 419–422why, one step back419 def cmd(arg):420 click.echo("|".join(arg))421422 result = runner.invoke(Bump actions/cache from 3.2.3 to 3.2.4 (#2446)David Lordbefore adoption328ab1f
lines 423–426why, one step back423 cmd,424 [],425 auto_envvar_prefix="TEST",426 env={"TEST_ARG": f"foo{os.path.pathsep}bar"},Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 427–434why, one step back427 )428 assert not result.exception429 assert result.output == "foo|bar\n"430431432@pytest.mark.parametrize(433 ("envvar_name", "envvar_value", "expected"),434 (Fix reconciliation of envvar with `default`, `flag_value` and `type` parameters for flag options (#2956)Kevin Deldyckebefore adoption565f36d
lines 435–449why, one step back435 # Lower-cased variations of value.436 ("SHOUT", "true", True),437 ("SHOUT", "false", False),438 # Title-cased variations of value.439 ("SHOUT", "True", True),440 ("SHOUT", "False", False),441 # Upper-cased variations of value.442 ("SHOUT", "TRUE", True),443 ("SHOUT", "FALSE", False),444 # Random-cased variations of value.445 ("SHOUT", "TruE", True),446 ("SHOUT", "truE", True),447 ("SHOUT", "FaLsE", False),448 ("SHOUT", "falsE", False),449 # Extra spaces around the value.Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 450–455why, one step back450 ("SHOUT", "true ", True),451 ("SHOUT", " true ", True),452 ("SHOUT", " true", True),453 ("SHOUT", "false ", False),454 ("SHOUT", " false ", False),455 ("SHOUT", " false", False),Fix reconciliation of envvar with `default`, `flag_value` and `type` parameters for flag options (#2956)Kevin Deldyckebefore adoption565f36d
lines 456–460why, one step back456 # Integer variations.457 ("SHOUT", "1", True),458 ("SHOUT", "0", False),459 # Alternative states.460 ("SHOUT", "t", True),Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 461–462why, one step back461 ("SHOUT", "T", True),462 ("SHOUT", " T ", True),Fix reconciliation of envvar with `default`, `flag_value` and `type` parameters for flag options (#2956)Kevin Deldyckebefore adoption565f36d
lines 463–476why, one step back463 ("SHOUT", "f", False),464 ("SHOUT", "y", True),465 ("SHOUT", "n", False),466 ("SHOUT", "yes", True),467 ("SHOUT", "no", False),468 ("SHOUT", "on", True),469 ("SHOUT", "off", False),470 # Blank value variations.471 ("SHOUT", None, False),472 ("SHOUT", "", False),473 ("SHOUT", " ", False),474 ("SHOUT", " ", False),475 # Variable names are not stripped of spaces and so don't match the476 # flag, which then naturraly takes its default value.Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 477–478why, one step back477 ("SHOUT ", "True", False),478 ("SHOUT ", "False", False),Fix reconciliation of envvar with `default`, `flag_value` and `type` parameters for flag options (#2956)Kevin Deldyckebefore adoption565f36d
lines 479–480why, one step back479 (" SHOUT ", "True", False),480 (" SHOUT ", "False", False),Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 481–484why, one step back481 (" SHOUT", "True", False),482 (" SHOUT", "False", False),483 ("SH OUT", "True", False),484 ("SH OUT", "False", False),Fix reconciliation of envvar with `default`, `flag_value` and `type` parameters for flag options (#2956)Kevin Deldyckebefore adoption565f36d
lines 485–489why, one step back485 # Same for random and reverse environment variable names: they are not486 # recognized by the option.487 ("RANDOM", "True", False),488 ("NO_SHOUT", "True", False),489 ("NO_SHOUT", "False", False),Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 490–541why, one step back490 ("NOSHOUT", "True", False),491 ("NOSHOUT", "False", False),492 ),493)494def test_boolean_flag_envvar(runner, envvar_name, envvar_value, expected):495 assert isinstance(envvar_name, str)496 assert isinstance(envvar_value, str) or envvar_value is None497498 @click.command()499 @click.option("--shout/--no-shout", envvar="SHOUT")500 def cli(shout):501 click.echo(repr(shout), nl=False)502503 result = runner.invoke(cli, [], env={envvar_name: envvar_value})504 assert result.exit_code == 0505 assert result.output == repr(expected)506507508@pytest.mark.parametrize(509 "value",510 (511 # Extra spaces inside the value.512 "tr ue",513 "fa lse",514 # Numbers.515 "10",516 "01",517 "00",518 "11",519 "0.0",520 "1.1",521 # Random strings.522 "randomstring",523 "None",524 "'None'",525 "A B",526 " 1 2 ",527 "9.3",528 "a;n",529 "x:y",530 "i/o",531 ),532)533def test_boolean_envvar_bad_values(runner, value):534 @click.command()535 @click.option("--shout/--no-shout", envvar="SHOUT")536 def cli(shout):537 click.echo(shout)538539 result = runner.invoke(cli, [], env={"SHOUT": value})540 assert result.exit_code == 2541 assert (Fix reconciliation of envvar with `default`, `flag_value` and `type` parameters for flag options (#2956)Kevin Deldyckebefore adoption565f36d
line 542why, one step back542 f"Invalid value for '--shout': {value!r} is not a valid boolean."Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 543–546why, one step back543 in result.output544 )545546Bump actions/cache from 3.2.3 to 3.2.4 (#2446)David Lordbefore adoption328ab1f
line 547why, one step back547def test_multiple_default_help(runner):Merge branch 'main' into stableDavid Lordbefore adoption81a482f
line 548why, one step back548 @click.command()Bump actions/cache from 3.2.3 to 3.2.4 (#2446)David Lordbefore adoption328ab1f
lines 549–551why, one step back549 @click.option("--arg1", multiple=True, default=("foo", "bar"), show_default=True)550 @click.option("--arg2", multiple=True, default=(1, 2), type=int, show_default=True)551 def cmd(arg, arg2):Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 552–555why, one step back552 pass553554 result = runner.invoke(cmd, ["--help"])555 assert not result.exceptionBump actions/cache from 3.2.3 to 3.2.4 (#2446)David Lordbefore adoption328ab1f
lines 556–557why, one step back556 assert "foo, bar" in result.output557 assert "1, 2" in result.outputMerge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 558–559why, one step back558559Bump actions/cache from 3.2.3 to 3.2.4 (#2446)David Lordbefore adoption328ab1f
line 560why, one step back560def test_show_default_default_map(runner):Merge branch 'main' into stableDavid Lordbefore adoption81a482f
line 561why, one step back561 @click.command()Bump actions/cache from 3.2.3 to 3.2.4 (#2446)David Lordbefore adoption328ab1f
line 562why, one step back562 @click.option("--arg", default="a", show_default=True)Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 563–565why, one step back563 def cmd(arg):564 click.echo(arg)565Bump actions/cache from 3.2.3 to 3.2.4 (#2446)David Lordbefore adoption328ab1f
line 566why, one step back566 result = runner.invoke(cmd, ["--help"], default_map={"arg": "b"})Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 567–568why, one step back567568 assert not result.exceptionBump actions/cache from 3.2.3 to 3.2.4 (#2446)David Lordbefore adoption328ab1f
line 569why, one step back569 assert "[default: b]" in result.outputMerge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 570–571why, one step back570571Bump actions/cache from 3.2.3 to 3.2.4 (#2446)David Lordbefore adoption328ab1f
lines 572–574why, one step back572def test_multiple_default_type():573 opt = click.Option(["-a"], multiple=True, default=(1, 2))574 assert opt.nargs == 1Merge branch 'main' into stableDavid Lordbefore adoption81a482f
line 575why, one step back575 assert opt.multipleBump actions/cache from 3.2.3 to 3.2.4 (#2446)David Lordbefore adoption328ab1f
line 576why, one step back576 assert opt.type is click.INTMerge branch 'main' into stableDavid Lordbefore adoption81a482f
line 577why, one step back577 ctx = click.Context(click.Command("test"))Bump actions/cache from 3.2.3 to 3.2.4 (#2446)David Lordbefore adoption328ab1f
line 578why, one step back578 assert opt.get_default(ctx) == (1, 2)Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 579–580why, one step back579580Bump actions/cache from 3.2.3 to 3.2.4 (#2446)David Lordbefore adoption328ab1f
lines 581–583why, one step back581def test_multiple_default_composite_type():582 opt = click.Option(["-a"], multiple=True, default=[(1, "a")])583 assert opt.nargs == 2Merge branch 'main' into stableDavid Lordbefore adoption81a482f
line 584why, one step back584 assert opt.multipleBump actions/cache from 3.2.3 to 3.2.4 (#2446)David Lordbefore adoption328ab1f
lines 585–586why, one step back585 assert isinstance(opt.type, click.Tuple)586 assert opt.type.types == [click.INT, click.STRING]Merge branch 'main' into stableDavid Lordbefore adoption81a482f
line 587why, one step back587 ctx = click.Context(click.Command("test"))Bump actions/cache from 3.2.3 to 3.2.4 (#2446)David Lordbefore adoption328ab1f
line 588why, one step back588 assert opt.type_cast_value(ctx, opt.get_default(ctx)) == ((1, "a"),)Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 589–590why, one step back589590Bump actions/cache from 3.2.3 to 3.2.4 (#2446)David Lordbefore adoption328ab1f
line 591why, one step back591def test_parse_multiple_default_composite_type(runner):Merge branch 'main' into stableDavid Lordbefore adoption81a482f
line 592why, one step back592 @click.command()Bump actions/cache from 3.2.3 to 3.2.4 (#2446)David Lordbefore adoption328ab1f
lines 593–595why, one step back593 @click.option("-a", multiple=True, default=("a", "b"))594 @click.option("-b", multiple=True, default=[(1, "a")])595 def cmd(a, b):Merge branch 'main' into stableDavid Lordbefore adoption81a482f
line 596why, one step back596 click.echo(a)Bump actions/cache from 3.2.3 to 3.2.4 (#2446)David Lordbefore adoption328ab1f
line 597why, one step back597 click.echo(b)Merge branch 'main' into stableDavid Lordbefore adoption81a482f
line 598why, one step back598Bump actions/cache from 3.2.3 to 3.2.4 (#2446)David Lordbefore adoption328ab1f
lines 599–600why, one step back599 # result = runner.invoke(cmd, "-a c -a 1 -a d -b 2 two -b 4 four".split())600 # assert result.output == "('c', '1', 'd')\n((2, 'two'), (4, 'four'))\n"Merge branch 'main' into stableDavid Lordbefore adoption81a482f
line 601why, one step back601 result = runner.invoke(cmd)Bump actions/cache from 3.2.3 to 3.2.4 (#2446)David Lordbefore adoption328ab1f
line 602why, one step back602 assert result.output == "('a', 'b')\n((1, 'a'),)\n"Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 603–604why, one step back603604Bump actions/cache from 3.2.3 to 3.2.4 (#2446)David Lordbefore adoption328ab1f
line 605why, one step back605def test_dynamic_default_help_unset(runner):Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 606–619why, one step back606 @click.command()607 @click.option(608 "--username",609 prompt=True,610 default=lambda: os.environ.get("USER", ""),611 show_default=True,612 )613 def cmd(username):614 print("Hello,", username)615616 result = runner.invoke(cmd, ["--help"])617 assert result.exit_code == 0618 assert "--username" in result.output619 assert "lambda" not in result.outputBump actions/cache from 3.2.3 to 3.2.4 (#2446)David Lordbefore adoption328ab1f
line 620why, one step back620 assert "(dynamic)" in result.outputMerge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 621–622why, one step back621622Bump actions/cache from 3.2.3 to 3.2.4 (#2446)David Lordbefore adoption328ab1f
line 623why, one step back623def test_dynamic_default_help_text(runner):Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 624–628why, one step back624 @click.command()625 @click.option(626 "--username",627 prompt=True,628 default=lambda: os.environ.get("USER", ""),Bump actions/cache from 3.2.3 to 3.2.4 (#2446)David Lordbefore adoption328ab1f
line 629why, one step back629 show_default="current user",Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 630–637why, one step back630 )631 def cmd(username):632 print("Hello,", username)633634 result = runner.invoke(cmd, ["--help"])635 assert result.exit_code == 0636 assert "--username" in result.output637 assert "lambda" not in result.outputBump actions/cache from 3.2.3 to 3.2.4 (#2446)David Lordbefore adoption328ab1f
line 638why, one step back638 assert "(current user)" in result.outputMerge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 639–640why, one step back639640Bump actions/cache from 3.2.3 to 3.2.4 (#2446)David Lordbefore adoption328ab1f
lines 641–644why, one step back641def test_dynamic_default_help_special_method(runner):642 class Value:643 def __call__(self):644 return 42Merge branch 'main' into stableDavid Lordbefore adoption81a482f
line 645why, one step back645Bump actions/cache from 3.2.3 to 3.2.4 (#2446)David Lordbefore adoption328ab1f
lines 646–647why, one step back646 def __str__(self):647 return "special value"Merge branch 'main' into stableDavid Lordbefore adoption81a482f
line 648why, one step back648Bump actions/cache from 3.2.3 to 3.2.4 (#2446)David Lordbefore adoption328ab1f
line 649why, one step back649 opt = click.Option(["-a"], default=Value(), show_default=True)Merge branch 'main' into stableDavid Lordbefore adoption81a482f
line 650why, one step back650 ctx = click.Context(click.Command("cli"))Split generation of help extra items and rendering (#2517)Kevin Deldyckebefore adoption948d7a7
line 651why, one step back651 assert opt.get_help_extra(ctx) == {"default": "special value"}Bump actions/cache from 3.2.3 to 3.2.4 (#2446)David Lordbefore adoption328ab1f
line 652why, one step back652 assert "special value" in opt.get_help_record(ctx)[1]Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 653–655why, one step back653654655@pytest.mark.parametrize(Bump actions/cache from 3.2.3 to 3.2.4 (#2446)David Lordbefore adoption328ab1f
line 656why, one step back656 ("type", "expect"),Merge branch 'main' into stableDavid Lordbefore adoption81a482f
line 657why, one step back657 [Bump actions/cache from 3.2.3 to 3.2.4 (#2446)David Lordbefore adoption328ab1f
lines 658–661why, one step back658 (click.IntRange(1, 32), "1<=x<=32"),659 (click.IntRange(1, 32, min_open=True, max_open=True), "1<x<32"),660 (click.IntRange(1), "x>=1"),661 (click.IntRange(max=32), "x<=32"),Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 662–663why, one step back662 ],663)Bump actions/cache from 3.2.3 to 3.2.4 (#2446)David Lordbefore adoption328ab1f
lines 664–665why, one step back664def test_intrange_default_help_text(type, expect):665 option = click.Option(["--num"], type=type, show_default=True, default=2)Merge branch 'main' into stableDavid Lordbefore adoption81a482f
line 666why, one step back666 context = click.Context(click.Command("test"))Split generation of help extra items and rendering (#2517)Kevin Deldyckebefore adoption948d7a7
line 667why, one step back667 assert option.get_help_extra(context) == {"default": "2", "range": expect}Merge branch 'main' into stableDavid Lordbefore adoption81a482f
line 668why, one step back668 result = option.get_help_record(context)[1]Bump actions/cache from 3.2.3 to 3.2.4 (#2446)David Lordbefore adoption328ab1f
line 669why, one step back669 assert expect in resultMerge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 670–671why, one step back670671Bump actions/cache from 3.2.3 to 3.2.4 (#2446)David Lordbefore adoption328ab1f
lines 672–674why, one step back672def test_count_default_type_help():673 """A count option with the default type should not show >=0 in help."""674 option = click.Option(["--count"], count=True, help="some words")Merge branch 'main' into stableDavid Lordbefore adoption81a482f
line 675why, one step back675 context = click.Context(click.Command("test"))Split generation of help extra items and rendering (#2517)Kevin Deldyckebefore adoption948d7a7
line 676why, one step back676 assert option.get_help_extra(context) == {}Merge branch 'main' into stableDavid Lordbefore adoption81a482f
line 677why, one step back677 result = option.get_help_record(context)[1]Bump actions/cache from 3.2.3 to 3.2.4 (#2446)David Lordbefore adoption328ab1f
line 678why, one step back678 assert result == "some words"Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 679–680why, one step back679680Bump actions/cache from 3.2.3 to 3.2.4 (#2446)David Lordbefore adoption328ab1f
lines 681–683why, one step back681def test_file_type_help_default():682 """The default for a File type is a filename string. The string683 should be displayed in help, not an open file object.Merge branch 'main' into stableDavid Lordbefore adoption81a482f
line 684why, one step back684Bump actions/cache from 3.2.3 to 3.2.4 (#2446)David Lordbefore adoption328ab1f
lines 685–686why, one step back685 Type casting is only applied to defaults in processing, not when686 getting the default value.Merge branch 'main' into stableDavid Lordbefore adoption81a482f
line 687why, one step back687 """Bump actions/cache from 3.2.3 to 3.2.4 (#2446)David Lordbefore adoption328ab1f
lines 688–689why, one step back688 option = click.Option(689 ["--in"], type=click.File(), default=__file__, show_default=TrueMerge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 690–691why, one step back690 )691 context = click.Context(click.Command("test"))Split generation of help extra items and rendering (#2517)Kevin Deldyckebefore adoption948d7a7
line 692why, one step back692 assert option.get_help_extra(context) == {"default": __file__}Merge branch 'main' into stableDavid Lordbefore adoption81a482f
line 693why, one step back693 result = option.get_help_record(context)[1]Bump actions/cache from 3.2.3 to 3.2.4 (#2446)David Lordbefore adoption328ab1f
line 694why, one step back694 assert __file__ in resultMerge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 695–696why, one step back695696Bump actions/cache from 3.2.3 to 3.2.4 (#2446)David Lordbefore adoption328ab1f
line 697why, one step back697def test_toupper_envvar_prefix(runner):Merge branch 'main' into stableDavid Lordbefore adoption81a482f
line 698why, one step back698 @click.command()Bump actions/cache from 3.2.3 to 3.2.4 (#2446)David Lordbefore adoption328ab1f
line 699why, one step back699 @click.option("--arg")Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 700–702why, one step back700 def cmd(arg):701 click.echo(arg)702Bump actions/cache from 3.2.3 to 3.2.4 (#2446)David Lordbefore adoption328ab1f
line 703why, one step back703 result = runner.invoke(cmd, [], auto_envvar_prefix="test", env={"TEST_ARG": "foo"})Merge branch 'main' into stableDavid Lordbefore adoption81a482f
line 704why, one step back704 assert not result.exceptionBump actions/cache from 3.2.3 to 3.2.4 (#2446)David Lordbefore adoption328ab1f
line 705why, one step back705 assert result.output == "foo\n"Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 706–707why, one step back706707Bump actions/cache from 3.2.3 to 3.2.4 (#2446)David Lordbefore adoption328ab1f
line 708why, one step back708def test_nargs_envvar(runner):Merge branch 'main' into stableDavid Lordbefore adoption81a482f
line 709why, one step back709 @click.command()Bump actions/cache from 3.2.3 to 3.2.4 (#2446)David Lordbefore adoption328ab1f
line 710why, one step back710 @click.option("--arg", nargs=2)Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 711–714why, one step back711 def cmd(arg):712 click.echo("|".join(arg))713714 result = runner.invoke(Bump actions/cache from 3.2.3 to 3.2.4 (#2446)David Lordbefore adoption328ab1f
line 715why, one step back715 cmd, [], auto_envvar_prefix="TEST", env={"TEST_ARG": "foo bar"}Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 716–720why, one step back716 )717 assert not result.exception718 assert result.output == "foo|bar\n"719720 @click.command()Bump actions/cache from 3.2.3 to 3.2.4 (#2446)David Lordbefore adoption328ab1f
line 721why, one step back721 @click.option("--arg", nargs=2, multiple=True)Merge branch 'main' into stableDavid Lordbefore adoption81a482f
line 722why, one step back722 def cmd(arg):Bump actions/cache from 3.2.3 to 3.2.4 (#2446)David Lordbefore adoption328ab1f
lines 723–724why, one step back723 for item in arg:724 click.echo("|".join(item))Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 725–726why, one step back725726 result = runner.invoke(Bump actions/cache from 3.2.3 to 3.2.4 (#2446)David Lordbefore adoption328ab1f
line 727why, one step back727 cmd, [], auto_envvar_prefix="TEST", env={"TEST_ARG": "x 1 y 2"}Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 728–729why, one step back728 )729 assert not result.exceptionBump actions/cache from 3.2.3 to 3.2.4 (#2446)David Lordbefore adoption328ab1f
line 730why, one step back730 assert result.output == "x|1\ny|2\n"Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 731–732why, one step back731732Bump actions/cache from 3.2.3 to 3.2.4 (#2446)David Lordbefore adoption328ab1f
line 733why, one step back733def test_show_envvar(runner):Merge branch 'main' into stableDavid Lordbefore adoption81a482f
line 734why, one step back734 @click.command()Bump actions/cache from 3.2.3 to 3.2.4 (#2446)David Lordbefore adoption328ab1f
line 735why, one step back735 @click.option("--arg1", envvar="ARG1", show_envvar=True)Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 736–740why, one step back736 def cmd(arg):737 pass738739 result = runner.invoke(cmd, ["--help"])740 assert not result.exceptionBump actions/cache from 3.2.3 to 3.2.4 (#2446)David Lordbefore adoption328ab1f
line 741why, one step back741 assert "ARG1" in result.outputMerge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 742–743why, one step back742743Bump actions/cache from 3.2.3 to 3.2.4 (#2446)David Lordbefore adoption328ab1f
line 744why, one step back744def test_show_envvar_auto_prefix(runner):Merge branch 'main' into stableDavid Lordbefore adoption81a482f
line 745why, one step back745 @click.command()Bump actions/cache from 3.2.3 to 3.2.4 (#2446)David Lordbefore adoption328ab1f
line 746why, one step back746 @click.option("--arg1", show_envvar=True)Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 747–749why, one step back747 def cmd(arg):748 pass749Bump actions/cache from 3.2.3 to 3.2.4 (#2446)David Lordbefore adoption328ab1f
line 750why, one step back750 result = runner.invoke(cmd, ["--help"], auto_envvar_prefix="TEST")Merge branch 'main' into stableDavid Lordbefore adoption81a482f
line 751why, one step back751 assert not result.exceptionBump actions/cache from 3.2.3 to 3.2.4 (#2446)David Lordbefore adoption328ab1f
line 752why, one step back752 assert "TEST_ARG1" in result.outputMerge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 753–754why, one step back753754Bump actions/cache from 3.2.3 to 3.2.4 (#2446)David Lordbefore adoption328ab1f
lines 755–756why, one step back755def test_show_envvar_auto_prefix_dash_in_command(runner):756 @click.group()Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 757–759why, one step back757 def cli():758 pass759Bump actions/cache from 3.2.3 to 3.2.4 (#2446)David Lordbefore adoption328ab1f
lines 760–762why, one step back760 @cli.command()761 @click.option("--baz", show_envvar=True)762 def foo_bar(baz):Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 763–764why, one step back763 pass764Bump actions/cache from 3.2.3 to 3.2.4 (#2446)David Lordbefore adoption328ab1f
line 765why, one step back765 result = runner.invoke(cli, ["foo-bar", "--help"], auto_envvar_prefix="TEST")Merge branch 'main' into stableDavid Lordbefore adoption81a482f
line 766why, one step back766 assert not result.exceptionBump actions/cache from 3.2.3 to 3.2.4 (#2446)David Lordbefore adoption328ab1f
line 767why, one step back767 assert "TEST_FOO_BAR_BAZ" in result.outputMerge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 768–769why, one step back768769Bump actions/cache from 3.2.3 to 3.2.4 (#2446)David Lordbefore adoption328ab1f
lines 770–771why, one step back770def test_custom_validation(runner):771 def validate_pos_int(ctx, param, value):Merge branch 'main' into stableDavid Lordbefore adoption81a482f
line 772why, one step back772 if value < 0:Bump actions/cache from 3.2.3 to 3.2.4 (#2446)David Lordbefore adoption328ab1f
line 773why, one step back773 raise click.BadParameter("Value needs to be positive")Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 774–776why, one step back774 return value775776 @click.command()Bump actions/cache from 3.2.3 to 3.2.4 (#2446)David Lordbefore adoption328ab1f
line 777why, one step back777 @click.option("--foo", callback=validate_pos_int, default=1)Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 778–780why, one step back778 def cmd(foo):779 click.echo(foo)780Bump actions/cache from 3.2.3 to 3.2.4 (#2446)David Lordbefore adoption328ab1f
lines 781–782why, one step back781 result = runner.invoke(cmd, ["--foo", "-1"])782 assert "Invalid value for '--foo': Value needs to be positive" in result.outputMerge branch 'main' into stableDavid Lordbefore adoption81a482f
line 783why, one step back783Bump actions/cache from 3.2.3 to 3.2.4 (#2446)David Lordbefore adoption328ab1f
line 784why, one step back784 result = runner.invoke(cmd, ["--foo", "42"])Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 785–787why, one step back785 assert result.output == "42\n"786787Bump actions/cache from 3.2.3 to 3.2.4 (#2446)David Lordbefore adoption328ab1f
lines 788–789why, one step back788def test_callback_validates_prompt(runner, monkeypatch):789 def validate(ctx, param, value):Merge branch 'main' into stableDavid Lordbefore adoption81a482f
line 790why, one step back790 if value < 0:Bump actions/cache from 3.2.3 to 3.2.4 (#2446)David Lordbefore adoption328ab1f
line 791why, one step back791 raise click.BadParameter("should be positive")Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 792–795why, one step back792793 return value794795 @click.command()Bump actions/cache from 3.2.3 to 3.2.4 (#2446)David Lordbefore adoption328ab1f
lines 796–797why, one step back796 @click.option("-a", type=int, callback=validate, prompt=True)797 def cli(a):Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 798–799why, one step back798 click.echo(a)799Bump actions/cache from 3.2.3 to 3.2.4 (#2446)David Lordbefore adoption328ab1f
lines 800–801why, one step back800 result = runner.invoke(cli, input="-12\n60\n")801 assert result.output == "A: -12\nError: should be positive\nA: 60\n60\n"Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 802–803why, one step back802803Bump actions/cache from 3.2.3 to 3.2.4 (#2446)David Lordbefore adoption328ab1f
line 804why, one step back804def test_winstyle_options(runner):Merge branch 'main' into stableDavid Lordbefore adoption81a482f
line 805why, one step back805 @click.command()Bump actions/cache from 3.2.3 to 3.2.4 (#2446)David Lordbefore adoption328ab1f
lines 806–808why, one step back806 @click.option("/debug;/no-debug", help="Enables or disables debug mode.")807 def cmd(debug):808 click.echo(debug)Merge branch 'main' into stableDavid Lordbefore adoption81a482f
line 809why, one step back809Bump actions/cache from 3.2.3 to 3.2.4 (#2446)David Lordbefore adoption328ab1f
line 810why, one step back810 result = runner.invoke(cmd, ["/debug"], help_option_names=["/?"])Merge branch 'main' into stableDavid Lordbefore adoption81a482f
line 811why, one step back811 assert result.output == "True\n"Bump actions/cache from 3.2.3 to 3.2.4 (#2446)David Lordbefore adoption328ab1f
line 812why, one step back812 result = runner.invoke(cmd, ["/no-debug"], help_option_names=["/?"])Merge branch 'main' into stableDavid Lordbefore adoption81a482f
line 813why, one step back813 assert result.output == "False\n"Bump actions/cache from 3.2.3 to 3.2.4 (#2446)David Lordbefore adoption328ab1f
line 814why, one step back814 result = runner.invoke(cmd, [], help_option_names=["/?"])Merge branch 'main' into stableDavid Lordbefore adoption81a482f
line 815why, one step back815 assert result.output == "False\n"Bump actions/cache from 3.2.3 to 3.2.4 (#2446)David Lordbefore adoption328ab1f
lines 816–818why, one step back816 result = runner.invoke(cmd, ["/?"], help_option_names=["/?"])817 assert "/debug; /no-debug Enables or disables debug mode." in result.output818 assert "/? Show this message and exit." in result.outputMerge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 819–820why, one step back819820Bump actions/cache from 3.2.3 to 3.2.4 (#2446)David Lordbefore adoption328ab1f
line 821why, one step back821def test_legacy_options(runner):Merge branch 'main' into stableDavid Lordbefore adoption81a482f
line 822why, one step back822 @click.command()Bump actions/cache from 3.2.3 to 3.2.4 (#2446)David Lordbefore adoption328ab1f
lines 823–825why, one step back823 @click.option("-whatever")824 def cmd(whatever):825 click.echo(whatever)Merge branch 'main' into stableDavid Lordbefore adoption81a482f
line 826why, one step back826Bump actions/cache from 3.2.3 to 3.2.4 (#2446)David Lordbefore adoption328ab1f
line 827why, one step back827 result = runner.invoke(cmd, ["-whatever", "42"])Merge branch 'main' into stableDavid Lordbefore adoption81a482f
line 828why, one step back828 assert result.output == "42\n"Bump actions/cache from 3.2.3 to 3.2.4 (#2446)David Lordbefore adoption328ab1f
lines 829–830why, one step back829 result = runner.invoke(cmd, ["-whatever=23"])830 assert result.output == "23\n"Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 831–852why, one step back831832833@pytest.mark.parametrize(834 ("value", "expect_missing", "processed_value"),835 [836 (UNSET, True, None),837 (None, False, None),838 # Default type of the argument is str, so everything is processed as strings.839 ("", False, ""),840 (" ", False, " "),841 ("foo", False, "foo"),842 ("12", False, "12"),843 (12, False, "12"),844 (12.1, False, "12.1"),845 (list(), False, "[]"),846 (tuple(), False, "()"),847 (set(), False, "set()"),848 (frozenset(), False, "frozenset()"),849 (dict(), False, "{}"),850 ],851)852def test_required_option(value, expect_missing, processed_value):Bump actions/cache from 3.2.3 to 3.2.4 (#2446)David Lordbefore adoption328ab1f
line 853why, one step back853 ctx = click.Context(click.Command(""))Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 854–865why, one step back854 argument = click.Option(["-a"], required=True)855856 if expect_missing:857 with pytest.raises(click.MissingParameter) as excinfo:858 argument.process_value(ctx, value)859 assert str(excinfo.value) == "Missing parameter: a"860861 else:862 value = argument.process_value(ctx, value)863 assert value == processed_value864865Bump actions/cache from 3.2.3 to 3.2.4 (#2446)David Lordbefore adoption328ab1f
line 866why, one step back866def test_missing_required_flag(runner):Merge branch 'main' into stableDavid Lordbefore adoption81a482f
line 867why, one step back867 cli = click.Command(Bump actions/cache from 3.2.3 to 3.2.4 (#2446)David Lordbefore adoption328ab1f
line 868why, one step back868 "cli", params=[click.Option(["--on/--off"], is_flag=True, required=True)]Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 869–871why, one step back869 )870 result = runner.invoke(cli)871 assert result.exit_code == 2Bump actions/cache from 3.2.3 to 3.2.4 (#2446)David Lordbefore adoption328ab1f
line 872why, one step back872 assert "Error: Missing option '--on'." in result.outputMerge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 873–874why, one step back873874Bump actions/cache from 3.2.3 to 3.2.4 (#2446)David Lordbefore adoption328ab1f
line 875why, one step back875def test_missing_choice(runner):Merge branch 'main' into stableDavid Lordbefore adoption81a482f
line 876why, one step back876 @click.command()Bump actions/cache from 3.2.3 to 3.2.4 (#2446)David Lordbefore adoption328ab1f
line 877why, one step back877 @click.option("--foo", type=click.Choice(["foo", "bar"]), required=True)Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 878–882why, one step back878 def cmd(foo):879 click.echo(foo)880881 result = runner.invoke(cmd)882 assert result.exit_code == 2Bump actions/cache from 3.2.3 to 3.2.4 (#2446)David Lordbefore adoption328ab1f
lines 883–887why, one step back883 error, separator, choices = result.output.partition("Choose from")884 assert "Error: Missing option '--foo'. " in error885 assert "Choose from" in separator886 assert "foo" in choices887 assert "bar" in choicesMerge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 888–889why, one step back888889show env var in error hint (#2696)KotlinIslandbefore adoption1b524bb
line 890why, one step back890def test_missing_envvar(runner):Merge branch 'main' into stableDavid Lordbefore adoption81a482f
line 891why, one step back891 cli = click.Command(show env var in error hint (#2696)KotlinIslandbefore adoption1b524bb
line 892why, one step back892 "cli", params=[click.Option(["--foo"], envvar="bar", required=True)]Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 893–898why, one step back893 )894 result = runner.invoke(cli)895 assert result.exit_code == 2896 assert "Error: Missing option '--foo'." in result.output897 cli = click.Command(898 "cli",show env var in error hint (#2696)KotlinIslandbefore adoption1b524bb
line 899why, one step back899 params=[click.Option(["--foo"], envvar="bar", show_envvar=True, required=True)],Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 900–902why, one step back900 )901 result = runner.invoke(cli)902 assert result.exit_code == 2show env var in error hint (#2696)KotlinIslandbefore adoption1b524bb
line 903why, one step back903 assert "Error: Missing option '--foo' (env var: 'bar')." in result.outputMerge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 904–905why, one step back904905 cli = click.Command(Don't include envvar in error hint when envvar not configured (#2972)Rowlando13before adoption0be2bd3
line 906why, one step back906 "cli", params=[click.Option(["--foo"], show_envvar=True, required=True)]Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 907–912why, one step back907 )908 result = runner.invoke(cli)909 assert result.exit_code == 2910 assert "Error: Missing option '--foo'." in result.output911912Bump actions/cache from 3.2.3 to 3.2.4 (#2446)David Lordbefore adoption328ab1f
line 913why, one step back913def test_case_insensitive_choice(runner):Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 914–922why, one step back914 @click.command()915 @click.option("--foo", type=click.Choice(["Orange", "Apple"], case_sensitive=False))916 def cmd(foo):917 click.echo(foo)918919 result = runner.invoke(cmd, ["--foo", "apple"])920 assert result.exit_code == 0921 assert result.output == "Apple\n"922Bump actions/cache from 3.2.3 to 3.2.4 (#2446)David Lordbefore adoption328ab1f
line 923why, one step back923 result = runner.invoke(cmd, ["--foo", "oRANGe"])Merge branch 'main' into stableDavid Lordbefore adoption81a482f
line 924why, one step back924 assert result.exit_code == 0Bump actions/cache from 3.2.3 to 3.2.4 (#2446)David Lordbefore adoption328ab1f
line 925why, one step back925 assert result.output == "Orange\n"Merge branch 'main' into stableDavid Lordbefore adoption81a482f
line 926why, one step back926Bump actions/cache from 3.2.3 to 3.2.4 (#2446)David Lordbefore adoption328ab1f
line 927why, one step back927 result = runner.invoke(cmd, ["--foo", "Apple"])Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 928–931why, one step back928 assert result.exit_code == 0929 assert result.output == "Apple\n"930931 @click.command()Bump actions/cache from 3.2.3 to 3.2.4 (#2446)David Lordbefore adoption328ab1f
lines 932–933why, one step back932 @click.option("--foo", type=click.Choice(["Orange", "Apple"]))933 def cmd2(foo):Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 934–935why, one step back934 click.echo(foo)935Bump actions/cache from 3.2.3 to 3.2.4 (#2446)David Lordbefore adoption328ab1f
line 936why, one step back936 result = runner.invoke(cmd2, ["--foo", "apple"])Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 937–938why, one step back937 assert result.exit_code == 2938Bump actions/cache from 3.2.3 to 3.2.4 (#2446)David Lordbefore adoption328ab1f
line 939why, one step back939 result = runner.invoke(cmd2, ["--foo", "oRANGe"])Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 940–941why, one step back940 assert result.exit_code == 2941Bump actions/cache from 3.2.3 to 3.2.4 (#2446)David Lordbefore adoption328ab1f
line 942why, one step back942 result = runner.invoke(cmd2, ["--foo", "Apple"])Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 943–945why, one step back943 assert result.exit_code == 0944945Bump actions/cache from 3.2.3 to 3.2.4 (#2446)David Lordbefore adoption328ab1f
line 946why, one step back946def test_case_insensitive_choice_returned_exactly(runner):Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 947–956why, one step back947 @click.command()948 @click.option("--foo", type=click.Choice(["Orange", "Apple"], case_sensitive=False))949 def cmd(foo):950 click.echo(foo)951952 result = runner.invoke(cmd, ["--foo", "apple"])953 assert result.exit_code == 0954 assert result.output == "Apple\n"955956Bump actions/cache from 3.2.3 to 3.2.4 (#2446)David Lordbefore adoption328ab1f
line 957why, one step back957def test_option_help_preserve_paragraphs(runner):Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 958–959why, one step back958 @click.command()959 @click.option(Bump actions/cache from 3.2.3 to 3.2.4 (#2446)David Lordbefore adoption328ab1f
lines 960–963why, one step back960 "-C",961 "--config",962 type=click.Path(),963 help="""Configuration file to use.Merge branch 'main' into stableDavid Lordbefore adoption81a482f
line 964why, one step back964Bump actions/cache from 3.2.3 to 3.2.4 (#2446)David Lordbefore adoption328ab1f
lines 965–967why, one step back965 If not given, the environment variable CONFIG_FILE is consulted966 and used if set. If neither are given, a default configuration967 file is loaded.""",Merge branch 'main' into stableDavid Lordbefore adoption81a482f
line 968why, one step back968 )Bump actions/cache from 3.2.3 to 3.2.4 (#2446)David Lordbefore adoption328ab1f
line 969why, one step back969 def cmd(config):Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 970–973why, one step back970 pass971972 result = runner.invoke(cmd, ["--help"])973 assert result.exit_code == 0Bump actions/cache from 3.2.3 to 3.2.4 (#2446)David Lordbefore adoption328ab1f
line 974why, one step back974 i = " " * 21Merge branch 'main' into stableDavid Lordbefore adoption81a482f
line 975why, one step back975 assert (Bump actions/cache from 3.2.3 to 3.2.4 (#2446)David Lordbefore adoption328ab1f
lines 976–981why, one step back976 " -C, --config PATH Configuration file to use.\n"977 f"{i}\n"978 f"{i}If not given, the environment variable CONFIG_FILE is\n"979 f"{i}consulted and used if set. If neither are given, a default\n"980 f"{i}configuration file is loaded."981 ) in result.outputMerge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 982–983why, one step back982983Bump actions/cache from 3.2.3 to 3.2.4 (#2446)David Lordbefore adoption328ab1f
lines 984–988why, one step back984def test_argument_custom_class(runner):985 class CustomArgument(click.Argument):986 def get_default(self, ctx, call=True):987 """a dumb override of a default value for testing"""988 return "I am a default"Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 989–990why, one step back989990 @click.command()Bump actions/cache from 3.2.3 to 3.2.4 (#2446)David Lordbefore adoption328ab1f
lines 991–993why, one step back991 @click.argument("testarg", cls=CustomArgument, default="you wont see me")992 def cmd(testarg):993 click.echo(testarg)Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 994–995why, one step back994995 result = runner.invoke(cmd)Bump actions/cache from 3.2.3 to 3.2.4 (#2446)David Lordbefore adoption328ab1f
line 996why, one step back996 assert "I am a default" in result.outputMerge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 997–999why, one step back997 assert "you wont see me" not in result.output998999Bump actions/cache from 3.2.3 to 3.2.4 (#2446)David Lordbefore adoption328ab1f
line 1000why, one step back1000def test_option_custom_class(runner):Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 1001–1006why, one step back1001 class CustomOption(click.Option):1002 def get_help_record(self, ctx):1003 """a dumb override of a help text for testing"""1004 return ("--help", "I am a help text")10051006 @click.command()Bump actions/cache from 3.2.3 to 3.2.4 (#2446)David Lordbefore adoption328ab1f
lines 1007–1008why, one step back1007 @click.option("--testoption", cls=CustomOption, help="you wont see me")1008 def cmd(testoption):Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 1009–1011why, one step back1009 click.echo(testoption)10101011 result = runner.invoke(cmd, ["--help"])Bump actions/cache from 3.2.3 to 3.2.4 (#2446)David Lordbefore adoption328ab1f
line 1012why, one step back1012 assert "I am a help text" in result.outputMerge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 1013–1015why, one step back1013 assert "you wont see me" not in result.output10141015Ensure that `type_cast_value` never gets `UNSET` (#3090)Rowlando13before adoption8d4051e
lines 1016–1051why, one step back1016@pytest.mark.parametrize(1017 ("param_decl", "option_kwargs", "pass_argv"),1018 (1019 # there is a large potential parameter space to explore here1020 # this is just a very small sample of it1021 ("--opt", {}, []),1022 ("--opt", {"multiple": True}, []),1023 ("--opt", {"is_flag": True}, []),1024 ("--opt/--no-opt", {"is_flag": True, "default": None}, []),1025 ("--req", {"is_flag": True, "required": True}, ["--req"]),1026 ),1027)1028def test_option_custom_class_can_override_type_cast_value_and_never_sees_unset(1029 runner, param_decl, option_kwargs, pass_argv1030):1031 """1032 Test that overriding type_cast_value is supported10331034 In particular, the option is never passed an UNSET sentinel value.1035 """10361037 class CustomOption(click.Option):1038 def type_cast_value(self, ctx, value):1039 assert value is not UNSET1040 return value10411042 @click.command()1043 @click.option("myparam", param_decl, **option_kwargs, cls=CustomOption)1044 def cmd(myparam):1045 click.echo("ok")10461047 result = runner.invoke(cmd, pass_argv)1048 assert not result.exception1049 assert result.exit_code == 010501051Bump actions/cache from 3.2.3 to 3.2.4 (#2446)David Lordbefore adoption328ab1f
lines 1052–1053why, one step back1052def test_option_custom_class_reusable(runner):1053 """Ensure we can reuse a custom class option. See Issue #926"""Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 1054–1059why, one step back10541055 class CustomOption(click.Option):1056 def get_help_record(self, ctx):1057 """a dumb override of a help text for testing"""1058 return ("--help", "I am a help text")1059Bump actions/cache from 3.2.3 to 3.2.4 (#2446)David Lordbefore adoption328ab1f
lines 1060–1061why, one step back1060 # Assign to a variable to re-use the decorator.1061 testoption = click.option("--testoption", cls=CustomOption, help="you wont see me")Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 1062–1064why, one step back10621063 @click.command()1064 @testoptionBump actions/cache from 3.2.3 to 3.2.4 (#2446)David Lordbefore adoption328ab1f
line 1065why, one step back1065 def cmd1(testoption):Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 1066–1069why, one step back1066 click.echo(testoption)10671068 @click.command()1069 @testoptionBump actions/cache from 3.2.3 to 3.2.4 (#2446)David Lordbefore adoption328ab1f
line 1070why, one step back1070 def cmd2(testoption):Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 1071–1072why, one step back1071 click.echo(testoption)1072Bump actions/cache from 3.2.3 to 3.2.4 (#2446)David Lordbefore adoption328ab1f
lines 1073–1077why, one step back1073 # Both of the commands should have the --help option now.1074 for cmd in (cmd1, cmd2):1075 result = runner.invoke(cmd, ["--help"])1076 assert "I am a help text" in result.output1077 assert "you wont see me" not in result.outputMerge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 1078–1079why, one step back10781079Move `--help` option defaults from its class to its decorator (#2840)Rowlando13before adoptionc9f7d9d
line 1080why, one step back1080@pytest.mark.parametrize("custom_class", (True, False))Merge branch 'main' into stableDavid Lordbefore adoption81a482f
line 1081why, one step back1081@pytest.mark.parametrize(Move `--help` option defaults from its class to its decorator (#2840)Rowlando13before adoptionc9f7d9d
line 1082why, one step back1082 ("name_specs", "expected"),Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 1083–1084why, one step back1083 (1084 (Move `--help` option defaults from its class to its decorator (#2840)Rowlando13before adoptionc9f7d9d
lines 1085–1086why, one step back1085 ("-h", "--help"),1086 " -h, --help Show this message and exit.\n",Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 1087–1088why, one step back1087 ),1088 (Move `--help` option defaults from its class to its decorator (#2840)Rowlando13before adoptionc9f7d9d
lines 1089–1090why, one step back1089 ("-h",),1090 " -h Show this message and exit.\n"Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 1091–1093why, one step back1091 " --help Show this message and exit.\n",1092 ),1093 (Move `--help` option defaults from its class to its decorator (#2840)Rowlando13before adoptionc9f7d9d
line 1094why, one step back1094 ("--help",),Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 1095–1098why, one step back1095 " --help Show this message and exit.\n",1096 ),1097 ),1098)Move `--help` option defaults from its class to its decorator (#2840)Rowlando13before adoptionc9f7d9d
lines 1099–1100why, one step back1099def test_help_option_custom_names_and_class(runner, custom_class, name_specs, expected):1100 class CustomHelpOption(click.Option):Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 1101–1102why, one step back1101 pass1102Move `--help` option defaults from its class to its decorator (#2840)Rowlando13before adoptionc9f7d9d
lines 1103–1105why, one step back1103 option_attrs = {}1104 if custom_class:1105 option_attrs["cls"] = CustomHelpOptionMerge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 1106–1107why, one step back11061107 @click.command()Move `--help` option defaults from its class to its decorator (#2840)Rowlando13before adoptionc9f7d9d
lines 1108–1109why, one step back1108 @click.help_option(*name_specs, **option_attrs)1109 def cmd():Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 1110–1111why, one step back1110 pass1111Move `--help` option defaults from its class to its decorator (#2840)Rowlando13before adoptionc9f7d9d
lines 1112–1116why, one step back1112 for arg in name_specs:1113 result = runner.invoke(cmd, [arg])1114 assert not result.exception1115 assert result.exit_code == 01116 assert expected in result.outputMerge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 1117–1118why, one step back11171118Bump actions/cache from 3.2.3 to 3.2.4 (#2446)David Lordbefore adoption328ab1f
line 1119why, one step back1119def test_bool_flag_with_type(runner):Merge branch 'main' into stableDavid Lordbefore adoption81a482f
line 1120why, one step back1120 @click.command()Bump actions/cache from 3.2.3 to 3.2.4 (#2446)David Lordbefore adoption328ab1f
lines 1121–1122why, one step back1121 @click.option("--shout/--no-shout", default=False, type=bool)1122 def cmd(shout):Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 1123–1128why, one step back1123 pass11241125 result = runner.invoke(cmd)1126 assert not result.exception11271128Bump actions/cache from 3.2.3 to 3.2.4 (#2446)David Lordbefore adoption328ab1f
line 1129why, one step back1129def test_aliases_for_flags(runner):Merge branch 'main' into stableDavid Lordbefore adoption81a482f
line 1130why, one step back1130 @click.command()Bump actions/cache from 3.2.3 to 3.2.4 (#2446)David Lordbefore adoption328ab1f
lines 1131–1132why, one step back1131 @click.option("--warnings/--no-warnings", " /-W", default=True)1132 def cli(warnings):Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 1133–1134why, one step back1133 click.echo(warnings)1134Bump actions/cache from 3.2.3 to 3.2.4 (#2446)David Lordbefore adoption328ab1f
line 1135why, one step back1135 result = runner.invoke(cli, ["--warnings"])Merge branch 'main' into stableDavid Lordbefore adoption81a482f
line 1136why, one step back1136 assert result.output == "True\n"Bump actions/cache from 3.2.3 to 3.2.4 (#2446)David Lordbefore adoption328ab1f
line 1137why, one step back1137 result = runner.invoke(cli, ["--no-warnings"])Merge branch 'main' into stableDavid Lordbefore adoption81a482f
line 1138why, one step back1138 assert result.output == "False\n"Bump actions/cache from 3.2.3 to 3.2.4 (#2446)David Lordbefore adoption328ab1f
line 1139why, one step back1139 result = runner.invoke(cli, ["-W"])Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 1140–1142why, one step back1140 assert result.output == "False\n"11411142 @click.command()Bump actions/cache from 3.2.3 to 3.2.4 (#2446)David Lordbefore adoption328ab1f
lines 1143–1144why, one step back1143 @click.option("--warnings/--no-warnings", "-w", default=True)1144 def cli_alt(warnings):Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 1145–1146why, one step back1145 click.echo(warnings)1146Bump actions/cache from 3.2.3 to 3.2.4 (#2446)David Lordbefore adoption328ab1f
line 1147why, one step back1147 result = runner.invoke(cli_alt, ["--warnings"])Merge branch 'main' into stableDavid Lordbefore adoption81a482f
line 1148why, one step back1148 assert result.output == "True\n"Bump actions/cache from 3.2.3 to 3.2.4 (#2446)David Lordbefore adoption328ab1f
line 1149why, one step back1149 result = runner.invoke(cli_alt, ["--no-warnings"])Merge branch 'main' into stableDavid Lordbefore adoption81a482f
line 1150why, one step back1150 assert result.output == "False\n"Bump actions/cache from 3.2.3 to 3.2.4 (#2446)David Lordbefore adoption328ab1f
line 1151why, one step back1151 result = runner.invoke(cli_alt, ["-w"])Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 1152–1155why, one step back1152 assert result.output == "True\n"115311541155@pytest.mark.parametrize(Bump actions/cache from 3.2.3 to 3.2.4 (#2446)David Lordbefore adoption328ab1f
line 1156why, one step back1156 "option_args,expected",Merge branch 'main' into stableDavid Lordbefore adoption81a482f
line 1157why, one step back1157 [Bump actions/cache from 3.2.3 to 3.2.4 (#2446)David Lordbefore adoption328ab1f
lines 1158–1167why, one step back1158 (["--aggressive", "--all", "-a"], "aggressive"),1159 (["--first", "--second", "--third", "-a", "-b", "-c"], "first"),1160 (["--apple", "--banana", "--cantaloupe", "-a", "-b", "-c"], "apple"),1161 (["--cantaloupe", "--banana", "--apple", "-c", "-b", "-a"], "cantaloupe"),1162 (["-a", "-b", "-c"], "a"),1163 (["-c", "-b", "-a"], "c"),1164 (["-a", "--apple", "-b", "--banana", "-c", "--cantaloupe"], "apple"),1165 (["-c", "-a", "--cantaloupe", "-b", "--banana", "--apple"], "cantaloupe"),1166 (["--from", "-f", "_from"], "_from"),1167 (["--return", "-r", "_ret"], "_ret"),Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 1168–1169why, one step back1168 ],1169)Bump actions/cache from 3.2.3 to 3.2.4 (#2446)David Lordbefore adoption328ab1f
line 1170why, one step back1170def test_option_names(runner, option_args, expected):Merge branch 'main' into stableDavid Lordbefore adoption81a482f
line 1171why, one step back1171 @click.command()Bump actions/cache from 3.2.3 to 3.2.4 (#2446)David Lordbefore adoption328ab1f
lines 1172–1174why, one step back1172 @click.option(*option_args, is_flag=True)1173 def cmd(**kwargs):1174 click.echo(str(kwargs[expected]))Merge branch 'main' into stableDavid Lordbefore adoption81a482f
line 1175why, one step back1175Bump actions/cache from 3.2.3 to 3.2.4 (#2446)David Lordbefore adoption328ab1f
line 1176why, one step back1176 assert cmd.params[0].name == expectedMerge branch 'main' into stableDavid Lordbefore adoption81a482f
line 1177why, one step back1177Bump actions/cache from 3.2.3 to 3.2.4 (#2446)David Lordbefore adoption328ab1f
lines 1178–1181why, one step back1178 for form in option_args:1179 if form.startswith("-"):1180 result = runner.invoke(cmd, [form])1181 assert result.output == "True\n"Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 1182–1183why, one step back11821183Bump actions/cache from 3.2.3 to 3.2.4 (#2446)David Lordbefore adoption328ab1f
lines 1184–1186why, one step back1184def test_flag_duplicate_names(runner):1185 with pytest.raises(ValueError, match="cannot use the same flag for true/false"):1186 click.Option(["--foo/--foo"], default=False)Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 1187–1188why, one step back11871188Bump actions/cache from 3.2.3 to 3.2.4 (#2446)David Lordbefore adoption328ab1f
lines 1189–1193why, one step back1189@pytest.mark.parametrize(("default", "expect"), [(False, "no-cache"), (True, "cache")])1190def test_show_default_boolean_flag_name(runner, default, expect):1191 """When a boolean flag has distinct True/False opts, it should show1192 the default opt name instead of the default value. It should only1193 show one name even if multiple are declared.Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 1194–1195why, one step back1194 """1195 opt = click.Option(Bump actions/cache from 3.2.3 to 3.2.4 (#2446)David Lordbefore adoption328ab1f
line 1196why, one step back1196 ("--cache/--no-cache", "--c/--nc"),Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 1197–1198why, one step back1197 default=default,1198 show_default=True,Bump actions/cache from 3.2.3 to 3.2.4 (#2446)David Lordbefore adoption328ab1f
line 1199why, one step back1199 help="Enable/Disable the cache.",Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 1200–1201why, one step back1200 )1201 ctx = click.Context(click.Command("test"))Split generation of help extra items and rendering (#2517)Kevin Deldyckebefore adoption948d7a7
line 1202why, one step back1202 assert opt.get_help_extra(ctx) == {"default": expect}Merge branch 'main' into stableDavid Lordbefore adoption81a482f
line 1203why, one step back1203 message = opt.get_help_record(ctx)[1]Bump actions/cache from 3.2.3 to 3.2.4 (#2446)David Lordbefore adoption328ab1f
line 1204why, one step back1204 assert f"[default: {expect}]" in messageMerge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 1205–1206why, one step back12051206Bump actions/cache from 3.2.3 to 3.2.4 (#2446)David Lordbefore adoption328ab1f
lines 1207–1209why, one step back1207def test_show_true_default_boolean_flag_value(runner):1208 """When a boolean flag only has one opt and its default is True,1209 it will show the default value, not the opt name.Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 1210–1214why, one step back1210 """1211 opt = click.Option(1212 ("--cache",),1213 is_flag=True,1214 show_default=True,Bump actions/cache from 3.2.3 to 3.2.4 (#2446)David Lordbefore adoption328ab1f
line 1215why, one step back1215 default=True,Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 1216–1218why, one step back1216 help="Enable the cache.",1217 )1218 ctx = click.Context(click.Command("test"))Split generation of help extra items and rendering (#2517)Kevin Deldyckebefore adoption948d7a7
line 1219why, one step back1219 assert opt.get_help_extra(ctx) == {"default": "True"}Merge branch 'main' into stableDavid Lordbefore adoption81a482f
line 1220why, one step back1220 message = opt.get_help_record(ctx)[1]Bump actions/cache from 3.2.3 to 3.2.4 (#2446)David Lordbefore adoption328ab1f
line 1221why, one step back1221 assert "[default: True]" in messageMerge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 1222–1223why, one step back12221223Bump actions/cache from 3.2.3 to 3.2.4 (#2446)David Lordbefore adoption328ab1f
lines 1224–1227why, one step back1224@pytest.mark.parametrize("default", [False, None])1225def test_hide_false_default_boolean_flag_value(runner, default):1226 """When a boolean flag only has one opt and its default is False or1227 None, it will not show the defaultMerge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 1228–1238why, one step back1228 """1229 opt = click.Option(1230 ("--cache",),1231 is_flag=True,1232 show_default=True,1233 default=default,1234 help="Enable the cache.",1235 )1236 ctx = click.Context(click.Command("test"))1237 assert opt.get_help_extra(ctx) == {}1238 message = opt.get_help_record(ctx)[1]Bump actions/cache from 3.2.3 to 3.2.4 (#2446)David Lordbefore adoption328ab1f
line 1239why, one step back1239 assert "[default: " not in messageMerge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 1240–1241why, one step back12401241Bump actions/cache from 3.2.3 to 3.2.4 (#2446)David Lordbefore adoption328ab1f
lines 1242–1244why, one step back1242def test_show_default_string(runner):1243 """When show_default is a string show that value as default."""1244 opt = click.Option(["--limit"], show_default="unlimited")Merge branch 'main' into stableDavid Lordbefore adoption81a482f
line 1245why, one step back1245 ctx = click.Context(click.Command("cli"))Split generation of help extra items and rendering (#2517)Kevin Deldyckebefore adoption948d7a7
line 1246why, one step back1246 assert opt.get_help_extra(ctx) == {"default": "(unlimited)"}Merge branch 'main' into stableDavid Lordbefore adoption81a482f
line 1247why, one step back1247 message = opt.get_help_record(ctx)[1]Bump actions/cache from 3.2.3 to 3.2.4 (#2446)David Lordbefore adoption328ab1f
line 1248why, one step back1248 assert "[default: (unlimited)]" in messageMerge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 1249–1250why, one step back12491250Merge branch '8.1.x'David Lordbefore adoption99015e1
lines 1251–1253why, one step back1251def test_show_default_with_empty_string(runner):1252 """When show_default is True and default is set to an empty string."""1253 opt = click.Option(["--limit"], default="", show_default=True)Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 1254–1255why, one step back1254 ctx = click.Context(click.Command("cli"))1255 message = opt.get_help_record(ctx)[1]Merge branch '8.1.x'David Lordbefore adoption99015e1
line 1256why, one step back1256 assert '[default: ""]' in messageMerge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 1257–1258why, one step back12571258Bump actions/cache from 3.2.3 to 3.2.4 (#2446)David Lordbefore adoption328ab1f
lines 1259–1261why, one step back1259def test_do_not_show_no_default(runner):1260 """When show_default is True and no default is set do not show None."""1261 opt = click.Option(["--limit"], show_default=True)Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 1262–1264why, one step back1262 ctx = click.Context(click.Command("cli"))1263 assert opt.get_help_extra(ctx) == {}1264 message = opt.get_help_record(ctx)[1]Bump actions/cache from 3.2.3 to 3.2.4 (#2446)David Lordbefore adoption328ab1f
line 1265why, one step back1265 assert "[default: None]" not in messageMerge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 1266–1267why, one step back12661267Bump actions/cache from 3.2.3 to 3.2.4 (#2446)David Lordbefore adoption328ab1f
lines 1268–1270why, one step back1268def test_do_not_show_default_empty_multiple():1269 """When show_default is True and multiple=True is set, it should not1270 print empty default value in --help output.Merge branch 'main' into stableDavid Lordbefore adoption81a482f
line 1271why, one step back1271 """Bump actions/cache from 3.2.3 to 3.2.4 (#2446)David Lordbefore adoption328ab1f
line 1272why, one step back1272 opt = click.Option(["-a"], multiple=True, help="values", show_default=True)Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 1273–1275why, one step back1273 ctx = click.Context(click.Command("cli"))1274 assert opt.get_help_extra(ctx) == {}1275 message = opt.get_help_record(ctx)[1]Bump actions/cache from 3.2.3 to 3.2.4 (#2446)David Lordbefore adoption328ab1f
line 1276why, one step back1276 assert message == "values"Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 1277–1279why, one step back127712781279@pytest.mark.parametrize(Split generation of help extra items and rendering (#2517)Kevin Deldyckebefore adoption948d7a7
line 1280why, one step back1280 ("ctx_value", "opt_value", "extra_value", "expect"),Merge branch 'main' into stableDavid Lordbefore adoption81a482f
line 1281why, one step back1281 [Split generation of help extra items and rendering (#2517)Kevin Deldyckebefore adoption948d7a7
lines 1282–1291why, one step back1282 (None, None, {}, False),1283 (None, False, {}, False),1284 (None, True, {"default": "1"}, True),1285 (False, None, {}, False),1286 (False, False, {}, False),1287 (False, True, {"default": "1"}, True),1288 (True, None, {"default": "1"}, True),1289 (True, False, {}, False),1290 (True, True, {"default": "1"}, True),1291 (False, "one", {"default": "(one)"}, True),Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 1292–1293why, one step back1292 ],1293)Split generation of help extra items and rendering (#2517)Kevin Deldyckebefore adoption948d7a7
line 1294why, one step back1294def test_show_default_precedence(ctx_value, opt_value, extra_value, expect):Bump actions/cache from 3.2.3 to 3.2.4 (#2446)David Lordbefore adoption328ab1f
lines 1295–1296why, one step back1295 ctx = click.Context(click.Command("test"), show_default=ctx_value)1296 opt = click.Option("-a", default=1, help="value", show_default=opt_value)Split generation of help extra items and rendering (#2517)Kevin Deldyckebefore adoption948d7a7
line 1297why, one step back1297 assert opt.get_help_extra(ctx) == extra_valueBump actions/cache from 3.2.3 to 3.2.4 (#2446)David Lordbefore adoption328ab1f
lines 1298–1299why, one step back1298 help = opt.get_help_record(ctx)[1]1299 assert ("default:" in help) is expectMerge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 1300–1302why, one step back130013011302@pytest.mark.parametrize(Bump actions/cache from 3.2.3 to 3.2.4 (#2446)David Lordbefore adoption328ab1f
line 1303why, one step back1303 ("args", "expect"),Merge branch 'main' into stableDavid Lordbefore adoption81a482f
line 1304why, one step back1304 [Bump actions/cache from 3.2.3 to 3.2.4 (#2446)David Lordbefore adoption328ab1f
lines 1305–1318why, one step back1305 (None, (None, None, ())),1306 (["--opt"], ("flag", None, ())),1307 (["--opt", "-a", 42], ("flag", "42", ())),1308 (["--opt", "test", "-a", 42], ("test", "42", ())),1309 (["--opt=test", "-a", 42], ("test", "42", ())),1310 (["-o"], ("flag", None, ())),1311 (["-o", "-a", 42], ("flag", "42", ())),1312 (["-o", "test", "-a", 42], ("test", "42", ())),1313 (["-otest", "-a", 42], ("test", "42", ())),1314 (["a", "b", "c"], (None, None, ("a", "b", "c"))),1315 (["--opt", "a", "b", "c"], ("a", None, ("b", "c"))),1316 (["--opt", "test"], ("test", None, ())),1317 (["-otest", "a", "b", "c"], ("test", None, ("a", "b", "c"))),1318 (["--opt=test", "a", "b", "c"], ("test", None, ("a", "b", "c"))),Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 1319–1320why, one step back1319 ],1320)Bump actions/cache from 3.2.3 to 3.2.4 (#2446)David Lordbefore adoption328ab1f
line 1321why, one step back1321def test_option_with_optional_value(runner, args, expect):Merge branch 'main' into stableDavid Lordbefore adoption81a482f
line 1322why, one step back1322 @click.command()Bump actions/cache from 3.2.3 to 3.2.4 (#2446)David Lordbefore adoption328ab1f
lines 1323–1327why, one step back1323 @click.option("-o", "--opt", is_flag=False, flag_value="flag")1324 @click.option("-a")1325 @click.argument("b", nargs=-1)1326 def cli(opt, a, b):1327 return opt, a, bMerge branch 'main' into stableDavid Lordbefore adoption81a482f
line 1328why, one step back1328Bump actions/cache from 3.2.3 to 3.2.4 (#2446)David Lordbefore adoption328ab1f
lines 1329–1330why, one step back1329 result = runner.invoke(cli, args, standalone_mode=False, catch_exceptions=False)1330 assert result.return_value == expectMerge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 1331–1332why, one step back13311332Bump actions/cache from 3.2.3 to 3.2.4 (#2446)David Lordbefore adoption328ab1f
line 1333why, one step back1333def test_multiple_option_with_optional_value(runner):Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 1334–1335why, one step back1334 cli = click.Command(1335 "cli",Bump actions/cache from 3.2.3 to 3.2.4 (#2446)David Lordbefore adoption328ab1f
lines 1336–1341why, one step back1336 params=[1337 click.Option(["-f"], is_flag=False, flag_value="flag", multiple=True),1338 click.Option(["-a"]),1339 click.Argument(["b"], nargs=-1),1340 ],1341 callback=lambda **kwargs: kwargs,Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 1342–1343why, one step back1342 )1343 result = runner.invoke(Bump actions/cache from 3.2.3 to 3.2.4 (#2446)David Lordbefore adoption328ab1f
lines 1344–1347why, one step back1344 cli,1345 ["-f", "-f", "other", "-f", "-a", "1", "a", "b"],1346 standalone_mode=False,1347 catch_exceptions=False,Merge branch 'main' into stableDavid Lordbefore adoption81a482f
line 1348why, one step back1348 )Bump actions/cache from 3.2.3 to 3.2.4 (#2446)David Lordbefore adoption328ab1f
lines 1349–1353why, one step back1349 assert result.return_value == {1350 "f": ("flag", "other", "flag"),1351 "a": "1",1352 "b": ("a", "b"),1353 }Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 1354–1355why, one step back13541355Bump actions/cache from 3.2.3 to 3.2.4 (#2446)David Lordbefore adoption328ab1f
lines 1356–1357why, one step back1356def test_type_from_flag_value():1357 param = click.Option(["-a", "x"], default=True, flag_value=4)Merge branch 'main' into stableDavid Lordbefore adoption81a482f
line 1358why, one step back1358 assert param.type is click.INTBump actions/cache from 3.2.3 to 3.2.4 (#2446)David Lordbefore adoption328ab1f
line 1359why, one step back1359 param = click.Option(["-b", "x"], flag_value=8)Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 1360–1463why, one step back1360 assert param.type is click.INT136113621363@pytest.mark.parametrize(1364 ("opt_params", "args", "expected"),1365 [1366 # The type passed to the option is responsible to converting the value, whether1367 # we pass the option flag or not.1368 ({"type": bool}, [], False),1369 ({"type": bool}, ["--foo"], True),1370 ({"type": click.BOOL}, [], False),1371 ({"type": click.BOOL}, ["--foo"], True),1372 ({"type": str}, [], None),1373 ({"type": str}, ["--foo"], "True"),1374 # Default value is given as-is to the --foo option when it is not passed,1375 # whatever the type. Now if --foo is passed, the value is always True, whatever1376 # the type. In both case the type of the option is responsible for the1377 # conversion of the value.1378 ({"type": bool, "default": True}, [], True),1379 ({"type": bool, "default": True}, ["--foo"], True),1380 ({"type": bool, "default": False}, [], False),1381 ({"type": bool, "default": False}, ["--foo"], True),1382 # ({"type": bool, "default": "foo"}, [], "foo"),1383 ({"type": bool, "default": "foo"}, ["--foo"], True),1384 ({"type": bool, "default": None}, [], None),1385 ({"type": bool, "default": None}, ["--foo"], True),1386 ({"type": click.BOOL, "default": True}, [], True),1387 ({"type": click.BOOL, "default": True}, ["--foo"], True),1388 ({"type": click.BOOL, "default": False}, [], False),1389 ({"type": click.BOOL, "default": False}, ["--foo"], True),1390 # ({"type": click.BOOL, "default": "foo"}, [], "foo"),1391 ({"type": click.BOOL, "default": "foo"}, ["--foo"], True),1392 ({"type": click.BOOL, "default": None}, [], None),1393 ({"type": click.BOOL, "default": None}, ["--foo"], True),1394 ({"type": str, "default": True}, [], "True"),1395 ({"type": str, "default": True}, ["--foo"], "True"),1396 ({"type": str, "default": False}, [], "False"),1397 ({"type": str, "default": False}, ["--foo"], "True"),1398 ({"type": str, "default": "foo"}, [], "foo"),1399 ({"type": str, "default": "foo"}, ["--foo"], "True"),1400 ({"type": str, "default": None}, [], None),1401 ({"type": str, "default": None}, ["--foo"], "True"),1402 # Flag value is given as-is to the --foo option when it is passed, then1403 # converted by the option type.1404 ({"type": bool, "flag_value": True}, [], False),1405 ({"type": bool, "flag_value": True}, ["--foo"], True),1406 ({"type": bool, "flag_value": False}, [], False),1407 ({"type": bool, "flag_value": False}, ["--foo"], False),1408 ({"type": bool, "flag_value": None}, [], False),1409 ({"type": bool, "flag_value": None}, ["--foo"], None),1410 ({"type": click.BOOL, "flag_value": True}, [], False),1411 ({"type": click.BOOL, "flag_value": True}, ["--foo"], True),1412 ({"type": click.BOOL, "flag_value": False}, [], False),1413 ({"type": click.BOOL, "flag_value": False}, ["--foo"], False),1414 ({"type": click.BOOL, "flag_value": None}, [], False),1415 ({"type": click.BOOL, "flag_value": None}, ["--foo"], None),1416 ({"type": str, "flag_value": True}, [], None),1417 ({"type": str, "flag_value": True}, ["--foo"], "True"),1418 ({"type": str, "flag_value": False}, [], None),1419 ({"type": str, "flag_value": False}, ["--foo"], "False"),1420 ({"type": str, "flag_value": "foo"}, [], None),1421 ({"type": str, "flag_value": "foo"}, ["--foo"], "foo"),1422 ({"type": str, "flag_value": None}, [], None),1423 ({"type": str, "flag_value": None}, ["--foo"], None),1424 # Not passing --foo returns the default value as-is, in its Python type, then1425 # converted by the option type.1426 ({"type": bool, "default": True, "flag_value": True}, [], True),1427 ({"type": bool, "default": True, "flag_value": False}, [], False),1428 ({"type": bool, "default": False, "flag_value": True}, [], False),1429 ({"type": bool, "default": False, "flag_value": False}, [], False),1430 ({"type": bool, "default": None, "flag_value": True}, [], None),1431 ({"type": bool, "default": None, "flag_value": False}, [], None),1432 ({"type": str, "default": True, "flag_value": True}, [], "True"),1433 ({"type": str, "default": True, "flag_value": False}, [], "False"),1434 ({"type": str, "default": False, "flag_value": True}, [], "False"),1435 ({"type": str, "default": False, "flag_value": False}, [], "False"),1436 ({"type": str, "default": "foo", "flag_value": True}, [], "foo"),1437 ({"type": str, "default": "foo", "flag_value": False}, [], "foo"),1438 ({"type": str, "default": "foo", "flag_value": "bar"}, [], "foo"),1439 ({"type": str, "default": "foo", "flag_value": None}, [], "foo"),1440 ({"type": str, "default": None, "flag_value": True}, [], None),1441 ({"type": str, "default": None, "flag_value": False}, [], None),1442 # Passing --foo returns the flag_value that was explicitly set by the user,1443 # with its Python type, but still converted by the option type.1444 ({"type": bool, "default": True, "flag_value": True}, ["--foo"], True),1445 ({"type": bool, "default": True, "flag_value": False}, ["--foo"], False),1446 ({"type": bool, "default": False, "flag_value": True}, ["--foo"], True),1447 ({"type": bool, "default": False, "flag_value": False}, ["--foo"], False),1448 ({"type": bool, "default": None, "flag_value": True}, ["--foo"], True),1449 ({"type": bool, "default": None, "flag_value": False}, ["--foo"], False),1450 ({"type": str, "default": True, "flag_value": True}, ["--foo"], "True"),1451 ({"type": str, "default": True, "flag_value": False}, ["--foo"], "False"),1452 ({"type": str, "default": False, "flag_value": True}, ["--foo"], "True"),1453 ({"type": str, "default": False, "flag_value": False}, ["--foo"], "False"),1454 ({"type": str, "default": "foo", "flag_value": True}, ["--foo"], "True"),1455 ({"type": str, "default": "foo", "flag_value": False}, ["--foo"], "False"),1456 ({"type": str, "default": "foo", "flag_value": "bar"}, ["--foo"], "bar"),1457 ({"type": str, "default": "foo", "flag_value": None}, ["--foo"], None),1458 ({"type": str, "default": None, "flag_value": True}, ["--foo"], "True"),1459 ({"type": str, "default": None, "flag_value": False}, ["--foo"], "False"),1460 ],1461)1462def test_flag_value_and_default(runner, opt_params, args, expected):1463 @click.command()Fix reconciliation of envvar with `default`, `flag_value` and `type` parameters for flag options (#2956)Kevin Deldyckebefore adoption565f36d
line 1464why, one step back1464 @click.option("--foo", is_flag=True, **opt_params)Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 1465–1482why, one step back1465 def cmd(foo):1466 click.echo(repr(foo), nl=False)14671468 result = runner.invoke(cmd, args)1469 assert result.output == repr(expected)147014711472@pytest.mark.parametrize(1473 ("args", "opts"),1474 [1475 ([], {"type": bool, "default": "foo"}),1476 ([], {"type": click.BOOL, "default": "foo"}),1477 (["--foo"], {"type": bool, "flag_value": "foo"}),1478 (["--foo"], {"type": click.BOOL, "flag_value": "foo"}),1479 ],1480)1481def test_invalid_flag_definition(runner, args, opts):1482 @click.command()Fix flag value set when is_flag=True and type is provided (#2930)David Lordbefore adoption884af5c
line 1483why, one step back1483 @click.option("--foo", is_flag=True, **opts)Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 1484–1488why, one step back1484 def cmd(foo):1485 click.echo(foo)14861487 result = runner.invoke(cmd, args)1488 assert (Fix reconciliation of envvar with `default`, `flag_value` and `type` parameters for flag options (#2956)Kevin Deldyckebefore adoption565f36d
line 1489why, one step back1489 "Error: Invalid value for '--foo': 'foo' is not a valid boolean"Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 1490–1553why, one step back1490 in result.output1491 )149214931494@pytest.mark.parametrize(1495 ("default", "args", "expected"),1496 # These test cases are similar to the ones in1497 # tests/test_basic.py::test_flag_value_dual_options, so keep them in sync.1498 (1499 # Each option is returning its own flag_value, whatever the default is.1500 (True, ["--js"], "js"),1501 (True, ["--xml"], "xml"),1502 (False, ["--js"], "js"),1503 (False, ["--xml"], "xml"),1504 (None, ["--js"], "js"),1505 (None, ["--xml"], "xml"),1506 (UNSET, ["--js"], "js"),1507 (UNSET, ["--xml"], "xml"),1508 # Check that the last option wins when both are specified.1509 (True, ["--js", "--xml"], "xml"),1510 (True, ["--xml", "--js"], "js"),1511 # Check that the default is returned as-is when no option is specified.1512 ("js", [], "js"),1513 ("xml", [], "xml"),1514 ("jS", [], "jS"),1515 ("xMl", [], "xMl"),1516 (" ᕕ( ᐛ )ᕗ ", [], " ᕕ( ᐛ )ᕗ "),1517 (None, [], None),1518 # Special case: UNSET is not provided as-is to the callback, but normalized to1519 # None.1520 (UNSET, [], None),1521 # Special case: if default=True and flag_value is set, the value returned is the1522 # flag_value, not the True Python value itself.1523 (True, [], "js"),1524 # Non-string defaults are process as strings by the default Parameter's type.1525 (False, [], "False"),1526 (42, [], "42"),1527 (12.3, [], "12.3"),1528 ),1529)1530def test_default_dual_option_callback(runner, default, args, expected):1531 """Check how default is processed by the callback when options compete for the same1532 variable name.15331534 Reproduction of the issue reported in1535 https://github.com/pallets/click/pull/3030#discussion_r22715718191536 """15371538 def _my_func(ctx, param, value):1539 # Print the value received by the callback as-is, so we can check for it.1540 return f"Callback value: {value!r}"15411542 @click.command()1543 @click.option("--js", "fmt", flag_value="js", callback=_my_func, default=default)1544 @click.option("--xml", "fmt", flag_value="xml", callback=_my_func)1545 def main(fmt):1546 click.secho(fmt, nl=False)15471548 result = runner.invoke(main, args)1549 assert result.output == f"Callback value: {expected!r}"1550 assert result.exit_code == 0155115521553@pytest.mark.parametrize(Fix reconciliation of envvar with `default`, `flag_value` and `type` parameters for flag options (#2956)Kevin Deldyckebefore adoption565f36d
line 1554why, one step back1554 ("flag_value", "envvar_value", "expected"),Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 1555–1556why, one step back1555 [1556 # The envvar match exactly the flag value and is case-sensitive.Fix reconciliation of envvar with `default`, `flag_value` and `type` parameters for flag options (#2956)Kevin Deldyckebefore adoption565f36d
lines 1557–1559why, one step back1557 ("bar", "bar", "bar"),1558 ("BAR", "BAR", "BAR"),1559 (" bar ", " bar ", " bar "),Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 1560–1640why, one step back1560 ("42", "42", "42"),1561 ("None", "None", "None"),1562 ("True", "True", "True"),1563 ("False", "False", "False"),1564 ("true", "true", "true"),1565 ("false", "false", "false"),1566 ("A B", "A B", "A B"),1567 (" 1 2 ", " 1 2 ", " 1 2 "),1568 ("9.3", "9.3", "9.3"),1569 ("a;n", "a;n", "a;n"),1570 ("x:y", "x:y", "x:y"),1571 ("i/o", "i/o", "i/o"),1572 # Empty or absent envvar is consider unset, so the flag default value is1573 # returned, which is None in this case.1574 ("bar", "", None),1575 ("bar", None, None),1576 ("BAR", "", None),1577 ("BAR", None, None),1578 (" bar ", "", None),1579 (" bar ", None, None),1580 (42, "", None),1581 (42, None, None),1582 ("42", "", None),1583 ("42", None, None),1584 (None, "", None),1585 (None, None, None),1586 ("None", "", None),1587 ("None", None, None),1588 (True, "", None),1589 (True, None, None),1590 ("True", "", None),1591 ("True", None, None),1592 ("true", "", None),1593 ("true", None, None),1594 (False, "", None),1595 (False, None, None),1596 ("False", "", None),1597 ("False", None, None),1598 ("false", "", None),1599 ("false", None, None),1600 # Activate the flag with a value recognized as True by the envvar, which1601 # returns the flag_value.1602 ("bar", "True", "bar"),1603 ("bar", "true", "bar"),1604 ("bar", "trUe", "bar"),1605 ("bar", " TRUE ", "bar"),1606 ("bar", "1", "bar"),1607 ("bar", "yes", "bar"),1608 ("bar", "on", "bar"),1609 ("bar", "t", "bar"),1610 ("bar", "y", "bar"),1611 # Deactivating the flag with a value recognized as False by the envvar, which1612 # explicitly return the 'False' as the option is explicitly declared as a1613 # string.1614 ("bar", "False", "False"),1615 ("bar", "false", "False"),1616 ("bar", "faLse", "False"),1617 ("bar", " FALSE ", "False"),1618 ("bar", "0", "False"),1619 ("bar", "no", "False"),1620 ("bar", "off", "False"),1621 ("bar", "f", "False"),1622 ("bar", "n", "False"),1623 # Any other value than the flag_value, or a recogned True or False value, fails1624 # to explicitly activate or deactivate the flag. So the flag default value is1625 # returned, which is None in this case.1626 ("bar", " bar ", None),1627 ("bar", "BAR", None),1628 ("bar", "random", None),1629 ("bar", "bar random", None),1630 ("bar", "random bar", None),1631 ("BAR", "bar", None),1632 (" bar ", "bar", None),1633 (" bar ", "BAR", None),1634 (42, "42", None),1635 (42, "foo", None),1636 (None, "foo", None),1637 ("None", "foo", None),1638 ],1639)1640def test_envvar_string_flag_value(runner, flag_value, envvar_value, expected):Fix reconciliation of envvar with `default`, `flag_value` and `type` parameters for flag options (#2956)Kevin Deldyckebefore adoption565f36d
line 1641why, one step back1641 """Ensure that flag_value is recognized by the envvar."""Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 1642–1644why, one step back16421643 @click.command()1644 @click.option("--upper", type=str, flag_value=flag_value, envvar="UPPER")Fix reconciliation of envvar with `default`, `flag_value` and `type` parameters for flag options (#2956)Kevin Deldyckebefore adoption565f36d
line 1645why, one step back1645 def cmd(upper):Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 1646–1647why, one step back1646 click.echo(repr(upper), nl=False)1647Fix reconciliation of envvar with `default`, `flag_value` and `type` parameters for flag options (#2956)Kevin Deldyckebefore adoption565f36d
line 1648why, one step back1648 result = runner.invoke(cmd, env={"UPPER": envvar_value})Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 1649–1678why, one step back1649 assert result.exit_code == 01650 assert result.output == repr(expected)165116521653@pytest.mark.parametrize(1654 ("opt_decls", "opt_params", "expect_is_flag", "expect_is_bool_flag"),1655 [1656 # Not boolean flags.1657 ("-a", {"type": int}, False, False),1658 ("-a", {"type": bool}, False, False),1659 ("-a", {"default": True}, False, False),1660 ("-a", {"default": False}, False, False),1661 ("-a", {"flag_value": 1}, True, False),1662 # Boolean flags.1663 ("-a", {"is_flag": True}, True, True),1664 ("-a/-A", {}, True, True),1665 ("-a", {"flag_value": True}, True, True),1666 # Non-flag with flag_value.1667 ("-a", {"is_flag": False, "flag_value": 1}, False, False),1668 ],1669)1670def test_flag_auto_detection(1671 opt_decls, opt_params, expect_is_flag, expect_is_bool_flag1672):1673 option = Option([opt_decls], **opt_params)1674 assert option.is_flag is expect_is_flag1675 assert option.is_bool_flag is expect_is_bool_flag167616771678@pytest.mark.parametrize(Bump actions/cache from 3.2.3 to 3.2.4 (#2446)David Lordbefore adoption328ab1f
line 1679why, one step back1679 ("kwargs", "message"),Merge branch 'main' into stableDavid Lordbefore adoption81a482f
line 1680why, one step back1680 [Bump actions/cache from 3.2.3 to 3.2.4 (#2446)David Lordbefore adoption328ab1f
lines 1681–1682why, one step back1681 ({"count": True, "multiple": True}, "'count' is not valid with 'multiple'."),1682 ({"count": True, "is_flag": True}, "'count' is not valid with 'is_flag'."),Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 1683–1684why, one step back1683 ],1684)Bump actions/cache from 3.2.3 to 3.2.4 (#2446)David Lordbefore adoption328ab1f
lines 1685–1687why, one step back1685def test_invalid_flag_combinations(runner, kwargs, message):1686 with pytest.raises(TypeError) as e:1687 click.Option(["-a"], **kwargs)Merge branch 'main' into stableDavid Lordbefore adoption81a482f
line 1688why, one step back1688Bump actions/cache from 3.2.3 to 3.2.4 (#2446)David Lordbefore adoption328ab1f
line 1689why, one step back1689 assert message in str(e.value)Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 1690–1691why, one step back16901691Fix flag value set when is_flag=True and type is provided (#2930)David Lordbefore adoption884af5c
lines 1692–1695why, one step back1692def test_non_flag_with_non_negatable_default(runner):1693 class NonNegatable:1694 def __bool__(self):1695 raise ValueError("Cannot negate this object")Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 1696–1697why, one step back16961697 @click.command()Fix flag value set when is_flag=True and type is provided (#2930)David Lordbefore adoption884af5c
line 1698why, one step back1698 @click.option("--foo", default=NonNegatable())Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 1699–1705why, one step back1699 def cmd(foo):1700 pass17011702 result = runner.invoke(cmd)1703 assert result.exit_code == 017041705Fix rendering of Enum choices' default (#3004)Rowlando13before adoptioneaf33a9
line 1706why, one step back1706class HashType(enum.Enum):Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 1707–1711why, one step back1707 MD5 = "MD5"1708 SHA1 = "SHA1"1709 SHA256 = "SHA-256"17101711Fix rendering of Enum choices' default (#3004)Rowlando13before adoptioneaf33a9
lines 1712–1714why, one step back1712class Number(enum.IntEnum):1713 ONE = enum.auto()1714 TWO = enum.auto()Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 1715–1716why, one step back17151716Fix rendering of Enum choices' default (#3004)Rowlando13before adoptioneaf33a9
line 1717why, one step back1717class Letter(enum.StrEnum):Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 1718–1722why, one step back1718 NAME_1 = "Value-1"1719 NAME_2 = "Value_2"1720 NAME_3 = "42_value"17211722Fix rendering of Enum choices' default (#3004)Rowlando13before adoptioneaf33a9
line 1723why, one step back1723class Color(enum.Flag):Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 1724–1728why, one step back1724 RED = enum.auto()1725 GREEN = enum.auto()1726 BLUE = enum.auto()17271728Fix rendering of Enum choices' default (#3004)Rowlando13before adoptioneaf33a9
line 1729why, one step back1729class ColorInt(enum.IntFlag):Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 1730–1759why, one step back1730 RED = enum.auto()1731 GREEN = enum.auto()1732 BLUE = enum.auto()173317341735@pytest.mark.parametrize(1736 ("choices", "metavar"),1737 [1738 (["foo", "bar"], "[TEXT]"),1739 ([1, 2], "[INTEGER]"),1740 ([1.0, 2.0], "[FLOAT]"),1741 ([True, False], "[BOOLEAN]"),1742 (["foo", 1], "[TEXT|INTEGER]"),1743 (HashType, "[HASHTYPE]"),1744 (Number, "[NUMBER]"),1745 (Letter, "[LETTER]"),1746 (Color, "[COLOR]"),1747 (ColorInt, "[COLORINT]"),1748 ],1749)1750def test_choice_usage_rendering(runner, choices, metavar):1751 """BY default ``--help`` prints choice's values in the usage message.17521753 But ``show_choices=False`` makes ``--help`` prints choice's METAVAR instead of1754 values.17551756 Also check that usage error message always suggests the actual values.1757 """17581759 @click.command()Fix metavar for Choice options when show_choices=False (#2365)mrmupsbefore adoption1a4d8c1
line 1760why, one step back1760 @click.option("-g", type=click.Choice(choices))Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 1761–1765why, one step back1761 def cli_with_choices(g):1762 pass17631764 @click.command()1765 @click.option("-g", type=click.Choice(choices), show_choices=False)Fix metavar for Choice options when show_choices=False (#2365)mrmupsbefore adoption1a4d8c1
line 1766why, one step back1766 def cli_without_choices(g):Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 1767–1779why, one step back1767 pass17681769 display_values = tuple(1770 i.name if isinstance(i, enum.Enum) else str(i) for i in choices1771 )17721773 # Check that the choices values are rendered as-is in the usage message.1774 result = runner.invoke(cli_with_choices, ["--help"])1775 assert f"[{'|'.join(display_values)}]" in result.stdout1776 assert not result.stderr1777 assert result.exit_code == 017781779 # Check that the metavar is rendered instead of the choices values themselves.Fix metavar for Choice options when show_choices=False (#2365)mrmupsbefore adoption1a4d8c1
line 1780why, one step back1780 result = runner.invoke(cli_without_choices, ["--help"])Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 1781–1790why, one step back1781 assert metavar in result.stdout1782 assert not result.stderr1783 assert result.exit_code == 017841785 # Check the usage error message suggests the actual accepted values.1786 for cli in (cli_with_choices, cli_without_choices):1787 result = runner.invoke(cli, ["-g", "random"])1788 assert (1789 "\n\nError: Invalid value for '-g': 'random' is not one of "1790 f"{', '.join(map(repr, display_values))}.\n" in result.stderrtest: add two new tests for `Parameter.__init__` (recreation of #2861) (#3012)Rowlando13before adoptionc096a10
line 1791why, one step back1791 )Merge branch 'main' into stableDavid Lordbefore adoption81a482f
lines 1792–2278why, one step back1792 assert not result.stdout1793 assert result.exit_code == 2179417951796@pytest.mark.parametrize(1797 ("choices", "default", "default_string"),1798 [1799 (["foo", "bar"], "bar", "bar"),1800 # The default value is not enforced to be in the choices.1801 (["foo", "bar"], "random", "random"),1802 # None cannot be a default value as-is: it left the default value as unset.1803 (["foo", "bar"], None, None),1804 ([0, 1], 0, "0"),1805 # Values are not coerced to the type of the choice, even if equivalent.1806 ([0, 1], 0.0, "0.0"),1807 ([1, 2], 2, "2"),1808 ([1.0, 2.0], 2, "2"),1809 ([1.0, 2.0], 2.0, "2.0"),1810 ([True, False], True, "True"),1811 ([True, False], False, "False"),1812 (["foo", 1], "foo", "foo"),1813 (["foo", 1], 1, "1"),1814 # Enum choices are rendered as their names, not values.1815 # See: https://github.com/pallets/click/issues/29111816 (HashType, HashType.SHA1, "SHA1"),1817 # Enum choices allow defaults strings that are their names.1818 (HashType, HashType.SHA256, "SHA256"),1819 (HashType, "SHA256", "SHA256"),1820 (Number, Number.TWO, "TWO"),1821 (Number, "TWO", "TWO"),1822 (Letter, Letter.NAME_1, "NAME_1"),1823 (Letter, Letter.NAME_2, "NAME_2"),1824 (Letter, Letter.NAME_3, "NAME_3"),1825 (Letter, "NAME_1", "NAME_1"),1826 (Letter, "NAME_2", "NAME_2"),1827 (Letter, "NAME_3", "NAME_3"),1828 (Color, Color.GREEN, "GREEN"),1829 (Color, "GREEN", "GREEN"),1830 (ColorInt, ColorInt.GREEN, "GREEN"),1831 (ColorInt, "GREEN", "GREEN"),1832 ],1833)1834def test_choice_default_rendering(runner, choices, default, default_string):1835 @click.command()1836 @click.option("-g", type=click.Choice(choices), default=default, show_default=True)1837 def cli_with_choices(g):1838 pass18391840 # Check that the default value is kept normalized to the type of the choice.1841 assert cli_with_choices.params[0].default == default18421843 result = runner.invoke(cli_with_choices, ["--help"])1844 extra_usage = f"[default: {default_string}]"1845 if default_string is None:1846 assert extra_usage not in result.output1847 else:1848 assert extra_usage in result.output184918501851@pytest.mark.parametrize(1852 "opts_one,opts_two",1853 [1854 # No duplicate shortnames1855 (1856 ("-a", "--aardvark"),1857 ("-a", "--avocado"),1858 ),1859 # No duplicate long names1860 (1861 ("-a", "--aardvark"),1862 ("-b", "--aardvark"),1863 ),1864 ],1865)1866def test_duplicate_names_warning(runner, opts_one, opts_two):1867 @click.command()1868 @click.option(*opts_one)1869 @click.option(*opts_two)1870 def cli(one, two):1871 pass18721873 with pytest.warns(UserWarning):1874 runner.invoke(cli, [])187518761877OBJECT_SENTINEL = object()1878"""An object-based sentinel value."""187918801881class EnumSentinel(enum.Enum):1882 FALSY_SENTINEL = object()18831884 def __bool__(self) -> Literal[False]:1885 """Force the sentinel to be falsy to make sure it is not caught by Click1886 internal implementation.18871888 Falsy sentinels have been discussed in:1889 https://github.com/pallets/click/pull/3030#pullrequestreview-31066047951890 https://github.com/pallets/click/pull/3030#pullrequestreview-31084715521891 """1892 return False189318941895# Any kind of sentinel value is recognized by Click as a valid flag value.1896@pytest.mark.parametrize("sentinel", (OBJECT_SENTINEL, EnumSentinel.FALSY_SENTINEL))1897@pytest.mark.parametrize(1898 ("args", "expected"),1899 (1900 # Option default to None.1901 ([], None),1902 # Config has no default value, and no flag value, so it requires an argument.1903 (1904 ["--config"],1905 re.compile(re.escape("Error: Option '--config' requires an argument.\n")),1906 ),1907 (1908 ["--no-config", "--config"],1909 re.compile(re.escape("Error: Option '--config' requires an argument.\n")),1910 ),1911 # Passing --no-config defaults to the sentinel value because of the flag_value,1912 # and then the custom type receives that sentinel and returns a message.1913 (["--no-config"], "No configuration file provided."),1914 # Passing --config with an argument returns the file path.1915 (["--config", "foo.conf"], "foo.conf"),1916 # An argument is not allowed for --no-config, so it raises an error.1917 (1918 ["--no-config", "foo.conf"],1919 re.compile(1920 r"Usage: main \[OPTIONS\]\n"1921 r"Try 'main --help' for help.\n"1922 r"\n"1923 r"Error: Got unexpected extra argument (.+)\n"1924 ),1925 ),1926 # Passing --config with an argument that does not exist raises an error.1927 (1928 ["--config", "random-file.conf"],1929 re.compile(1930 r"Usage: main \[OPTIONS\]\n"1931 r"Try 'main --help' for help.\n"1932 r"\n"1933 r"Error: Invalid value for '-c' / '--config': "1934 r"File 'random-file.conf' does not exist.\n"1935 ),1936 ),1937 (1938 ["--config", "--no-config"],1939 re.compile(1940 r"Usage: main \[OPTIONS\]\n"1941 r"Try 'main --help' for help.\n"1942 r"\n"1943 r"Error: Invalid value for '-c' / '--config': "1944 r"File '--no-config' does not exist.\n"1945 ),1946 ),1947 (1948 ["--config", "--no-config", "foo.conf"],1949 re.compile(1950 r"Usage: main \[OPTIONS\]\n"1951 r"Try 'main --help' for help.\n"1952 r"\n"1953 r"Error: Invalid value for '-c' / '--config': "1954 r"File '--no-config' does not exist.\n"1955 ),1956 ),1957 # --config is passed last and overrides the --no-config option.1958 (["--no-config", "--config", "foo.conf"], "foo.conf"),1959 ),1960)1961def test_dual_options_custom_type_sentinel_flag_value(runner, sentinel, args, expected):1962 """Check that an object-based sentinel, used as a flag value, is returned as-is1963 to a custom type that is shared by two options, competing for the same variable1964 name.19651966 A reproduction of1967 https://github.com/pallets/click/issues/3024#issuecomment-31465113561968 """19691970 class ConfigParamType(click.ParamType):1971 """A custom type that accepts a file path or a sentinel value."""19721973 def convert(self, value, param, ctx):1974 if value is sentinel:1975 return "No configuration file provided."1976 else:1977 return click.Path(exists=True, dir_okay=False).convert(1978 value, param, ctx1979 )19801981 @click.command()1982 @click.option("-c", "--config", type=ConfigParamType())1983 @click.option("--no-config", "config", flag_value=sentinel, type=ConfigParamType())1984 def main(config):1985 click.echo(repr(config), nl=False)19861987 with tempfile.NamedTemporaryFile(mode="w") as named_tempfile:1988 if "foo.conf" in args:1989 named_tempfile.write("Blah blah")1990 named_tempfile.flush()1991 args = [named_tempfile.name if a == "foo.conf" else a for a in args]19921993 result = runner.invoke(main, args)19941995 if isinstance(expected, re.Pattern):1996 assert re.match(expected, result.output)1997 else:1998 assert result.output == repr(1999 named_tempfile.name if expected == "foo.conf" else expected2000 )The first 2,000 of 2,321 lines.