[Python-checkins] gh-101979: argparse: fix a bug where parentheses in metavar argument of add_argument() were dropped (GH-102318)
miss-islington
webhook-mailer at python.org
Sun Mar 5 10:16:21 EST 2023
https://github.com/python/cpython/commit/2a062f275970b3775b055c2c82f31b766271ae18
commit: 2a062f275970b3775b055c2c82f31b766271ae18
branch: 3.10
author: Miss Islington (bot) <31488909+miss-islington at users.noreply.github.com>
committer: miss-islington <31488909+miss-islington at users.noreply.github.com>
date: 2023-03-05T07:16:14-08:00
summary:
gh-101979: argparse: fix a bug where parentheses in metavar argument of add_argument() were dropped (GH-102318)
(cherry picked from commit 9a478be1a4314734c697dda7a7b0e633a6fb0751)
Co-authored-by: Yeojin Kim <yeojin.dev at gmail.com>
files:
A Misc/NEWS.d/next/Library/2023-02-28-09-52-25.gh-issue-101979.or3hXV.rst
M Lib/argparse.py
M Lib/test/test_argparse.py
diff --git a/Lib/argparse.py b/Lib/argparse.py
index fb042a86b47c..736bf5ab21a5 100644
--- a/Lib/argparse.py
+++ b/Lib/argparse.py
@@ -400,10 +400,18 @@ def _format_actions_usage(self, actions, groups):
except ValueError:
continue
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:
+
+ suppressed_actions_count = 0
for action in group._group_actions:
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 start in inserts:
inserts[start] += ' ['
@@ -413,7 +421,7 @@ def _format_actions_usage(self, actions, groups):
inserts[end] += ']'
else:
inserts[end] = ']'
- else:
+ elif exposed_actions_count > 1:
if start in inserts:
inserts[start] += ' ('
else:
@@ -487,7 +495,6 @@ def _format_actions_usage(self, actions, groups):
text = _re.sub(r'(%s) ' % open, 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'\(([^|]*)\)', r'\1', text)
text = text.strip()
# return the text
diff --git a/Lib/test/test_argparse.py b/Lib/test/test_argparse.py
index 4d43e3310d49..5df6f3263075 100644
--- a/Lib/test/test_argparse.py
+++ b/Lib/test/test_argparse.py
@@ -3729,6 +3729,28 @@ class TestHelpUsage(HelpTestCase):
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):
"""Test basic usage messages"""
diff --git a/Misc/NEWS.d/next/Library/2023-02-28-09-52-25.gh-issue-101979.or3hXV.rst b/Misc/NEWS.d/next/Library/2023-02-28-09-52-25.gh-issue-101979.or3hXV.rst
new file mode 100644
index 000000000000..1efe72439b3a
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2023-02-28-09-52-25.gh-issue-101979.or3hXV.rst
@@ -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.
More information about the Python-checkins
mailing list