From 7f41b88bb7b978205a438c7eea2b7f6bba6862d0 Mon Sep 17 00:00:00 2001 From: Steven Bethard Date: Sun, 30 Jan 2011 14:05:38 +0000 Subject: [PATCH] #10680: fix mutually exclusive arguments in argument groups. --- Lib/argparse.py | 1 + Lib/test/test_argparse.py | 40 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+) diff --git a/Lib/argparse.py b/Lib/argparse.py index 75cc28a0433..8e1722a2e32 100644 --- a/Lib/argparse.py +++ b/Lib/argparse.py @@ -1482,6 +1482,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) diff --git a/Lib/test/test_argparse.py b/Lib/test/test_argparse.py index 50a6d493905..0bcc762ba5f 100644 --- a/Lib/test/test_argparse.py +++ b/Lib/test/test_argparse.py @@ -2505,6 +2505,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):