Skip to content

Commit e599afa

Browse files
Address additional cases
1 parent 62ceb27 commit e599afa

File tree

2 files changed

+42
-14
lines changed

2 files changed

+42
-14
lines changed

Lib/argparse.py

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -691,26 +691,38 @@ def _expand_help(self, action):
691691

692692
t = self._theme
693693

694+
result = help_string % params
695+
694696
if not t.reset:
695-
return help_string % params
697+
return result
698+
699+
# Match format specifiers like: %s, %d, %(key)s, etc.
700+
fmt_spec = r'''
701+
%
702+
(?:
703+
% # %% escape
704+
|
705+
(?:\((?P<key>[^)]*)\))? # key
706+
[-#0\ +]* # flags
707+
(?:\*|\d+)? # width
708+
(?:\.(?:\*|\d+))? # precision
709+
[hlL]? # length modifier
710+
[diouxXeEfFgGcrsa] # conversion type
711+
)
712+
'''
696713

697-
# Format first to preserve types for specifiers, like %x that require int.
698714
def colorize(match):
699-
spec, name = match.group(0, 1)
715+
spec, key = match.group(0, 'key')
700716
if spec == '%%':
701717
return '%'
702-
if name in params:
703-
formatted = spec % {name: params[name]}
718+
if key is not None:
719+
# %(key)... - format and colorize
720+
formatted = spec % {key: params[key]}
704721
return f'{t.interpolated_value}{formatted}{t.reset}'
705-
return spec
722+
# bare %s etc. - format with full params dict, no colorization
723+
return spec % params
706724

707-
# Match %% or %(name)... format specifiers
708-
result = _re.sub(r'%%|%\((\w+)\)[^a-z]*[a-z]', colorize,
709-
help_string, flags=_re.IGNORECASE)
710-
711-
if '%' in result:
712-
raise ValueError(f"invalid format specifier in: {help_string!r}")
713-
return result
725+
return _re.sub(fmt_spec, colorize, help_string, flags=_re.VERBOSE)
714726

715727
def _iter_indented_subactions(self, action):
716728
try:

Lib/test/test_argparse.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7667,17 +7667,33 @@ def test_help_with_format_specifiers(self):
76677667
# GH-142950: format specifiers like %x should work with color=True
76687668
parser = argparse.ArgumentParser(prog='PROG', color=True)
76697669
parser.add_argument('--hex', type=int, default=255,
7670-
help='hex: %(default)x')
7670+
help='hex: %(default)x, alt: %(default)#x')
7671+
parser.add_argument('--zero', type=int, default=7,
7672+
help='zero: %(default)05d')
76717673
parser.add_argument('--str', default='test',
76727674
help='str: %(default)s')
7675+
parser.add_argument('--pct', type=int, default=50,
7676+
help='pct: %(default)d%%')
7677+
parser.add_argument('--literal', help='literal: 100%%')
7678+
parser.add_argument('--prog', help='prog: %(prog)s')
7679+
parser.add_argument('--type', type=int, help='type: %(type)s')
7680+
parser.add_argument('--choices', choices=['a', 'b'],
7681+
help='choices: %(choices)s')
76737682

76747683
help_text = parser.format_help()
76757684

76767685
interp = self.theme.interpolated_value
76777686
reset = self.theme.reset
76787687

76797688
self.assertIn(f'hex: {interp}ff{reset}', help_text)
7689+
self.assertIn(f'alt: {interp}0xff{reset}', help_text)
7690+
self.assertIn(f'zero: {interp}00007{reset}', help_text)
76807691
self.assertIn(f'str: {interp}test{reset}', help_text)
7692+
self.assertIn(f'pct: {interp}50{reset}%', help_text)
7693+
self.assertIn('literal: 100%', help_text)
7694+
self.assertIn(f'prog: {interp}PROG{reset}', help_text)
7695+
self.assertIn(f'type: {interp}int{reset}', help_text)
7696+
self.assertIn(f'choices: {interp}a, b{reset}', help_text)
76817697

76827698
def test_print_help_uses_target_file_for_color_decision(self):
76837699
parser = argparse.ArgumentParser(prog='PROG', color=True)

0 commit comments

Comments
 (0)