Skip to content

Fix HelpFormatter.write_usage with no args#3382

Closed
ibondarenko1 wants to merge 1 commit intopallets:mainfrom
ibondarenko1:fix-3360-write-usage-no-args
Closed

Fix HelpFormatter.write_usage with no args#3382
ibondarenko1 wants to merge 1 commit intopallets:mainfrom
ibondarenko1:fix-3360-write-usage-no-args

Conversation

@ibondarenko1
Copy link
Copy Markdown

Closes #3360.

When HelpFormatter.write_usage is called without an args value, the resulting output is a single blank line instead of the expected Usage: <prog> line. This happens because wrap_text("", initial_indent=usage_prefix, ...) ends up calling textwrap.TextWrapper.fill(""), which returns "" and discards initial_indent.

This short-circuits the empty-args path and writes the usage prefix directly. The trailing space added between prog and args is rstrip-ped before the newline so the output is exactly Usage: <prog> .

Reproducer (from #3360)

import click
f = click.HelpFormatter()
f.write_usage("program")
print(f.getvalue())
  • Before: empty line
  • After: Usage: program

Tests

Added three tests in tests/test_formatting.py:

  • test_help_formatter_write_usage_no_args — default prefix.
  • test_help_formatter_write_usage_no_args_custom_prefix — custom prefix preserved.
  • test_help_formatter_write_usage_no_args_indented — indent applied via the prefix:>{indent} format spec is preserved (rstrip only strips trailing whitespace).

pytest -x -q passes (1389 passed, 73 skipped, 1 xfailed).

When write_usage was called without args, wrap_text was passed an empty
string, which causes textwrap.TextWrapper.fill to drop the initial_indent
and produce a single blank line.

Short-circuit when args is empty, writing the usage prefix directly so
the output is 'Usage: prog' instead of an empty line.

Closes pallets#3360
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Empty output from HelpFormatter.write_usage for a program without arguments

3 participants