diff --git a/src/click/formatting.py b/src/click/formatting.py index 0b64f831b..64a54c336 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 c79f6577f..d3d7e4f0f 100644 --- a/tests/test_formatting.py +++ b/tests/test_formatting.py @@ -366,3 +366,24 @@ 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_write_usage_no_args(): + """write_usage should output the usage line even when args is empty.""" + formatter = click.HelpFormatter() + formatter.write_usage("program") + assert formatter.getvalue() == "Usage: program\n" + + +def test_write_usage_with_args(): + """write_usage should still work normally with arguments.""" + formatter = click.HelpFormatter() + formatter.write_usage("program", "FILE [OPTIONS]") + assert formatter.getvalue() == "Usage: program FILE [OPTIONS]\n" + + +def test_write_usage_no_args_custom_prefix(): + """write_usage with a custom prefix and no args.""" + formatter = click.HelpFormatter() + formatter.write_usage("program", prefix="Run: ") + assert formatter.getvalue() == "Run: program\n"