Skip to content

Commit 3419312

Browse files
Use backticks instead
1 parent 40040ca commit 3419312

File tree

4 files changed

+31
-85
lines changed

4 files changed

+31
-85
lines changed

Doc/library/argparse.rst

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -645,24 +645,24 @@ are set.
645645

646646
.. versionadded:: 3.14
647647

648-
To highlight command examples in your description or epilog text, you can use
649-
``[cmd]...[/cmd]`` markup::
648+
To highlight inline code in your description or epilog text, you can use
649+
backticks::
650650

651651
>>> parser = argparse.ArgumentParser(
652652
... formatter_class=argparse.RawDescriptionHelpFormatter,
653653
... epilog='''Examples:
654-
... [cmd]python -m myapp --verbose[/cmd]
655-
... [cmd]python -m myapp --config settings.json[/cmd]
654+
... `python -m myapp --verbose`
655+
... `python -m myapp --config settings.json`
656656
... ''')
657657

658-
When colors are enabled, the text inside ``[cmd]...[/cmd]`` tags will be
659-
displayed in a distinct color to help examples stand out. When colors are
660-
disabled, no transformation is applied and the tags remain as-is.
658+
When colors are enabled, the text inside backticks will be displayed in a
659+
distinct color to help examples stand out. When colors are disabled, backticks
660+
are preserved as-is, which is readable in plain text.
661661

662662
.. note::
663663

664-
The ``[cmd]`` markup only applies to description and epilog text. It does
665-
not apply to individual argument ``help`` strings.
664+
Backtick markup only applies to description and epilog text. It does not
665+
apply to individual argument ``help`` strings.
666666

667667
.. versionadded:: 3.15
668668

Doc/whatsnew/3.15.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -423,8 +423,8 @@ argparse
423423
default to ``True``. This enables suggestions for mistyped arguments by default.
424424
(Contributed by Jakob Schluse in :gh:`140450`.)
425425

426-
* Added ``[cmd]...[/cmd]`` markup support in description and epilog text to
427-
highlight command examples when color output is enabled.
426+
* Added backtick markup support in description and epilog text to highlight
427+
inline code when color output is enabled.
428428
(Contributed by Savannah Ostrowski in :gh:`142390`.)
429429

430430
calendar

Lib/argparse.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -525,18 +525,17 @@ def _apply_text_markup(self, text):
525525
"""Apply color markup to text.
526526
527527
Supported markup:
528-
[cmd]...[/cmd] - command/shell example (single color)
528+
`...` - inline code (rendered with prog_extra color)
529529
530-
When colors are disabled, no transformation is applied.
530+
When colors are disabled, backticks are preserved as-is.
531531
"""
532532
t = self._theme
533533
if not t.reset:
534534
return text
535535
text = _re.sub(
536-
r'\[cmd\](.*?)\[/cmd\]',
536+
r'`([^`]+)`',
537537
rf'{t.prog_extra}\1{t.reset}',
538538
text,
539-
flags=_re.DOTALL
540539
)
541540
return text
542541

Lib/test/test_argparse.py

Lines changed: 17 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -7560,11 +7560,11 @@ def test_error_and_warning_not_colorized_when_disabled(self):
75607560
self.assertNotIn('\x1b[', warn)
75617561
self.assertIn('warning:', warn)
75627562

7563-
def test_cmd_markup_in_epilog(self):
7563+
def test_backtick_markup_in_epilog(self):
75647564
parser = argparse.ArgumentParser(
75657565
prog='PROG',
75667566
color=True,
7567-
epilog='Example: [cmd]python -m myapp --verbose[/cmd]',
7567+
epilog='Example: `python -m myapp --verbose`',
75687568
)
75697569

75707570
prog_extra = self.theme.prog_extra
@@ -7573,14 +7573,13 @@ def test_cmd_markup_in_epilog(self):
75737573
help_text = parser.format_help()
75747574
self.assertIn(f'Example: {prog_extra}python -m myapp --verbose{reset}',
75757575
help_text)
7576-
self.assertNotIn('[cmd]', help_text)
7577-
self.assertNotIn('[/cmd]', help_text)
7576+
self.assertNotIn('`', help_text)
75787577

7579-
def test_cmd_markup_in_description(self):
7578+
def test_backtick_markup_in_description(self):
75807579
parser = argparse.ArgumentParser(
75817580
prog='PROG',
75827581
color=True,
7583-
description='Run [cmd]python -m myapp[/cmd] to start.',
7582+
description='Run `python -m myapp` to start.',
75847583
)
75857584

75867585
prog_extra = self.theme.prog_extra
@@ -7590,26 +7589,11 @@ def test_cmd_markup_in_description(self):
75907589
self.assertIn(f'Run {prog_extra}python -m myapp{reset} to start.',
75917590
help_text)
75927591

