Skip to content

fix: write_usage outputs usage line when args is empty#3386

Closed
blackwell-systems wants to merge 1 commit intopallets:mainfrom
blackwell-systems:fix/write-usage-no-args
Closed

fix: write_usage outputs usage line when args is empty#3386
blackwell-systems wants to merge 1 commit intopallets:mainfrom
blackwell-systems:fix/write-usage-no-args

Conversation

@blackwell-systems
Copy link
Copy Markdown

Motivation

HelpFormatter.write_usage("program") with no args outputs an empty line instead of Usage: program. This affects CLI tools with commands that take no arguments (e.g. exit, help).

The bug

write_usage passes args="" to wrap_text(), which returns "" for empty input, dropping the initial_indent (which contains the usage prefix). The entire usage line vanishes.

Fix

Handle the empty-args case before calling wrap_text: write usage_prefix.rstrip() directly. The .rstrip() removes the trailing space that is only needed to separate the prefix from arguments. All other behavior is unchanged.

Tests

Added three tests in tests/test_formatting.py:

  • test_write_usage_no_args: the bug case
  • test_write_usage_with_args: regression check
  • test_write_usage_no_args_custom_prefix: edge case with custom prefix

All 20 formatting tests pass.

Fixes #3360

When write_usage is called with no args (the default), wrap_text
receives an empty string and returns an empty string, dropping the
initial_indent that contains the usage prefix. The usage line
vanishes entirely.

Handle the empty-args case before calling wrap_text: write the
usage prefix directly (stripped of trailing space) followed by a
newline.

Fixes #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

2 participants