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 printing of default option value to handle empty strings #2724

Merged
merged 1 commit into from
May 21, 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 @@ -8,6 +8,8 @@ Unreleased
- Fix an issue with type hints for ``click.open_file()``. :issue:`2717`
- Fix issue where error message for invalid ``click.Path`` displays on
multiple lines. :issue:`2697`
- Fixed issue that prevented a default value of ``""`` from being displayed in
the help for an option. :issue:`2500`


Version 8.1.7
Expand Down
2 changes: 2 additions & 0 deletions src/click/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -2804,6 +2804,8 @@ def _write_opts(opts: t.Sequence[str]) -> str:
)[1]
elif self.is_bool_flag and not self.secondary_opts and not default_value:
default_string = ""
elif default_value == "":
default_string = '""'
else:
default_string = str(default_value)

Expand Down
8 changes: 8 additions & 0 deletions tests/test_options.py
Original file line number Diff line number Diff line change
Expand Up @@ -786,6 +786,14 @@ def test_show_default_string(runner):
assert "[default: (unlimited)]" in message


def test_show_default_with_empty_string(runner):
"""When show_default is True and default is set to an empty string."""
opt = click.Option(["--limit"], default="", show_default=True)
ctx = click.Context(click.Command("cli"))
message = opt.get_help_record(ctx)[1]
assert '[default: ""]' in message


def test_do_not_show_no_default(runner):
"""When show_default is True and no default is set do not show None."""
opt = click.Option(["--limit"], show_default=True)
Expand Down