7593-
def test_cmd_markup_multiline(self):
7592+
def test_backtick_markup_multiple(self):
75947593
parser = argparse.ArgumentParser(
75957594
prog='PROG',
75967595
color=True,
7597-
formatter_class=argparse.RawDescriptionHelpFormatter,
7598-
epilog='Example:\n[cmd]python -m myapp \\\n --verbose[/cmd]',
7599-
)
7600-
7601-
prog_extra = self.theme.prog_extra
7602-
reset = self.theme.reset
7603-
7604-
help_text = parser.format_help()
7605-
self.assertIn(f'{prog_extra}python -m myapp \\\n --verbose{reset}',
7606-
help_text)
7607-
7608-
def test_cmd_markup_multiple_tags(self):
7609-
parser = argparse.ArgumentParser(
7610-
prog='PROG',
7611-
color=True,
7612-
epilog='Try [cmd]app run[/cmd] or [cmd]app test[/cmd].',
7596+
epilog='Try `app run` or `app test`.',
76137597
)
76147598

76157599
prog_extra = self.theme.prog_extra
@@ -7619,46 +7603,23 @@ def test_cmd_markup_multiple_tags(self):
76197603
self.assertIn(f'{prog_extra}app run{reset}', help_text)
76207604
self.assertIn(f'{prog_extra}app test{reset}', help_text)
76217605

7622-
def test_cmd_markup_not_applied_when_color_disabled(self):
7623-
# When color is disabled, markup is not transformed (tags remain as-is)
7606+
def test_backtick_markup_not_applied_when_color_disabled(self):
7607+
# When color is disabled, backticks are preserved as-is
76247608
parser = argparse.ArgumentParser(
76257609
prog='PROG',
76267610
color=False,
7627-
epilog='Example: [cmd]python -m myapp[/cmd]',
7611+
epilog='Example: `python -m myapp`',
76287612
)
76297613

76307614
help_text = parser.format_help()
7631-
self.assertIn('[cmd]python -m myapp[/cmd]', help_text)
7615+
self.assertIn('`python -m myapp`', help_text)
76327616
self.assertNotIn('\x1b[', help_text)
76337617

7634-
def test_cmd_markup_unclosed_tag_unchanged(self):
7635-
parser = argparse.ArgumentParser(
7636-
prog='PROG',
7637-
color=True,
7638-
epilog='Example: [cmd]python -m myapp without closing tag',
7639-
)
7640-
7641-
help_text = parser.format_help()
7642-
self.assertIn('[cmd]', help_text)
7643-
7644-
def test_cmd_markup_empty_tag(self):
7645-
parser = argparse.ArgumentParser(
7646-
prog='PROG',
7647-
color=True,
7648-
epilog='Before [cmd][/cmd] after',
7649-
)
7650-
7651-
prog_extra = self.theme.prog_extra
7652-
reset = self.theme.reset
7653-
7654-
help_text = parser.format_help()
7655-
self.assertIn(f'Before {prog_extra}{reset} after', help_text)
7656-
7657-
def test_cmd_markup_with_format_string(self):
7618+
def test_backtick_markup_with_format_string(self):
76587619
parser = argparse.ArgumentParser(
76597620
prog='myapp',
76607621
color=True,
7661-
epilog='Run [cmd]%(prog)s --help[/cmd] for more info.',
7622+
epilog='Run `%(prog)s --help` for more info.',
76627623
)
76637624

76647625
prog_extra = self.theme.prog_extra
@@ -7667,26 +7628,12 @@ def test_cmd_markup_with_format_string(self):
76677628
help_text = parser.format_help()
76687629
self.assertIn(f'{prog_extra}myapp --help{reset}', help_text)
76697630

7670-
def test_cmd_markup_case_sensitive(self):
7671-
parser = argparse.ArgumentParser(
7672-
prog='PROG',
7673-
color=True,
7674-
epilog='[CMD]uppercase[/CMD] vs [cmd]lowercase[/cmd]',
7675-
)
7676-
7677-
prog_extra = self.theme.prog_extra
7678-
reset = self.theme.reset
7679-
7680-
help_text = parser.format_help()
7681-
self.assertIn('[CMD]uppercase[/CMD]', help_text)
7682-
self.assertIn(f'{prog_extra}lowercase{reset}', help_text)
7683-
7684-
def test_cmd_markup_in_subparser(self):
7631+
def test_backtick_markup_in_subparser(self):
76857632
parser = argparse.ArgumentParser(prog='PROG', color=True)
76867633
subparsers = parser.add_subparsers()
76877634
sub = subparsers.add_parser(
76887635
'sub',
7689-
description='Run [cmd]PROG sub --foo[/cmd] to start.',
7636+
description='Run `PROG sub --foo` to start.',
76907637
)
76917638

76927639
prog_extra = self.theme.prog_extra
@@ -7695,11 +7642,11 @@ def test_cmd_markup_in_subparser(self):
76957642
help_text = sub.format_help()
76967643
self.assertIn(f'{prog_extra}PROG sub --foo{reset}', help_text)
76977644

7698-
def test_cmd_markup_special_regex_chars(self):
7645+
def test_backtick_markup_special_regex_chars(self):
76997646
parser = argparse.ArgumentParser(
77007647
prog='PROG',
77017648
color=True,
7702-
epilog='[cmd]grep "foo.*bar" | sort[/cmd]',
7649+
epilog='`grep "foo.*bar" | sort`',
77037650
)
77047651

77057652
prog_extra = self.theme.prog_extra

0 commit comments

Comments
 (0)