Don't break usage option tokens at internal hyphens#3388
Closed
charlieleith wants to merge 1 commit intopallets:mainfrom
Closed
Don't break usage option tokens at internal hyphens#3388charlieleith wants to merge 1 commit intopallets:mainfrom
charlieleith wants to merge 1 commit intopallets:mainfrom
Conversation
``HelpFormatter.write_usage`` runs its argument string through
:func:`wrap_text`, which delegates to :class:`textwrap.TextWrapper`.
``TextWrapper.break_on_hyphens`` defaults to ``True``, which is the
right call for prose ("very-much-needed") but the wrong call for
usage args, where ``--enable-verbose-logging`` is a single token.
Today a borderline-length usage line ends up shaped like
Usage: program --enable-verbose-logging --output-file-path --max-
retry-count --disable-cache-mode --config-file-
...
which fragments tokens at arbitrary internal hyphens.
Plumb ``break_on_hyphens`` through ``wrap_text`` and pass
``break_on_hyphens=False`` from both ``write_usage`` paths
(prefix-fits and prefix-overflows). Hyphenated tokens stay intact
and the wrapper still breaks on whitespace between tokens, so they
move to the next line as a unit when they don't fit.
Closes pallets#3362
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
Summary
HelpFormatter.write_usageruns its argument string throughwrap_text, which builds atextwrap.TextWrapper.TextWrapper.break_on_hyphensdefaults toTrue— the right call for prose, the wrong call here, where each usage token is a long flag like--enable-verbose-loggingand an internal hyphen is part of the token, not a natural break point.Repro from the issue:
```python
import click
options = [
"--enable-verbose-logging",
"--output-file-path",
"--max-retry-count",
"--disable-cache-mode",
"--config-file-location",
"--user-auth-token",
"--auto-update-interval",
"--force-overwrite-existing",
"--network-timeout-seconds",
"--debug-trace-enabled",
]
f = click.HelpFormatter(width=65)
f.write_usage("program", " ".join(options))
print(f.getvalue())
```
Today, this prints fragmented tokens:
```
Usage: program --enable-verbose-logging --output-file-path --max-
retry-count --disable-cache-mode --config-file-
location --user-auth-token --auto-update-interval
--force-overwrite-existing --network-timeout-
seconds --debug-trace-enabled
```
After the fix, hyphenated tokens stay whole and only break on whitespace:
```
Usage: program --enable-verbose-logging --output-file-path
--max-retry-count --disable-cache-mode
--config-file-location --user-auth-token
--auto-update-interval --force-overwrite-existing
--network-timeout-seconds --debug-trace-enabled
```
Change
wrap_textgains abreak_on_hyphens: bool = Truekeyword so callers can opt out without going throughTextWrapperdirectly. Default keeps existing behaviour.write_usagepaths (prefix-fits and prefix-overflows) passbreak_on_hyphens=False.write_text,write_dl,write_epilog) change — prose is still allowed to break at hyphens.Mirrors the precedent of
:class:textwrap.TextWrapperexposing the same flag.Tests
tests/test_formatting.py::test_usage_does_not_break_options_at_internal_hyphenis new and asserts:Without the fix the test fails on assertion (1) with the very first wrapped line. Full suite:
1437 passed, 23 skipped, 1 xfailed.Checklist
Note
There was an earlier PR (#3385) that took the same general approach and was closed without merge — by the author, not by a maintainer, with no review comments — so reopening with my own write-up.