# prog defaults to the usage message of this parser, skipping
# optional arguments and with no "usage:" prefix
if kwargs.get('prog') is None:
- formatter = self._get_formatter()
+ # Create formatter without color to avoid storing ANSI codes in prog
+ formatter = self.formatter_class(prog=self.prog)
+ formatter._set_color(False)
positionals = self._get_positional_actions()
groups = self._mutually_exclusive_groups
formatter.add_usage(None, positionals, groups, '')
captured_stderr,
force_not_colorized,
force_not_colorized_test_class,
+ swap_attr,
)
from test.support import import_helper
from test.support import os_helper
def setUp(self):
super().setUp()
# Ensure color even if ran with NO_COLOR=1
- _colorize.can_colorize = lambda *args, **kwargs: True
+ self.enterContext(swap_attr(_colorize, 'can_colorize',
+ lambda *args, **kwargs: True))
self.theme = _colorize.get_theme(force_color=True).argparse
def test_argparse_color(self):
{short_b}+f{reset}, {long_b}++foo{reset} {label_b}FOO{reset} foo help
'''))
+ def test_subparser_prog_is_stored_without_color(self):
+ parser = argparse.ArgumentParser(prog='complex', color=True)
+ sub = parser.add_subparsers(dest='command')
+ demo_parser = sub.add_parser('demo')
+
+ self.assertNotIn('\x1b[', demo_parser.prog)
+
+ demo_parser.color = False
+ help_text = demo_parser.format_help()
+ self.assertNotIn('\x1b[', help_text)
+
class TestModule(unittest.TestCase):
def test_deprecated__version__(self):