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
11 changes: 11 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
.. currentmodule:: click

Version 8.3.4
-------------

Unreleased

- Fix :meth:`HelpFormatter.write_usage` to write the usage prefix when called
with no ``args``. Previously, an empty string was passed through
:func:`wrap_text`, which dropped the ``initial_indent`` and produced a
blank line. :issue:`3360`


Version 8.3.3
-------------

Expand Down
7 changes: 7 additions & 0 deletions src/click/formatting.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,13 @@ 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 not args:
# Without args, ``wrap_text`` would drop the ``initial_indent``
# because the text is empty, so write the prefix directly.
self.write(usage_prefix.rstrip())
self.write("\n")
return

if text_width >= (term_len(usage_prefix) + 20):
# The arguments will fit to the right of the prefix.
indent = " " * term_len(usage_prefix)
Expand Down
19 changes: 19 additions & 0 deletions tests/test_formatting.py
Original file line number Diff line number Diff line change
Expand Up @@ -366,3 +366,22 @@ 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():
formatter = click.HelpFormatter()
formatter.write_usage("program")
assert formatter.getvalue() == "Usage: program\n"


def test_help_formatter_write_usage_no_args_custom_prefix():
formatter = click.HelpFormatter()
formatter.write_usage("program", prefix="Try: ")
assert formatter.getvalue() == "Try: program\n"


def test_help_formatter_write_usage_no_args_indented():
formatter = click.HelpFormatter()
formatter.current_indent = 10
formatter.write_usage("program")
assert formatter.getvalue() == " Usage: program\n"