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 bug in show defaults when using a flag option and a default map #2730

Merged
merged 1 commit into from
May 22, 2024
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 @@ -12,6 +12,8 @@ Unreleased
the help for an option. :issue:`2500`
- The test runner handles stripping color consistently on Windows.
:issue:`2705`
- Show correct value for flag default when using ``default_map``.
:issue:`2632`


Version 8.1.7
Expand Down
2 changes: 1 addition & 1 deletion src/click/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -2800,7 +2800,7 @@ def _write_opts(opts: t.Sequence[str]) -> str:
# For boolean flags that have distinct True/False opts,
# use the opt without prefix instead of the value.
default_string = split_opt(
(self.opts if self.default else self.secondary_opts)[0]
(self.opts if default_value else self.secondary_opts)[0]
)[1]
elif self.is_bool_flag and not self.secondary_opts and not default_value:
default_string = ""
Expand Down
25 changes: 25 additions & 0 deletions tests/test_defaults.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,28 @@ def cli(y, f, v):

result = runner.invoke(cli, ["-y", "-n", "-f", "-v", "-q"], standalone_mode=False)
assert result.return_value == ((True, False), (True,), (1, -1))


def test_flag_default_map(runner):
"""test flag with default map"""

@click.group()
def cli():
pass

@cli.command()
@click.option("--name/--no-name", is_flag=True, show_default=True, help="name flag")
def foo(name):
click.echo(name)

result = runner.invoke(cli, ["foo"])
assert "False" in result.output

result = runner.invoke(cli, ["foo", "--help"])
assert "default: no-name" in result.output

result = runner.invoke(cli, ["foo"], default_map={"foo": {"name": True}})
assert "True" in result.output

result = runner.invoke(cli, ["foo", "--help"], default_map={"foo": {"name": True}})
assert "default: name" in result.output