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

Show empty default #2501

Open
wants to merge 8 commits into
base: 8.1.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGES.rst
Expand Up @@ -10,6 +10,7 @@ Unreleased
- Improve type hinting for decorators and give all generic types parameters.
:issue:`2398`
- Fix return value and type signature of `shell_completion.add_completion_class` function. :pr:`2421`
- Display empty string defaults for options. :issue:`2500`


Version 8.1.3
Expand Down
5 changes: 3 additions & 2 deletions src/click/core.py
Expand Up @@ -2781,6 +2781,7 @@ def _write_opts(opts: t.Sequence[str]) -> str:
show_default = ctx.show_default

if show_default_is_str or (show_default and (default_value is not None)):
default_string = None
if show_default_is_str:
default_string = f"({self.show_default})"
elif isinstance(default_value, (list, tuple)):
Expand All @@ -2794,11 +2795,11 @@ def _write_opts(opts: t.Sequence[str]) -> str:
(self.opts if self.default else self.secondary_opts)[0]
)[1]
elif self.is_bool_flag and not self.secondary_opts and not default_value:
default_string = ""
default_string = None
else:
default_string = str(default_value)

if default_string:
if default_string is not None:
extra.append(_("default: {default}").format(default=default_string))

if (
Expand Down
14 changes: 14 additions & 0 deletions tests/test_options.py
Expand Up @@ -745,6 +745,20 @@ def test_show_default_boolean_flag_name(runner, default, expect):
assert f"[default: {expect}]" in message


@pytest.mark.parametrize(("default", "expect"), [("", ""), ("value", "value")])
def test_show_default_empty_string(runner, default, expect):
"""When an empty string is set as a default value it will show the default value."""
opt = click.Option(
("--string-option",),
type=str,
default=default,
show_default=True,
)
ctx = click.Context(click.Command("test"))
message = opt.get_help_record(ctx)[1]
assert f"[default: {expect}]" in message


def test_show_true_default_boolean_flag_value(runner):
"""When a boolean flag only has one opt and its default is True,
it will show the default value, not the opt name.
Expand Down