Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion Lib/argparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -623,7 +623,11 @@ def _expand_help(self, action):
if params.get('choices') is not None:
choices_str = ', '.join([str(c) for c in params['choices']])
params['choices'] = choices_str
return self._get_help_string(action) % params
expanded_help = self._get_help_string(action)
for param, value in params.items():
if f'%({param})s' in expanded_help:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this conditional is redundant, right? Doesn't replace() handle validating that the string is contained by expanded_help?

expanded_help = expanded_help.replace(f'%({param})s', str(value))
return expanded_help

def _iter_indented_subactions(self, action):
try:
Expand Down
2 changes: 1 addition & 1 deletion Lib/test/test_argparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -3901,7 +3901,7 @@ class TestHelpVariableExpansion(HelpTestCase):
parser_signature = Sig(prog='PROG')
argument_signatures = [
Sig('-x', type=int,
help='x %(prog)s %(default)s %(type)s %%'),
help='x %(prog)s %(default)s %(type)s %'),
Sig('-y', action='store_const', default=42, const='XXX',
help='y %(prog)s %(default)s %(const)s'),
Sig('--foo', choices='abc',
Expand Down