https://github.com/python/cpython/commit/d21b0b5d36834d4d35aec3a01661597019594936
commit: d21b0b5d36834d4d35aec3a01661597019594936
branch: main
author: Payton <[email protected]>
committer: serhiy-storchaka <[email protected]>
date: 2024-09-24T17:14:35+03:00
summary:
gh-113008: Correct argparse usage output for required, mutually exclusive
groups (GH-113085)
files:
A Misc/NEWS.d/next/Library/2023-12-14-13-43-27.gh-issue-113008.jWYn8T.rst
M Lib/argparse.py
M Lib/test/test_argparse.py
diff --git a/Lib/argparse.py b/Lib/argparse.py
index 66192fb92832a2..694c46db61d177 100644
--- a/Lib/argparse.py
+++ b/Lib/argparse.py
@@ -395,12 +395,12 @@ def _get_actions_usage_parts(self, actions, groups):
continue
try:
- start = actions.index(group._group_actions[0])
+ start = min(actions.index(item) for item in
group._group_actions)
except ValueError:
continue
else:
end = start + len(group._group_actions)
- if actions[start:end] == group._group_actions:
+ if set(actions[start:end]) == set(group._group_actions):
group_actions.update(group._group_actions)
inserts[start, end] = group
diff --git a/Lib/test/test_argparse.py b/Lib/test/test_argparse.py
index c6b76986940977..ef05a6fefcffcc 100644
--- a/Lib/test/test_argparse.py
+++ b/Lib/test/test_argparse.py
@@ -2902,6 +2902,29 @@ def test_help(self):
'''
self.assertEqual(parser.format_help(), textwrap.dedent(expected))
+ def test_optional_order(self):
+ parser = ErrorRaisingArgumentParser(prog='PROG')
+ group = parser.add_mutually_exclusive_group(required=True)
+ group.add_argument('--foo')
+ group.add_argument('bar', nargs='?')
+ expected = '''\
+ usage: PROG [-h] (--foo FOO | bar)
+
+ positional arguments:
+ bar
+
+ options:
+ -h, --help show this help message and exit
+ --foo FOO
+ '''
+ self.assertEqual(parser.format_help(), textwrap.dedent(expected))
+
+ parser = ErrorRaisingArgumentParser(prog='PROG')
+ group = parser.add_mutually_exclusive_group(required=True)
+ group.add_argument('bar', nargs='?')
+ group.add_argument('--foo')
+ self.assertEqual(parser.format_help(), textwrap.dedent(expected))
+
def
test_help_subparser_all_mutually_exclusive_group_members_suppressed(self):
self.maxDiff = None
parser = ErrorRaisingArgumentParser(prog='PROG')
diff --git
a/Misc/NEWS.d/next/Library/2023-12-14-13-43-27.gh-issue-113008.jWYn8T.rst
b/Misc/NEWS.d/next/Library/2023-12-14-13-43-27.gh-issue-113008.jWYn8T.rst
new file mode 100644
index 00000000000000..0f2a44299717c0
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2023-12-14-13-43-27.gh-issue-113008.jWYn8T.rst
@@ -0,0 +1 @@
+Correct argparse usage output for required, mutually exclusive groups
containing a positional argument
_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/python-checkins.python.org/
Member address: [email protected]