Skip to content

Commit

Permalink
Merge pull request #2004 from ikapelyukhin/fix-multiple-default-values
Browse files Browse the repository at this point in the history
Fix default values of multiple options with optional values
  • Loading branch information
davidism committed Oct 8, 2021
2 parents 0905156 + aa6f360 commit 7a226b3
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGES.rst
Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions src/click/core.py
Expand Up @@ -2853,6 +2853,14 @@ def consume_value(
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 (
Expand Down
23 changes: 23 additions & 0 deletions tests/test_options.py
Expand Up @@ -792,6 +792,29 @@ def cli(opt, a, b):
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
Expand Down

0 comments on commit 7a226b3

Please sign in to comment.