mirror of
https://github.com/python/cpython.git
synced 2025-12-09 10:37:17 +00:00
gh-113008: Correct argparse usage output for required, mutually exclusive groups (GH-113085)
This commit is contained in:
parent
4a5e4aade4
commit
d21b0b5d36
3 changed files with 26 additions and 2 deletions
|
|
@ -395,12 +395,12 @@ class HelpFormatter(object):
|
||||||
continue
|
continue
|
||||||
|
|
||||||
try:
|
try:
|
||||||
start = actions.index(group._group_actions[0])
|
start = min(actions.index(item) for item in group._group_actions)
|
||||||
except ValueError:
|
except ValueError:
|
||||||
continue
|
continue
|
||||||
else:
|
else:
|
||||||
end = start + len(group._group_actions)
|
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)
|
group_actions.update(group._group_actions)
|
||||||
inserts[start, end] = group
|
inserts[start, end] = group
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -2902,6 +2902,29 @@ class TestMutuallyExclusiveGroupErrors(TestCase):
|
||||||
'''
|
'''
|
||||||
self.assertEqual(parser.format_help(), textwrap.dedent(expected))
|
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):
|
def test_help_subparser_all_mutually_exclusive_group_members_suppressed(self):
|
||||||
self.maxDiff = None
|
self.maxDiff = None
|
||||||
parser = ErrorRaisingArgumentParser(prog='PROG')
|
parser = ErrorRaisingArgumentParser(prog='PROG')
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1 @@
|
||||||
|
Correct argparse usage output for required, mutually exclusive groups containing a positional argument
|
||||||
Loading…
Add table
Add a link
Reference in a new issue