From d01e2eec11d6a6780f5023cede95b64d8ec3566e Mon Sep 17 00:00:00 2001 From: andrewnester Date: Wed, 15 Feb 2017 11:51:34 +0300 Subject: [PATCH 1/3] bpo-29553 - Fixed ArgumentParses format_usage for mutually exclusive groups --- Lib/argparse.py | 10 ++++++++-- Lib/test/test_argparse.py | 29 +++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 2 deletions(-) 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..7b15c7584d559c 100644 --- a/Lib/test/test_argparse.py +++ b/Lib/test/test_argparse.py @@ -2725,6 +2725,35 @@ def get_parser(self, required): -c c help ''' +class TestMutuallyExclusiveNested(TestCase): + def test_format_usage(self): + parser = ErrorRaisingArgumentParser(prog='PROG') + group = parser.add_mutually_exclusive_group() + group.add_argument('-a') + group.add_argument('-b') + group2 = group.add_mutually_exclusive_group() + group2.add_argument('-c') + group2.add_argument('-d') + group3 = group2.add_mutually_exclusive_group() + group3.add_argument('-e') + group3.add_argument('-f') + self.assertEqual(parser.format_usage(), + 'usage: PROG [-h] [-a A | -b B | [-c C | -d D | [-e E | -f F]]]\n') + + def test_format_usage_required(self): + parser = ErrorRaisingArgumentParser(prog='PROG') + group = parser.add_mutually_exclusive_group(required=True) + group.add_argument('-a') + group.add_argument('-b') + group2 = group.add_mutually_exclusive_group(required=True) + group2.add_argument('-c') + group2.add_argument('-d') + group3 = group2.add_mutually_exclusive_group(required=True) + group3.add_argument('-e') + group3.add_argument('-f') + self.assertEqual(parser.format_usage(), + 'usage: PROG [-h] (-a A | -b B | (-c C | -d D | (-e E | -f F)))\n') + # ================================================= # Mutually exclusive group in parent parser tests # ================================================= From 12677f82d346dbaf91927e1c763d4abdb7bf8779 Mon Sep 17 00:00:00 2001 From: andrewnester Date: Sun, 19 Feb 2017 20:19:11 +0300 Subject: [PATCH 2/3] Updated test for ArgumentParser --- Lib/test/test_argparse.py | 48 +++++++++++++++++++++++---------------- 1 file changed, 28 insertions(+), 20 deletions(-) diff --git a/Lib/test/test_argparse.py b/Lib/test/test_argparse.py index 7b15c7584d559c..515ff42e13046d 100644 --- a/Lib/test/test_argparse.py +++ b/Lib/test/test_argparse.py @@ -2725,34 +2725,42 @@ def get_parser(self, required): -c c help ''' -class TestMutuallyExclusiveNested(TestCase): - def test_format_usage(self): +class TestMutuallyExclusiveNested(MEMixin, TestCase): + def get_parser(self, required): parser = ErrorRaisingArgumentParser(prog='PROG') - group = parser.add_mutually_exclusive_group() + group = parser.add_mutually_exclusive_group(required=required) group.add_argument('-a') group.add_argument('-b') - group2 = group.add_mutually_exclusive_group() + group2 = group.add_mutually_exclusive_group(required=required) group2.add_argument('-c') group2.add_argument('-d') - group3 = group2.add_mutually_exclusive_group() + group3 = group2.add_mutually_exclusive_group(required=required) group3.add_argument('-e') group3.add_argument('-f') - self.assertEqual(parser.format_usage(), - 'usage: PROG [-h] [-a A | -b B | [-c C | -d D | [-e E | -f F]]]\n') + return parser - def test_format_usage_required(self): - parser = ErrorRaisingArgumentParser(prog='PROG') - group = parser.add_mutually_exclusive_group(required=True) - group.add_argument('-a') - group.add_argument('-b') - group2 = group.add_mutually_exclusive_group(required=True) - group2.add_argument('-c') - group2.add_argument('-d') - group3 = group2.add_mutually_exclusive_group(required=True) - group3.add_argument('-e') - group3.add_argument('-f') - self.assertEqual(parser.format_usage(), - 'usage: PROG [-h] (-a A | -b B | (-c C | -d D | (-e E | -f F)))\n') + 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 From 882091bbb5b3c1bf5dedeb1f436e36e3aea302ac Mon Sep 17 00:00:00 2001 From: andrewnester Date: Sun, 19 Feb 2017 20:29:08 +0300 Subject: [PATCH 3/3] Updated MISC/NEWS --- Misc/NEWS | 1 + 1 file changed, 1 insertion(+) 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.