Skip to content

Commit 31ea447

Browse files
miss-islingtonberkerpeksag
authored andcommitted
bpo-29553: Fix ArgumentParser.format_usage() for mutually exclusive groups (GH-14976)
Co-authored-by: Andrew Nester <andrew.nester.dev@gmail.com> Co-authored-by: Flavian Hautbois <flavianh@sicara.com> (cherry picked from commit da27d9b)
1 parent f2b468d commit 31ea447

File tree

3 files changed

+50
-2
lines changed

3 files changed

+50
-2
lines changed

Lib/argparse.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -407,13 +407,19 @@ def _format_actions_usage(self, actions, groups):
407407
inserts[start] += ' ['
408408
else:
409409
inserts[start] = '['
410-
inserts[end] = ']'
410+
if end in inserts:
411+
inserts[end] += ']'
412+
else:
413+
inserts[end] = ']'
411414
else:
412415
if start in inserts:
413416
inserts[start] += ' ('
414417
else:
415418
inserts[start] = '('
416-
inserts[end] = ')'
419+
if end in inserts:
420+
inserts[end] += ')'
421+
else:
422+
inserts[end] = ')'
417423
for i in range(start + 1, end):
418424
inserts[i] = '|'
419425

Lib/test/test_argparse.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2772,6 +2772,46 @@ def get_parser(self, required):
27722772
-c c help
27732773
'''
27742774

2775+
class TestMutuallyExclusiveNested(MEMixin, TestCase):
2776+
2777+
def get_parser(self, required):
2778+
parser = ErrorRaisingArgumentParser(prog='PROG')
2779+
group = parser.add_mutually_exclusive_group(required=required)
2780+
group.add_argument('-a')
2781+
group.add_argument('-b')
2782+
group2 = group.add_mutually_exclusive_group(required=required)
2783+
group2.add_argument('-c')
2784+
group2.add_argument('-d')
2785+
group3 = group2.add_mutually_exclusive_group(required=required)
2786+
group3.add_argument('-e')
2787+
group3.add_argument('-f')
2788+
return parser
2789+
2790+
usage_when_not_required = '''\
2791+
usage: PROG [-h] [-a A | -b B | [-c C | -d D | [-e E | -f F]]]
2792+
'''
2793+
usage_when_required = '''\
2794+
usage: PROG [-h] (-a A | -b B | (-c C | -d D | (-e E | -f F)))
2795+
'''
2796+
2797+
help = '''\
2798+
2799+
optional arguments:
2800+
-h, --help show this help message and exit
2801+
-a A
2802+
-b B
2803+
-c C
2804+
-d D
2805+
-e E
2806+
-f F
2807+
'''
2808+
2809+
# We are only interested in testing the behavior of format_usage().
2810+
test_failures_when_not_required = None
2811+
test_failures_when_required = None
2812+
test_successes_when_not_required = None
2813+
test_successes_when_required = None
2814+
27752815
# =================================================
27762816
# Mutually exclusive group in parent parser tests
27772817
# =================================================
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fixed :meth:`argparse.ArgumentParser.format_usage` for mutually exclusive groups.
2+
Patch by Andrew Nester.

0 commit comments

Comments
 (0)