Skip to content

Commit 902acd1

Browse files
authored
Don't let help formatter line-wrap URLs (#19825)
Fixes #19816 This PR adjusts the `AugmentedHelpFormatter` for the command-line `--help` output so that it will no longer wrap/break long URLs in the output. It also (in a separate commit) adds `https://` in front of two URLs that lacked it, so they can be recognized as links by anything parsing the help text.
1 parent 08a0b07 commit 902acd1

File tree

1 file changed

+24
-10
lines changed

1 file changed

+24
-10
lines changed

mypy/main.py

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -256,10 +256,19 @@ def _fill_text(self, text: str, width: int, indent: str) -> str:
256256
if "\n" in text:
257257
# Assume we want to manually format the text
258258
return super()._fill_text(text, width, indent)
259-
else:
260-
# Assume we want argparse to manage wrapping, indenting, and
261-
# formatting the text for us.
262-
return argparse.HelpFormatter._fill_text(self, text, width, indent)
259+
# Format the text like argparse, but overflow rather than
260+
# breaking long words (like URLs)
261+
text = self._whitespace_matcher.sub(" ", text).strip()
262+
import textwrap
263+
264+
return textwrap.fill(
265+
text,
266+
width,
267+
initial_indent=indent,
268+
subsequent_indent=indent,
269+
break_on_hyphens=False,
270+
break_long_words=False,
271+
)
263272

264273

265274
# Define pairs of flag prefixes with inverse meaning.
@@ -544,10 +553,15 @@ def add_invertible_flag(
544553
# Feel free to add subsequent sentences that add additional details.
545554
# 3. If you cannot think of a meaningful description for a new group, omit it entirely.
546555
# (E.g. see the "miscellaneous" sections).
547-
# 4. The group description should end with a period (unless the last line is a link). If you
548-
# do end the group description with a link, omit the 'http://' prefix. (Some links are too
549-
# long and will break up into multiple lines if we include that prefix, so for consistency
550-
# we omit the prefix on all links.)
556+
# 4. The text of the group description should end with a period, optionally followed
557+
# by a documentation reference (URL).
558+
# 5. If you want to include a documentation reference, place it at the end of the
559+
# description. Feel free to open with a brief reference ("See also:", "For more
560+
# information:", etc.), followed by a space, then the entire URL including
561+
# "https://" scheme identifier and fragment ("#some-target-heading"), if any.
562+
# Do not end with a period (or any other characters not part of the URL).
563+
# URLs longer than the available terminal width will overflow without being
564+
# broken apart. This facilitates both URL detection, and manual copy-pasting.
551565

552566
general_group = parser.add_argument_group(title="Optional arguments")
553567
general_group.add_argument(
@@ -1034,7 +1048,7 @@ def add_invertible_flag(
10341048
"Mypy caches type information about modules into a cache to "
10351049
"let you speed up future invocations of mypy. Also see "
10361050
"mypy's daemon mode: "
1037-
"mypy.readthedocs.io/en/stable/mypy_daemon.html#mypy-daemon",
1051+
"https://mypy.readthedocs.io/en/stable/mypy_daemon.html#mypy-daemon",
10381052
)
10391053
incremental_group.add_argument(
10401054
"-i", "--incremental", action="store_true", help=argparse.SUPPRESS
@@ -1278,7 +1292,7 @@ def add_invertible_flag(
12781292
code_group = parser.add_argument_group(
12791293
title="Running code",
12801294
description="Specify the code you want to type check. For more details, see "
1281-
"mypy.readthedocs.io/en/stable/running_mypy.html#running-mypy",
1295+
"https://mypy.readthedocs.io/en/stable/running_mypy.html#running-mypy",
12821296
)
12831297
add_invertible_flag(
12841298
"--explicit-package-bases",

0 commit comments

Comments
 (0)