Skip to content

Commit bc16dbf

Browse files
authored
fix bug in show defaults when using a flag option and a default map (#2730)
2 parents 5b1624b + bf1e8be commit bc16dbf

File tree

3 files changed

+28
-1
lines changed

3 files changed

+28
-1
lines changed

CHANGES.rst

+2
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ Unreleased
1212
the help for an option. :issue:`2500`
1313
- The test runner handles stripping color consistently on Windows.
1414
:issue:`2705`
15+
- Show correct value for flag default when using ``default_map``.
16+
:issue:`2632`
1517

1618

1719
Version 8.1.7

src/click/core.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -2800,7 +2800,7 @@ def _write_opts(opts: t.Sequence[str]) -> str:
28002800
# For boolean flags that have distinct True/False opts,
28012801
# use the opt without prefix instead of the value.
28022802
default_string = split_opt(
2803-
(self.opts if self.default else self.secondary_opts)[0]
2803+
(self.opts if default_value else self.secondary_opts)[0]
28042804
)[1]
28052805
elif self.is_bool_flag and not self.secondary_opts and not default_value:
28062806
default_string = ""

tests/test_defaults.py

+25
Original file line numberDiff line numberDiff line change
@@ -59,3 +59,28 @@ def cli(y, f, v):
5959

6060
result = runner.invoke(cli, ["-y", "-n", "-f", "-v", "-q"], standalone_mode=False)
6161
assert result.return_value == ((True, False), (True,), (1, -1))
62+
63+
64+
def test_flag_default_map(runner):
65+
"""test flag with default map"""
66+
67+
@click.group()
68+
def cli():
69+
pass
70+
71+
@cli.command()
72+
@click.option("--name/--no-name", is_flag=True, show_default=True, help="name flag")
73+
def foo(name):
74+
click.echo(name)
75+
76+
result = runner.invoke(cli, ["foo"])
77+
assert "False" in result.output
78+
79+
result = runner.invoke(cli, ["foo", "--help"])
80+
assert "default: no-name" in result.output
81+
82+
result = runner.invoke(cli, ["foo"], default_map={"foo": {"name": True}})
83+
assert "True" in result.output
84+
85+
result = runner.invoke(cli, ["foo", "--help"], default_map={"foo": {"name": True}})
86+
assert "default: name" in result.output

0 commit comments

Comments
 (0)