Skip to content

Don't break hyphenated options across lines in the usage line#3437

Closed
c-tonneslan wants to merge 1 commit into
pallets:mainfrom
c-tonneslan:fix/usage-no-break-on-hyphens
Closed

Don't break hyphenated options across lines in the usage line#3437
c-tonneslan wants to merge 1 commit into
pallets:mainfrom
c-tonneslan:fix/usage-no-break-on-hyphens

Conversation

@c-tonneslan
Copy link
Copy Markdown

Closes #3362.

textwrap.TextWrapper defaults to break_on_hyphens=True, so when HelpFormatter.write_usage wraps a long args string at a narrow width, option names get split mid-word:

Usage: program --enable-verbose-logging --output-file-path --max-
               retry-count --disable-cache-mode --config-file-
               location ...

Adds a break_on_hyphens parameter to wrap_text that forwards to TextWrapper. The default stays True so existing callers (write_text, write_dl, paragraph wrapping) keep their current behavior. write_usage passes False so each option name stays on a single line:

Usage: program --enable-verbose-logging --output-file-path
               --max-retry-count --disable-cache-mode
               --config-file-location ...

Verified against the reproducer from the issue (output matches the expected block exactly).

Added two regression tests in test_formatting.py:

  • test_write_usage_does_not_break_options_at_hyphen checks the issue's specific scenario via HelpFormatter.write_usage
  • test_wrap_text_break_on_hyphens_opt_in exercises the new parameter directly and confirms the default still splits at hyphens

textwrap.TextWrapper defaults to break_on_hyphens=True, so when
HelpFormatter.write_usage wraps an args string like
"--enable-verbose-logging --output-file-path ..." at a narrow width,
options end up split mid-word as "--max-" / "retry-count" on adjacent
lines. That's confusing in help output.

Add a break_on_hyphens parameter to wrap_text that forwards to the
underlying TextWrapper, defaulting to True so existing behavior is
preserved everywhere else. write_usage passes False to keep option
names atomic. Closes pallets#3362.

Signed-off-by: Charlie Tonneslan <cst0520@gmail.com>
@davidism davidism closed this May 16, 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.

HelpFormatter.write_usage breaks options at a hyphen

2 participants