diff options
author | David Lord <davidism@gmail.com> | 2021-10-08 14:43:44 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-10-08 14:43:44 -0700 |
commit | 7a226b358cdf579c5cc8a7a048a426388cb8a0ad (patch) | |
tree | 6276b9a6a3ad8739fc35296ec7a5f6bad95bfbaf | |
parent | 0905156e0f9b3bb1eabf50173982add053e23ddf (diff) | |
parent | aa6f36017e5132836ea8679a4143a08944fa5c8c (diff) | |
download | click-7a226b358cdf579c5cc8a7a048a426388cb8a0ad.tar.gz |
Merge pull request #2004 from ikapelyukhin/fix-multiple-default-values
Fix default values of multiple options with optional values
-rw-r--r-- | CHANGES.rst | 2 | ||||
-rw-r--r-- | src/click/core.py | 8 | ||||
-rw-r--r-- | tests/test_options.py | 23 |
3 files changed, 33 insertions, 0 deletions
diff --git a/CHANGES.rst b/CHANGES.rst index 7c6e4a7..b82b477 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -24,6 +24,8 @@ Unreleased :issue:`2072` - Default values are not cast to the parameter type twice during processing. :issue:`2085` +- Options with ``multiple`` and ``flag_value`` use the flag value + instead of leaving an internal placeholder. :issue:`2001` Version 8.0.1 diff --git a/src/click/core.py b/src/click/core.py index 6cdba37..77a536a 100644 --- a/src/click/core.py +++ b/src/click/core.py @@ -2853,6 +2853,14 @@ class Option(Parameter): value = self.flag_value source = ParameterSource.COMMANDLINE + elif ( + self.multiple + and value is not None + and any(v is _flag_needs_value for v in value) + ): + value = [self.flag_value if v is _flag_needs_value else v for v in value] + source = ParameterSource.COMMANDLINE + # The value wasn't set, or used the param's default, prompt if # prompting is enabled. elif ( diff --git a/tests/test_options.py b/tests/test_options.py index 06a70b4..2e34337 100644 --- a/tests/test_options.py +++ b/tests/test_options.py @@ -792,6 +792,29 @@ def test_option_with_optional_value(runner, args, expect): assert result.return_value == expect +def test_multiple_option_with_optional_value(runner): + cli = click.Command( + "cli", + params=[ + click.Option(["-f"], is_flag=False, flag_value="flag", multiple=True), + click.Option(["-a"]), + click.Argument(["b"], nargs=-1), + ], + callback=lambda **kwargs: kwargs, + ) + result = runner.invoke( + cli, + ["-f", "-f", "other", "-f", "-a", "1", "a", "b"], + standalone_mode=False, + catch_exceptions=False, + ) + assert result.return_value == { + "f": ("flag", "other", "flag"), + "a": "1", + "b": ("a", "b"), + } + + def test_type_from_flag_value(): param = click.Option(["-a", "x"], default=True, flag_value=4) assert param.type is click.INT |