Skip to content
Closed
Show file tree
Hide file tree
Changes from 12 commits
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 @@ -600,7 +600,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.replace('%%','%')

def _iter_indented_subactions(self, action):
try:
Expand Down
33 changes: 33 additions & 0 deletions Lib/test/test_argparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -3934,6 +3934,39 @@ class TestHelpUsageWithParentheses(HelpTestCase):
'''
version = ''

class TestHelpWithPercentageSymbols(HelpTestCase):
"""Test a help message including % symbols"""
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
"""Test a help message including % symbols"""
"""Test help output that includes % symbols"""

parser_signature = Sig(prog='PROG', description='Just a test code.')
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
parser_signature = Sig(prog='PROG', description='Just a test code.')
parser_signature = Sig(prog='PROG', description='Just a test program.')

argument_signatures = [
Sig('--somearg', metavar='somearg',
help='Now you dont need to escape this: %, and you will not get nonsensical errors!'),
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
help='Now you dont need to escape this: %, and you will not get nonsensical errors!'),
help='No escape required for %'),

Sig('--date', metavar='when', help='A date in format %Y-%m-%d',
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
Sig('--date', metavar='when', help='A date in format %Y-%m-%d',
Sig('--date', metavar='when', help='A date in %Y-%m-%d format',

dest=f'date', type=str, required=True),
Sig('bar', nargs='?', type=int, default=42,
help='the bar to %(prog)s (default: %(default)s)'),
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
help='the bar to %(prog)s (default: %(default)s)'),
help='The bar to %(prog)s (default: %(default)s)'),

Sig('--weirdarg', metavar='weird', dest=f'weird', type=str,
required=True, help='A weird arg with 1 %, 2 %%% and 3 %%%%%'),
]

usage = '''\
usage: PROG [-h] [--somearg somearg] --date when --weirdarg weird [bar]
'''
help = usage + '''\

Just a test code.

positional arguments:
bar the bar to PROG (default: 42)

options:
-h, --help show this help message and exit
--somearg somearg Now you dont need to escape this: %, and you will not get
nonsensical errors!
--date when A date in format %Y-%m-%d
--weirdarg weird A weird arg with 1 %, 2 %% and 3 %%%
'''
version = ''


class TestHelpOnlyUserGroups(HelpTestCase):
"""Test basic usage messages"""
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Added feature to argparse so that percentage symbols (%) don't have to be
escaped in help messages. Without this fix, they have to be escaped (as
stated in the documentation) but if you forget this, the error message that
is produced is completely cryptic. With this patch, 1) this does not happen
anymore, 2) single % don't need to be escaped and 3) it remains fully
compatible with the previous implementation.