]> granicus.if.org Git - python/commitdiff
#10680: fix mutually exclusive arguments in argument groups.
authorGeorg Brandl <georg@python.org>
Sun, 30 Jan 2011 12:19:35 +0000 (12:19 +0000)
committerGeorg Brandl <georg@python.org>
Sun, 30 Jan 2011 12:19:35 +0000 (12:19 +0000)
Lib/argparse.py
Lib/test/test_argparse.py
Misc/NEWS

index 5fd82dac433b6904326ce49b47d7dd2a165d833b..de3cd114794c1eb11c125d6809ec0a2e227d5096 100644 (file)
@@ -1495,6 +1495,7 @@ class _ArgumentGroup(_ActionsContainer):
         self._defaults = container._defaults
         self._has_negative_number_optionals = \
             container._has_negative_number_optionals
+        self._mutually_exclusive_groups = container._mutually_exclusive_groups
 
     def _add_action(self, action):
         action = super(_ArgumentGroup, self)._add_action(action)
index 36415f4057df2c1711ea51c12881bb1f27d60c62..03c95fade4125e27d49f68e232f878295a292b2b 100644 (file)
@@ -2540,6 +2540,46 @@ class TestMutuallyExclusiveOptionalsMixed(MEMixin, TestCase):
         '''
 
 
+class TestMutuallyExclusiveInGroup(MEMixin, TestCase):
+
+    def get_parser(self, required=None):
+        parser = ErrorRaisingArgumentParser(prog='PROG')
+        titled_group = parser.add_argument_group(
+            title='Titled group', description='Group description')
+        mutex_group = \
+            titled_group.add_mutually_exclusive_group(required=required)
+        mutex_group.add_argument('--bar', help='bar help')
+        mutex_group.add_argument('--baz', help='baz help')
+        return parser
+
+    failures = ['--bar X --baz Y', '--baz X --bar Y']
+    successes = [
+        ('--bar X', NS(bar='X', baz=None)),
+        ('--baz Y', NS(bar=None, baz='Y')),
+    ]
+    successes_when_not_required = [
+        ('', NS(bar=None, baz=None)),
+    ]
+
+    usage_when_not_required = '''\
+        usage: PROG [-h] [--bar BAR | --baz BAZ]
+        '''
+    usage_when_required = '''\
+        usage: PROG [-h] (--bar BAR | --baz BAZ)
+        '''
+    help = '''\
+
+        optional arguments:
+          -h, --help  show this help message and exit
+
+        Titled group:
+          Group description
+
+          --bar BAR   bar help
+          --baz BAZ   baz help
+        '''
+
+
 class TestMutuallyExclusiveOptionalsAndPositionalsMixed(MEMixin, TestCase):
 
     def get_parser(self, required):
index 80c46397f1d1abdbdc2e1ead51caf6b948a42bd3..c58114af4b6138fe7c6891272da2c024f4a0f7bd 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -91,6 +91,9 @@ Library
 - Issue #10961: The new pydoc server now better handles exceptions raised
   during request handling.
 
+- Issue #10680: Fix mutually exclusive arguments for argument groups in
+  argparse.
+
 Build
 -----