diff options
author | Steven Bethard <steven.bethard@gmail.com> | 2010-11-01 16:29:26 (GMT) |
---|---|---|
committer | Steven Bethard <steven.bethard@gmail.com> | 2010-11-01 16:29:26 (GMT) |
commit | 49998eec49f16262381219eb43dcb58bbe092842 (patch) | |
tree | d40bdfbc29ac2d3538a27eadd05c8c9b8101bfcf | |
parent | 1ca45a5292a382d5902b80de5c8274994a985e84 (diff) | |
download | cpython-49998eec49f16262381219eb43dcb58bbe092842.zip cpython-49998eec49f16262381219eb43dcb58bbe092842.tar.gz cpython-49998eec49f16262381219eb43dcb58bbe092842.tar.bz2 |
Fix for issue 9355 where with multiple mutually exclusive arguments, some brackets were being lost in the usage messages
-rw-r--r-- | Lib/argparse.py | 10 | ||||
-rw-r--r-- | Lib/test/test_argparse.py | 19 |
2 files changed, 27 insertions, 2 deletions
diff --git a/Lib/argparse.py b/Lib/argparse.py index 223790c..38dd8e6 100644 --- a/Lib/argparse.py +++ b/Lib/argparse.py @@ -392,10 +392,16 @@ class HelpFormatter(object): for action in group._group_actions: group_actions.add(action) if not group.required: - inserts[start] = '[' + if start in inserts: + inserts[start] += ' [' + else: + inserts[start] = '[' inserts[end] = ']' else: - inserts[start] = '(' + if start in inserts: + inserts[start] += ' (' + else: + inserts[start] = '(' 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 30302e9..1503ccd 100644 --- a/Lib/test/test_argparse.py +++ b/Lib/test/test_argparse.py @@ -2163,6 +2163,25 @@ class TestMutuallyExclusiveGroupErrors(TestCase): raises(ValueError, add_argument, 'bar', nargs=1) raises(ValueError, add_argument, 'bar', nargs=argparse.PARSER) + def test_help(self): + parser = ErrorRaisingArgumentParser(prog='PROG') + group1 = parser.add_mutually_exclusive_group() + group1.add_argument('--foo', action='store_true') + group1.add_argument('--bar', action='store_false') + group2 = parser.add_mutually_exclusive_group() + group2.add_argument('--soup', action='store_true') + group2.add_argument('--nuts', action='store_false') + expected = '''\ + usage: PROG [-h] [--foo | --bar] [--soup | --nuts] + + optional arguments: + -h, --help show this help message and exit + --foo + --bar + --soup + --nuts + ''' + self.assertEqual(parser.format_help(), textwrap.dedent(expected)) class MEMixin(object): |