mirror of https://github.com/python/cpython
gh-101979: argparse: fix a bug where parentheses in metavar argument of add_argument() were dropped (#102318)
This commit is contained in:
parent
66aa78cbe6
commit
9a478be1a4
|
@ -403,10 +403,18 @@ class HelpFormatter(object):
|
||||||
except ValueError:
|
except ValueError:
|
||||||
continue
|
continue
|
||||||
else:
|
else:
|
||||||
end = start + len(group._group_actions)
|
group_action_count = len(group._group_actions)
|
||||||
|
end = start + group_action_count
|
||||||
if actions[start:end] == group._group_actions:
|
if actions[start:end] == group._group_actions:
|
||||||
|
|
||||||
|
suppressed_actions_count = 0
|
||||||
for action in group._group_actions:
|
for action in group._group_actions:
|
||||||
group_actions.add(action)
|
group_actions.add(action)
|
||||||
|
if action.help is SUPPRESS:
|
||||||
|
suppressed_actions_count += 1
|
||||||
|
|
||||||
|
exposed_actions_count = group_action_count - suppressed_actions_count
|
||||||
|
|
||||||
if not group.required:
|
if not group.required:
|
||||||
if start in inserts:
|
if start in inserts:
|
||||||
inserts[start] += ' ['
|
inserts[start] += ' ['
|
||||||
|
@ -416,7 +424,7 @@ class HelpFormatter(object):
|
||||||
inserts[end] += ']'
|
inserts[end] += ']'
|
||||||
else:
|
else:
|
||||||
inserts[end] = ']'
|
inserts[end] = ']'
|
||||||
else:
|
elif exposed_actions_count > 1:
|
||||||
if start in inserts:
|
if start in inserts:
|
||||||
inserts[start] += ' ('
|
inserts[start] += ' ('
|
||||||
else:
|
else:
|
||||||
|
@ -490,7 +498,6 @@ class HelpFormatter(object):
|
||||||
text = _re.sub(r'(%s) ' % open, r'\1', text)
|
text = _re.sub(r'(%s) ' % open, r'\1', text)
|
||||||
text = _re.sub(r' (%s)' % close, r'\1', text)
|
text = _re.sub(r' (%s)' % close, r'\1', text)
|
||||||
text = _re.sub(r'%s *%s' % (open, close), r'', text)
|
text = _re.sub(r'%s *%s' % (open, close), r'', text)
|
||||||
text = _re.sub(r'\(([^|]*)\)', r'\1', text)
|
|
||||||
text = text.strip()
|
text = text.strip()
|
||||||
|
|
||||||
# return the text
|
# return the text
|
||||||
|
|
|
@ -3764,6 +3764,28 @@ class TestHelpUsage(HelpTestCase):
|
||||||
version = ''
|
version = ''
|
||||||
|
|
||||||
|
|
||||||
|
class TestHelpUsageWithParentheses(HelpTestCase):
|
||||||
|
parser_signature = Sig(prog='PROG')
|
||||||
|
argument_signatures = [
|
||||||
|
Sig('positional', metavar='(example) positional'),
|
||||||
|
Sig('-p', '--optional', metavar='{1 (option A), 2 (option B)}'),
|
||||||
|
]
|
||||||
|
|
||||||
|
usage = '''\
|
||||||
|
usage: PROG [-h] [-p {1 (option A), 2 (option B)}] (example) positional
|
||||||
|
'''
|
||||||
|
help = usage + '''\
|
||||||
|
|
||||||
|
positional arguments:
|
||||||
|
(example) positional
|
||||||
|
|
||||||
|
options:
|
||||||
|
-h, --help show this help message and exit
|
||||||
|
-p {1 (option A), 2 (option B)}, --optional {1 (option A), 2 (option B)}
|
||||||
|
'''
|
||||||
|
version = ''
|
||||||
|
|
||||||
|
|
||||||
class TestHelpOnlyUserGroups(HelpTestCase):
|
class TestHelpOnlyUserGroups(HelpTestCase):
|
||||||
"""Test basic usage messages"""
|
"""Test basic usage messages"""
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,2 @@
|
||||||
|
Fix a bug where parentheses in the ``metavar`` argument to :meth:`argparse.ArgumentParser.add_argument` were
|
||||||
|
dropped. Patch by Yeojin Kim.
|
Loading…
Reference in New Issue