Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 12 additions & 7 deletions src/click/formatting.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,14 +161,19 @@ def write_usage(self, prog: str, args: str = "", prefix: str | None = None) -> N
if text_width >= (term_len(usage_prefix) + 20):
# The arguments will fit to the right of the prefix.
indent = " " * term_len(usage_prefix)
self.write(
wrap_text(
args,
text_width,
initial_indent=usage_prefix,
subsequent_indent=indent,
if args:
self.write(
wrap_text(
args,
text_width,
initial_indent=usage_prefix,
subsequent_indent=indent,
)
)
)
else:
# No args, but still need to write the "Usage: prog" prefix.
# rstrip removes the trailing space added when building usage_prefix.
self.write(usage_prefix.rstrip())
else:
# The prefix is too long, put the arguments on the next line.
self.write(usage_prefix)
Expand Down
8 changes: 8 additions & 0 deletions tests/test_formatting.py
Original file line number Diff line number Diff line change
Expand Up @@ -433,3 +433,11 @@ 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():
"""Regression test for issue #3360: write_usage with empty args
should still emit the 'Usage: prog' prefix, not an empty line."""
f = click.HelpFormatter()
f.write_usage("program")
assert f.getvalue() == "Usage: program\n"
Loading