From d84a563cb9becb5047360d05b49f09090b5e728d Mon Sep 17 00:00:00 2001 From: Tejas Date: Fri, 8 May 2026 22:59:25 -0400 Subject: [PATCH] Fixed write_usage producing empty output when args is empty. --- src/click/formatting.py | 4 +++- tests/test_formatting.py | 7 +++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/src/click/formatting.py b/src/click/formatting.py index de2ca47117..c05d8b50c3 100644 --- a/src/click/formatting.py +++ b/src/click/formatting.py @@ -158,7 +158,9 @@ def write_usage(self, prog: str, args: str = "", prefix: str | None = None) -> N usage_prefix = f"{prefix:>{self.current_indent}}{prog} " text_width = self.width - self.current_indent - if text_width >= (term_len(usage_prefix) + 20): + if not args: + self.write(usage_prefix.rstrip()) + elif text_width >= (term_len(usage_prefix) + 20): # The arguments will fit to the right of the prefix. indent = " " * term_len(usage_prefix) self.write( diff --git a/tests/test_formatting.py b/tests/test_formatting.py index c74b53a3df..52fb400183 100644 --- a/tests/test_formatting.py +++ b/tests/test_formatting.py @@ -433,3 +433,10 @@ def test_help_formatter_write_text(): actual = formatter.getvalue() expected = " Lorem ipsum dolor sit amet,\n consectetur adipiscing elit\n" assert actual == expected + + +def test_help_formatter_write_usage_no_args(): + """write_usage with no args should still print the usage line.""" + formatter = click.HelpFormatter() + formatter.write_usage("program") + assert formatter.getvalue() == "Usage: program\n"