Skip to content

Commit 6f9b9cb

Browse files
author
SapirBaruch
committed
formatting: don't break wrapped text at hyphens
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 #3362
1 parent c480210 commit 6f9b9cb

3 files changed

Lines changed: 27 additions & 0 deletions

File tree

CHANGES.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ Version 8.5.0
55

66
Unreleased
77

8+
- :func:`wrap_text` no longer breaks text at hyphens. CLI option names
9+
such as ``--enable-verbose-logging`` now wrap only at whitespace. :issue:`3362`
810
- Supported versions of Windows enable ANSI terminal styles by default.
911
Colorama is no longer a dependency and is not used. :issue:`2986` :pr:`3505`
1012
- :class:`Argument` accepts a ``help`` parameter, and help output includes

src/click/formatting.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ def wrap_text(
6767
initial_indent=initial_indent,
6868
subsequent_indent=subsequent_indent,
6969
replace_whitespace=False,
70+
break_on_hyphens=False,
7071
)
7172
if not preserve_paragraphs:
7273
return wrapper.fill(text)

tests/test_formatting.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -436,6 +436,30 @@ def test_help_formatter_write_text():
436436
assert actual == expected
437437

438438

439+
def test_wrap_text_does_not_break_at_hyphens():
440+
"""wrap_text must not split tokens at hyphens.
441+
442+
CLI option names such as ``--enable-verbose-logging`` should never be
443+
broken in the middle at a hyphen; wrapping must happen only at
444+
whitespace boundaries. Regression for :issue:`3362`.
445+
"""
446+
args = (
447+
"--enable-verbose-logging --output-file-path --max-retry-count"
448+
" --disable-cache-mode --config-file-location"
449+
)
450+
result = click.formatting.wrap_text(
451+
args,
452+
width=65,
453+
initial_indent="Usage: program ",
454+
subsequent_indent=" ",
455+
)
456+
for line in result.splitlines():
457+
stripped = line.rstrip()
458+
assert not stripped.endswith(
459+
"-"
460+
), f"wrap_text broke a token at a hyphen: {stripped!r}"
461+
462+
439463
@pytest.mark.parametrize(
440464
("body", "width", "initial_indent"),
441465
[

0 commit comments

Comments
 (0)