Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix default values of multiple options with optional values #2004

Merged
merged 1 commit into from
Oct 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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