diff --git a/Lib/argparse.py b/Lib/argparse.py index b69c5adfa072b9..1391b7cb12e763 100644 --- a/Lib/argparse.py +++ b/Lib/argparse.py @@ -401,13 +401,19 @@ def _format_actions_usage(self, actions, groups): inserts[start] += ' [' else: inserts[start] = '[' - inserts[end] = ']' + if end in inserts: + inserts[end] += ']' + else: + inserts[end] = ']' else: if start in inserts: inserts[start] += ' (' else: inserts[start] = '(' - inserts[end] = ')' + if end in inserts: + inserts[end] += ')' + else: + inserts[end] = ')' for i in range(start + 1, end): inserts[i] = '|' diff --git a/Lib/test/test_argparse.py b/Lib/test/test_argparse.py index a5c4a8ec97d7ad..515ff42e13046d 100644 --- a/Lib/test/test_argparse.py +++ b/Lib/test/test_argparse.py @@ -2725,6 +2725,43 @@ def get_parser(self, required): -c c help ''' +class TestMutuallyExclusiveNested(MEMixin, TestCase): + def get_parser(self, required): + parser = ErrorRaisingArgumentParser(prog='PROG') + group = parser.add_mutually_exclusive_group(required=required) + group.add_argument('-a') + group.add_argument('-b') + group2 = group.add_mutually_exclusive_group(required=required) + group2.add_argument('-c') + group2.add_argument('-d') + group3 = group2.add_mutually_exclusive_group(required=required) + group3.add_argument('-e') + group3.add_argument('-f') + return parser + + failures = [] + successes = [] + successes_when_not_required = [] + + usage_when_not_required = '''\ + usage: PROG [-h] [-a A | -b B | [-c C | -d D | [-e E | -f F]]] + ''' + usage_when_required = '''\ + usage: PROG [-h] (-a A | -b B | (-c C | -d D | (-e E | -f F))) + ''' + + help = '''\ + + optional arguments: + -h, --help show this help message and exit + -a A + -b B + -c C + -d D + -e E + -f F + ''' + # ================================================= # Mutually exclusive group in parent parser tests # ================================================= diff --git a/Misc/NEWS b/Misc/NEWS index 1422627b9052a2..066acbf22509ee 100644 --- a/Misc/NEWS +++ b/Misc/NEWS @@ -228,6 +228,7 @@ Extension Modules Library ------- +- bpo-29553: Fixed ArgumentParses format_usage for mutually exclusive groups. - bpo-29576: Improve some deprecations in importlib. Some deprecated methods now emit DeprecationWarnings and have better descriptive messages.
Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.
Alternative Proxies: