Don't break usage-line options at a hyphen - #3810
Open
vikasvardhanv wants to merge 1 commit into
Open
Conversation
`HelpFormatter.write_usage` wrapped the arguments with the standard library default of `break_on_hyphens=True`, so a long option name or metavar such as `--max-retry-count` could be split at one of its hyphens when it landed at the wrap boundary, producing output like `--max-\n retry-count`. Add a `break_on_hyphens` parameter to `wrap_text` (defaulting to `True` to preserve existing behavior) and pass `break_on_hyphens=False` from `write_usage`, so usage tokens only ever wrap at the spaces between them. Closes pallets#3362 Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #3362
The bug
When
HelpFormatter.write_usagewraps the usage line, an option name or metavar containing hyphens can be split at one of those hyphens if it lands at the wrap boundary. For example:produces broken option names:
Root cause
write_usagewraps the arguments throughwrap_text, which builds aTextWrapper.textwrap.TextWrapperdefaults tobreak_on_hyphens=True, so it treats each hyphen inside a token as an allowed break point and can wrap mid-option. There was no way for the caller to influence this.The fix
break_on_hyphensparameter towrap_text, defaulting toTrueso existing behavior (help/prose wrapping) is unchanged.break_on_hyphens=Falsefromwrite_usage, so usage tokens only ever wrap at the spaces between them.The wide-character/ANSI-aware wrapping and every other code path are untouched. Output now matches the expected result from the issue:
Testing
test_write_usage_does_not_break_options_at_hyphenintests/test_formatting.py. It fails onmain(options split at hyphens) and passes with this change.1991 passed, 25 skipped, 1 xfailed.ruff check,ruff format --check, andmypy src/click/formatting.pyare all clean.CHANGES.mdentry and.. versionchanged::note in thewrap_textdocstring.