diff --git a/src/click/formatting.py b/src/click/formatting.py index de2ca4711..c05d8b50c 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 c74b53a3d..52fb40018 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"