summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorsteven.bethard <devnull@localhost>2009-09-12 15:56:57 +0000
committersteven.bethard <devnull@localhost>2009-09-12 15:56:57 +0000
commit731583b6079d863789ee79bb60481b88c4165a74 (patch)
tree0de685804a389898e78788347f7570e0cbcb7e0d
parent58696981329163d657cdeacbe8940f337aef0a63 (diff)
downloadargparse-731583b6079d863789ee79bb60481b88c4165a74.tar.gz
Fix bug with mutually exclusive groups in parent= parsers.
-rw-r--r--argparse.py11
-rw-r--r--test/test_argparse.py29
2 files changed, 40 insertions, 0 deletions
diff --git a/argparse.py b/argparse.py
index 8950cc7..cb77a4a 100644
--- a/argparse.py
+++ b/argparse.py
@@ -1281,6 +1281,17 @@ class _ActionsContainer(object):
for action in group._group_actions:
group_map[action] = title_group_map[group.title]
+ # add container's mutually exclusive groups
+ # NOTE: if add_mutually_exclusive_group ever gains title= and
+ # description= then this code will need to be expanded as above
+ for group in container._mutually_exclusive_groups:
+ mutex_group = self.add_mutually_exclusive_group(
+ required=group.required)
+
+ # map the actions to their new mutex group
+ for action in group._group_actions:
+ group_map[action] = mutex_group
+
# add all actions to this container or their group
for action in container._actions:
group_map.get(action, self)._add_action(action)
diff --git a/test/test_argparse.py b/test/test_argparse.py
index 34a9c3f..6c8ee06 100644
--- a/test/test_argparse.py
+++ b/test/test_argparse.py
@@ -1848,6 +1848,35 @@ class TestParentParsers(TestCase):
-y Y
'''))
+ def test_groups_parents(self):
+ parent = ErrorRaisingArgumentParser(add_help=False)
+ g = parent.add_argument_group(title='g', description='gd')
+ g.add_argument('-w')
+ g.add_argument('-x')
+ m = parent.add_mutually_exclusive_group()
+ m.add_argument('-y')
+ m.add_argument('-z')
+ parser = ErrorRaisingArgumentParser(parents=[parent])
+
+ self.assertRaises(ArgumentParserError, parser.parse_args,
+ ['-y', 'Y', '-z', 'Z'])
+
+ parser_help = parser.format_help()
+ self.assertEqual(parser_help, textwrap.dedent('''\
+ usage: test_argparse.py [-h] [-w W] [-x X] [-y Y | -z Z]
+
+ optional arguments:
+ -h, --help show this help message and exit
+ -y Y
+ -z Z
+
+ g:
+ gd
+
+ -w W
+ -x X
+ '''))
+
# ==============================
# Mutually exclusive group tests
# ==============================