Skip to content

formatting: don't break wrapped text at hyphens#3544

Closed
sapirbaruch wants to merge 1 commit into
pallets:mainfrom
sapirbaruch:fix-break-on-hyphens
Closed

formatting: don't break wrapped text at hyphens#3544
sapirbaruch wants to merge 1 commit into
pallets:mainfrom
sapirbaruch:fix-break-on-hyphens

Conversation

@sapirbaruch
Copy link
Copy Markdown

Fixes #3362.

textwrap.TextWrapper defaults to break_on_hyphens=True, which breaks compound words at hyphens when they fall at the wrap boundary. That default is designed for English prose, but a CLI help formatter is not prose — option names like --enable-verbose-logging, paths, and other hyphenated identifiers should never be split at a hyphen.

Before this change, write_usage could produce:

Usage: program --enable-verbose-logging --output-file-path --max-
               retry-count ...

After:

Usage: program --enable-verbose-logging --output-file-path
               --max-retry-count ...

The fix is a single-line change: pass break_on_hyphens=False to the TextWrapper constructor inside wrap_text. All existing tests continue to pass (none depended on hyphen-breaking behaviour), and a new test captures the regression.

CLI option names and other hyphenated tokens (e.g. --enable-verbose-logging)
are split at the hyphen when they fall exactly at the text-wrap boundary.
textwrap.TextWrapper.break_on_hyphens defaults to True, which is designed
for prose but is wrong for a CLI help formatter where option names, paths,
and compound identifiers must stay intact.

Pass break_on_hyphens=False to the TextWrapper so that wrap_text, and by
extension HelpFormatter.write_usage / write_text, only ever break at
whitespace.

Fixes pallets#3362
@davidism davidism closed this Jun 1, 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