Skip to content

Commit e62cd6a

Browse files
Flavian Hautboisandrewnester
Flavian Hautbois
andcommitted
Fixed ArgumentParses format_usage for mutually exclusive groups.
Co-authored-by: Andrew Nester <andrew.nester.dev@gmail.com>
1 parent 46ebd4a commit e62cd6a

File tree

3 files changed

+46
-2
lines changed

3 files changed

+46
-2
lines changed

Lib/argparse.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -404,13 +404,19 @@ def _format_actions_usage(self, actions, groups):
404404
inserts[start] += ' ['
405405
else:
406406
inserts[start] = '['
407-
inserts[end] = ']'
407+
if end in inserts:
408+
inserts[end] += ']'
409+
else:
410+
inserts[end] = ']'
408411
else:
409412
if start in inserts:
410413
inserts[start] += ' ('
411414
else:
412415
inserts[start] = '('
413-
inserts[end] = ')'
416+
if end in inserts:
417+
inserts[end] += ')'
418+
else:
419+
inserts[end] = ')'
414420
for i in range(start + 1, end):
415421
inserts[i] = '|'
416422

Lib/test/test_argparse.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2813,6 +2813,43 @@ def get_parser(self, required):
28132813
-c c help
28142814
'''
28152815

2816+
class TestMutuallyExclusiveNested(MEMixin, TestCase):
2817+
def get_parser(self, required):
2818+
parser = ErrorRaisingArgumentParser(prog='PROG')
2819+
group = parser.add_mutually_exclusive_group(required=required)
2820+
group.add_argument('-a')
2821+
group.add_argument('-b')
2822+
group2 = group.add_mutually_exclusive_group(required=required)
2823+
group2.add_argument('-c')
2824+
group2.add_argument('-d')
2825+
group3 = group2.add_mutually_exclusive_group(required=required)
2826+
group3.add_argument('-e')
2827+
group3.add_argument('-f')
2828+
return parser
2829+
2830+
failures = []
2831+
successes = []
2832+
successes_when_not_required = []
2833+
2834+
usage_when_not_required = '''\
2835+
usage: PROG [-h] [-a A | -b B | [-c C | -d D | [-e E | -f F]]]
2836+
'''
2837+
usage_when_required = '''\
2838+
usage: PROG [-h] (-a A | -b B | (-c C | -d D | (-e E | -f F)))
2839+
'''
2840+
2841+
help = '''\
2842+
2843+
optional arguments:
2844+
-h, --help show this help message and exit
2845+
-a A
2846+
-b B
2847+
-c C
2848+
-d D
2849+
-e E
2850+
-f F
2851+
'''
2852+
28162853
# =================================================
28172854
# Mutually exclusive group in parent parser tests
28182855
# =================================================
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fixed ArgumentParses format_usage for mutually exclusive groups. Patch by Andrew Nester (@andrewnester)

0 commit comments

Comments
 (0)