Skip to content

Commit c51f27c

Browse files
committed
Add support for backtick colorisation in argparse help text
1 parent 10f950c commit c51f27c

5 files changed

Lines changed: 42 additions & 11 deletions

File tree

Doc/library/argparse.rst

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -637,25 +637,21 @@ are set.
637637

638638
.. versionadded:: 3.14
639639

640-
To highlight inline code in your description or epilog text, you can use
641-
backticks::
640+
To highlight inline code in your description, epilog, or argument ``help``
641+
text, you can use backticks::
642642

643643
>>> parser = argparse.ArgumentParser(
644644
... formatter_class=argparse.RawDescriptionHelpFormatter,
645645
... epilog='''Examples:
646646
... `python -m myapp --verbose`
647647
... `python -m myapp --config settings.json`
648648
... ''')
649+
>>> parser.add_argument('--foo', help='set the `foo` value')
649650

650651
When colors are enabled, the text inside backticks will be displayed in a
651652
distinct color to help examples stand out. When colors are disabled, backticks
652653
are preserved as-is, which is readable in plain text.
653654

654-
.. note::
655-
656-
Backtick markup only applies to description and epilog text. It does not
657-
apply to individual argument ``help`` strings.
658-
659655
.. versionadded:: 3.15
660656

661657

Doc/whatsnew/3.15.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -715,9 +715,9 @@ argparse
715715
default to ``True``. This enables suggestions for mistyped arguments by default.
716716
(Contributed by Jakob Schluse in :gh:`140450`.)
717717

718-
* Added backtick markup support in description and epilog text to highlight
718+
* Added backtick markup support in help text to highlight
719719
inline code when color output is enabled.
720-
(Contributed by Savannah Ostrowski in :gh:`142390`.)
720+
(Contributed by Savannah Ostrowski and Hugo van Kemenade in :gh:`142389`.)
721721

722722

723723
array

Lib/argparse.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -682,7 +682,7 @@ def _format_args(self, action, default_metavar):
682682
def _expand_help(self, action):
683683
help_string = self._get_help_string(action)
684684
if '%' not in help_string:
685-
return help_string
685+
return self._apply_text_markup(help_string)
686686
params = dict(vars(action), prog=self._prog)
687687
for name in list(params):
688688
value = params[name]
@@ -726,7 +726,9 @@ def colorize(match):
726726
# bare %s etc. - format with full params dict, no colorization
727727
return spec % params
728728

729-
return _re.sub(fmt_spec, colorize, help_string, flags=_re.VERBOSE)
729+
return self._apply_text_markup(
730+
_re.sub(fmt_spec, colorize, help_string, flags=_re.VERBOSE)
731+
)
730732

731733
def _iter_indented_subactions(self, action):
732734
try:

Lib/test/test_argparse.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7696,6 +7696,37 @@ def test_backtick_markup_special_regex_chars(self):
76967696
help_text = parser.format_help()
76977697
self.assertIn(f'{prog_extra}grep "foo.*bar" | sort{reset}', help_text)
76987698

7699+
def test_backtick_markup_in_argument_help(self):
7700+
parser = argparse.ArgumentParser(prog="PROG", color=True)
7701+
parser.add_argument("--foo", help="set the `foo` value")
7702+
7703+
prog_extra = self.theme.prog_extra
7704+
reset = self.theme.reset
7705+
7706+
help_text = parser.format_help()
7707+
self.assertIn(f"set the {prog_extra}foo{reset} value", help_text)
7708+
self.assertNotIn("`", help_text)
7709+
7710+
def test_backtick_markup_in_argument_help_with_format(self):
7711+
parser = argparse.ArgumentParser(prog="PROG", color=True)
7712+
parser.add_argument(
7713+
"--foo", default="bar", help="set `foo` (default: %(default)s)"
7714+
)
7715+
7716+
prog_extra = self.theme.prog_extra
7717+
reset = self.theme.reset
7718+
7719+
help_text = parser.format_help()
7720+
self.assertIn(f"set {prog_extra}foo{reset}", help_text)
7721+
7722+
def test_backtick_markup_in_argument_help_color_disabled(self):
7723+
parser = argparse.ArgumentParser(prog="PROG", color=False)
7724+
parser.add_argument("--foo", help="set the `foo` value")
7725+
7726+
help_text = parser.format_help()
7727+
self.assertIn("set the `foo` value", help_text)
7728+
self.assertNotIn("\x1b[", help_text)
7729+
76997730
def test_help_with_format_specifiers(self):
77007731
# GH-142950: format specifiers like %x should work with color=True
77017732
parser = argparse.ArgumentParser(prog='PROG', color=True)
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Add backtick markup support in :mod:`argparse` option help text to highlight
2+
inline code when color output is enabled. Patch by Hugo van Kemenade.

0 commit comments

Comments
 (0)