Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ Version 8.4.0

Unreleased

- :func:`wrap_text` accepts a ``break_on_hyphens`` parameter.
:meth:`HelpFormatter.write_usage` passes ``False`` so option
names like ``--max-retry-count`` are not split across lines.
:issue:`3362`
- :class:`ParamType` typing improvements. :pr:`3371`

- :class:`ParamType` is now a generic abstract base class,
Expand Down
11 changes: 10 additions & 1 deletion src/click/formatting.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ def wrap_text(
initial_indent: str = "",
subsequent_indent: str = "",
preserve_paragraphs: bool = False,
break_on_hyphens: bool = True,
) -> str:
"""A helper function that intelligently wraps text. By default, it
assumes that it operates on a single paragraph of text but if the
Expand All @@ -52,6 +53,8 @@ def wrap_text(
each consecutive line.
:param preserve_paragraphs: if this flag is set then the wrapping will
intelligently handle paragraphs.
:param break_on_hyphens: if ``False``, hyphens in words will not be
used as line break points.
"""
from ._textwrap import TextWrapper

Expand All @@ -61,6 +64,7 @@ def wrap_text(
initial_indent=initial_indent,
subsequent_indent=subsequent_indent,
replace_whitespace=False,
break_on_hyphens=break_on_hyphens,
)
if not preserve_paragraphs:
return wrapper.fill(text)
Expand Down Expand Up @@ -167,6 +171,7 @@ def write_usage(self, prog: str, args: str = "", prefix: str | None = None) -> N
text_width,
initial_indent=usage_prefix,
subsequent_indent=indent,
break_on_hyphens=False,
)
)
else:
Expand All @@ -176,7 +181,11 @@ def write_usage(self, prog: str, args: str = "", prefix: str | None = None) -> N
indent = " " * (max(self.current_indent, term_len(prefix)) + 4)
self.write(
wrap_text(
args, text_width, initial_indent=indent, subsequent_indent=indent
args,
text_width,
initial_indent=indent,
subsequent_indent=indent,
break_on_hyphens=False,
)
)

Expand Down
13 changes: 13 additions & 0 deletions tests/test_formatting.py
Original file line number Diff line number Diff line change
Expand Up @@ -433,3 +433,16 @@ def test_help_formatter_write_text():
actual = formatter.getvalue()
expected = " Lorem ipsum dolor sit amet,\n consectetur adipiscing elit\n"
assert actual == expected


def test_write_usage_no_break_on_hyphens():
f = click.HelpFormatter(width=65)
f.write_usage(
"program",
"--enable-verbose-logging --output-file-path"
" --max-retry-count --disable-cache-mode",
)
assert f.getvalue() == (
"Usage: program --enable-verbose-logging --output-file-path\n"
" --max-retry-count --disable-cache-mode\n"
)
Loading