From 61f8101f4e0d02cb902ef17853fa35ad88185265 Mon Sep 17 00:00:00 2001 From: Stefanie Molin <24376333+stefmolin@users.noreply.github.com> Date: Mon, 20 May 2024 11:57:28 -0400 Subject: [PATCH] Fix printing of default option value to handle empty strings --- CHANGES.rst | 2 ++ src/click/core.py | 2 ++ tests/test_options.py | 8 ++++++++ 3 files changed, 12 insertions(+) diff --git a/CHANGES.rst b/CHANGES.rst index aba1bbe07..f298cee77 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -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 diff --git a/src/click/core.py b/src/click/core.py index f205c79c6..367beb266 100644 --- a/src/click/core.py +++ b/src/click/core.py @@ -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) diff --git a/tests/test_options.py b/tests/test_options.py index 91249b234..7397f3667 100644 --- a/tests/test_options.py +++ b/tests/test_options.py @@ -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)