Skip to content

Fixed write_usage producing empty output when args is empty.#3413

Closed
tejasae-afk wants to merge 1 commit into
pallets:mainfrom
tejasae-afk:fix/write-usage-empty-args
Closed

Fixed write_usage producing empty output when args is empty.#3413
tejasae-afk wants to merge 1 commit into
pallets:mainfrom
tejasae-afk:fix/write-usage-empty-args

Conversation

@tejasae-afk
Copy link
Copy Markdown

Fixes #3360.

When HelpFormatter.write_usage is called with no args (or an empty string), the usage line was silently dropped. The call to wrap_text("", ...) returned an empty string, so only the trailing \n was written to the buffer.

Root cause: wrap_text with an empty text string returns "", writing nothing. The usage_prefix (e.g. "Usage: program ") was only passed as the initial_indent to wrap_text, not written directly.

Fix: Short-circuit when args is empty — write usage_prefix (stripped of its trailing space) directly and skip wrap_text entirely.

Before:

import click
f = click.HelpFormatter()
f.write_usage("program")
print(repr(f.getvalue()))
# '\n'  ← empty line, usage line missing

After:

print(repr(f.getvalue()))
# 'Usage: program\n'  ← correct

Test added in tests/test_formatting.py. Full suite: 1494 passed, 24 skipped.

@davidism davidism closed this May 9, 2026
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

2 participants