Skip to content

Commit

Permalink
Fix printing of default option value to handle empty strings
Browse files Browse the repository at this point in the history
  • Loading branch information
stefmolin authored and davidism committed May 21, 2024
1 parent c021f05 commit 61f8101
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 0 deletions.
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

0 comments on commit 61f8101

Please sign in to comment.