Skip to content

Commit cb2163a

Browse files
Do not transform text if color=False
1 parent 9422e89 commit cb2163a

File tree

3 files changed

+7
-4
lines changed

3 files changed

+7
-4
lines changed

Doc/library/argparse.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -657,7 +657,7 @@ To highlight command examples in your description or epilog text, you can use
657657

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

662662
.. note::
663663

Lib/argparse.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -526,8 +526,12 @@ def _apply_text_markup(self, text):
526526
527527
Supported markup:
528528
[cmd]...[/cmd] - command/shell example (single color)
529+
530+
When colors are disabled, no transformation is applied.
529531
"""
530532
t = self._theme
533+
if not t.reset:
534+
return text
531535
text = _re.sub(
532536
r'\[cmd\](.*?)\[/cmd\]',
533537
rf'{t.prog_extra}\1{t.reset}',

Lib/test/test_argparse.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7618,16 +7618,15 @@ def test_cmd_markup_multiple_tags(self):
76187618
self.assertIn(f'{prog_extra}app test{reset}', help_text)
76197619

76207620
def test_cmd_markup_not_applied_when_color_disabled(self):
7621+
# When color is disabled, markup is not transformed (tags remain as-is)
76217622
parser = argparse.ArgumentParser(
76227623
prog='PROG',
76237624
color=False,
76247625
epilog='Example: [cmd]python -m myapp[/cmd]',
76257626
)
76267627

76277628
help_text = parser.format_help()
7628-
self.assertNotIn('[cmd]', help_text)
7629-
self.assertNotIn('[/cmd]', help_text)
7630-
self.assertIn('python -m myapp', help_text)
7629+
self.assertIn('[cmd]python -m myapp[/cmd]', help_text)
76317630
self.assertNotIn('\x1b[', help_text)
76327631

76337632
def test_cmd_markup_unclosed_tag_unchanged(self):

0 commit comments

Comments
 (0)