[Python-checkins] r88268 - in python/branches/release27-maint/Lib: argparse.py test/test_argparse.py

steven.bethard python-checkins at python.org
Sun Jan 30 15:05:38 CET 2011


Author: steven.bethard
Date: Sun Jan 30 15:05:38 2011
New Revision: 88268

Log:
#10680: fix mutually exclusive arguments in argument groups.

Modified:
   python/branches/release27-maint/Lib/argparse.py
   python/branches/release27-maint/Lib/test/test_argparse.py

Modified: python/branches/release27-maint/Lib/argparse.py
==============================================================================
--- python/branches/release27-maint/Lib/argparse.py	(original)
+++ python/branches/release27-maint/Lib/argparse.py	Sun Jan 30 15:05:38 2011
@@ -1482,6 +1482,7 @@
         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)

Modified: python/branches/release27-maint/Lib/test/test_argparse.py
==============================================================================
--- python/branches/release27-maint/Lib/test/test_argparse.py	(original)
+++ python/branches/release27-maint/Lib/test/test_argparse.py	Sun Jan 30 15:05:38 2011
@@ -2505,6 +2505,46 @@
         '''
 
 
+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):


More information about the Python-checkins mailing list