Skip to content
Myr

allow multiple=True with flags

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by David Lord · committed by David Lord · parent d34d31f · Browse files · 4 files+29 added−8 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Trailers

  • co-authored-by: Amethyst Reese <amethyst@n7.gg>

Diff stats

4 files+29 added−8 removed

  • tests:+21 added−4 removed
  • other:+8 added−4 removed

4 files+29 added−8 removed

modified:src/click/core.py+6 added-4 removed
2568 unmodified lines
2569 # Not a flag, and prompt is not enabled, can be used as a2570 # flag if flag_value is set.2571 self._flag_needs_value = flag_value is not None25722573 self.default: t.Union[t.Any, t.Callable[[], t.Any]]25742575 if is_flag and default_is_missing and not self.required:2574 self.default: t.Union[t.Any, t.Callable[[], t.Any]] = False2576 if multiple:2577 self.default = ()2578 else:2579 self.default = False25802581 if flag_value is None:2582 flag_value = not self.default2583
42 unmodified lines
26262627 if self.is_flag:2628 raise TypeError("'count' is not valid with 'is_flag'.")26292625 if self.multiple and self.is_flag:2626 raise TypeError("'multiple' is not valid with 'is_flag', use 'count'.")26272630 def to_info_dict(self) -> t.Dict[str, t.Any]:2631 info_dict = super().to_info_dict()2632 info_dict.update(2633 help=self.help,
409 unmodified lines
modified:tests/test_defaults.py+21 added
36 unmodified lines
3738 result = runner.invoke(cli, [])39 assert not result.exception40 assert result.output.splitlines() == ["<1|2>", "<3|4>"]414243def test_multiple_flag_default(runner):44 """Default default for flags when multiple=True should be empty tuple."""4546 @click.command47 # flag due to secondary token48 @click.option("-y/-n", multiple=True)49 # flag due to is_flag50 @click.option("-f", is_flag=True, multiple=True)51 # flag due to flag_value52 @click.option("-v", "v", flag_value=1, multiple=True)53 @click.option("-q", "v", flag_value=-1, multiple=True)54 def cli(y, f, v):55 return y, f, v5657 result = runner.invoke(cli, standalone_mode=False)58 assert result.return_value == ((), (), ())5960 result = runner.invoke(cli, ["-y", "-n", "-f", "-v", "-q"], standalone_mode=False)61 assert result.return_value == ((True, False), (True,), (1, -1))
modified:tests/test_options.py-4 removed
909 unmodified lines
910 ("kwargs", "message"),911 [912 ({"count": True, "multiple": True}, "'count' is not valid with 'multiple'."),913 ({"count": True, "is_flag": True}, "'count' is not valid with 'is_flag'."),914 (915 {"multiple": True, "is_flag": True},916 "'multiple' is not valid with 'is_flag', use 'count'.",917 ),914 ],915)916def test_invalid_flag_combinations(runner, kwargs, message):917 with pytest.raises(TypeError) as e:
3 unmodified lines
modified:CHANGES.rst+2 added
20 unmodified lines
21- ``EOFError`` and ``KeyboardInterrupt`` tracebacks are not suppressed when22 ``standalone_mode`` is disabled. :issue:`2380`23- ``@group.command`` does not fail if the group was created with a custom24 ``command_class``. :issue:`2416`25- ``multiple=True`` is allowed for flag options again and does not require26 setting ``default=()``. :issue:`2246, 2292, 2295`272829Version 8.1.330-------------
1040 unmodified lines