diff options
| author | Flavian Hautbois <flavianh@sicara.com> | 2019-08-25 21:06:45 +0200 |
|---|---|---|
| committer | Berker Peksag <berker.peksag@gmail.com> | 2019-08-25 22:06:45 +0300 |
| commit | da27d9b9dc44913ffee8f28d9638985eaaa03755 (patch) | |
| tree | 58520d3227798af6bdd60f90dae750c6ac58e933 | |
| parent | 5dbe0f59b7a4f39c7c606b48056bc29e406ebf78 (diff) | |
| download | cpython-git-da27d9b9dc44913ffee8f28d9638985eaaa03755.tar.gz | |
bpo-29553: Fix ArgumentParser.format_usage() for mutually exclusive groups (GH-14976)
Co-authored-by: Andrew Nester <andrew.nester.dev@gmail.com>
| -rw-r--r-- | Lib/argparse.py | 10 | ||||
| -rw-r--r-- | Lib/test/test_argparse.py | 40 | ||||
| -rw-r--r-- | Misc/NEWS.d/next/Library/2019-07-27-10-14-45.bpo-29553.TVeIDe.rst | 2 |
3 files changed, 50 insertions, 2 deletions
diff --git a/Lib/argparse.py b/Lib/argparse.py index a300828f9e..d29f161b66 100644 --- a/Lib/argparse.py +++ b/Lib/argparse.py @@ -404,13 +404,19 @@ class HelpFormatter(object): 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 d6d16090eb..86ec6cca51 100644 --- a/Lib/test/test_argparse.py +++ b/Lib/test/test_argparse.py @@ -2813,6 +2813,46 @@ class TestMutuallyExclusiveOptionalsAndPositionalsMixed(MEMixin, TestCase): -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 + + 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 + ''' + + # We are only interested in testing the behavior of format_usage(). + test_failures_when_not_required = None + test_failures_when_required = None + test_successes_when_not_required = None + test_successes_when_required = None + # ================================================= # Mutually exclusive group in parent parser tests # ================================================= diff --git a/Misc/NEWS.d/next/Library/2019-07-27-10-14-45.bpo-29553.TVeIDe.rst b/Misc/NEWS.d/next/Library/2019-07-27-10-14-45.bpo-29553.TVeIDe.rst new file mode 100644 index 0000000000..3472db7bf2 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2019-07-27-10-14-45.bpo-29553.TVeIDe.rst @@ -0,0 +1,2 @@ +Fixed :meth:`argparse.ArgumentParser.format_usage` for mutually exclusive groups. +Patch by Andrew Nester. |
