# HG changeset patch # Parent 6d278f426417063b12703f980cb17411849b6326 Allow arguments to be added to mulitple mutually exclusive groups diff -r 6d278f426417 Doc/library/argparse.rst --- a/Doc/library/argparse.rst Fri Jul 05 18:05:29 2013 -1000 +++ b/Doc/library/argparse.rst Sat Jul 06 16:03:35 2013 +0200 @@ -1728,6 +1728,28 @@ usage: PROG [-h] (--foo | --bar) PROG: error: one of the arguments --foo --bar is required + Finally, you can also add existing arguments to a mutually exclusive + group. This allows you to include arguments in more than one mutually + exclusive group:: + + >>> import argparse + >>> parser = argparse.ArgumentParser(prog='PROG') + >>> foo_arg = parser.add_argument('--foo', action='store_true') + >>> bar_arg = parser.add_argument('--bar', action='store_false') + >>> baz_arg = parser.add_argument('--baz', action='store_true') + >>> group = parser.add_mutually_exclusive_group(foo_arg, bar_arg, required=True) + >>> group = parser.add_mutually_exclusive_group(foo_arg, baz_arg, required=True) + >>> parser.parse_args(["--bar", "--baz"]) + Namespace(bar=False, baz=True, foo=False) + >>> parser.parse_args(["--foo"]) + Namespace(bar=False, baz=False, foo=True) + >>> parser.parse_args(["--foo", "--bar"]) + usage: PROG [-h] (--foo | --bar) [--baz] + PROG: error: argument --bar: not allowed with argument --foo + >>> parser.parse_args(["--foo", "--baz"]) + usage: PROG [-h] (--foo | --bar) [--baz] + PROG: error: argument --baz: not allowed with argument --foo + Note that currently mutually exclusive argument groups do not support the *title* and *description* arguments of :meth:`~ArgumentParser.add_argument_group`. diff -r 6d278f426417 Lib/argparse.py --- a/Lib/argparse.py Fri Jul 05 18:05:29 2013 -1000 +++ b/Lib/argparse.py Sat Jul 06 16:03:35 2013 +0200 @@ -1339,9 +1339,11 @@ self._action_groups.append(group) return group - def add_mutually_exclusive_group(self, **kwargs): + def add_mutually_exclusive_group(self, *args, **kwargs): group = _MutuallyExclusiveGroup(self, **kwargs) self._mutually_exclusive_groups.append(group) + for action in args: + group._group_actions.append(action) return group def _add_action(self, action): diff -r 6d278f426417 Lib/test/test_argparse.py --- a/Lib/test/test_argparse.py Fri Jul 05 18:05:29 2013 -1000 +++ b/Lib/test/test_argparse.py Sat Jul 06 16:03:35 2013 +0200 @@ -2689,6 +2689,42 @@ -c c help ''' +class TestMutuallyExclusiveGroupWithExistingArguments(MEMixin, TestCase): + + def get_parser(self, required): + parser = ErrorRaisingArgumentParser(prog='PROG') + a_action = parser.add_argument('-a', action='store_true', help='a help') + b_action = parser.add_argument('-b', action='store_true', help='b help') + c_action = parser.add_argument('-c', action='store_true', help='c help') + d_action = parser.add_argument('-d', action='store_true', help='d help') + parser.add_mutually_exclusive_group(a_action, c_action, required=required) + parser.add_mutually_exclusive_group(a_action, d_action, required=required) + return parser + + failures = ['-a -c', '-a -d', '-a -c -d', '-a -b -c', '-a -b -d', '-a -b -c -d'] + successes = [ + ('-a -b', NS(a=True, b=True, c=False, d=False)), + ('-c -d', NS(a=False, b=False, c=True, d=True)), + ('-c -b -d', NS(a=False, b=True, c=True, d=True)), + ] + successes_when_not_required = [ + ('', NS(a=False, b=False, c=False, d=False)), + ('-b', NS(a=False, b=True, c=False, d=False)), + ] + + usage_when_required = usage_when_not_required = '''\ + usage: PROG [-h] [-a] [-b] [-c] [-d] + ''' + help = '''\ + + optional arguments: + -h, --help show this help message and exit + -a a help + -b b help + -c c help + -d d help + ''' + # ================================================= # Mutually exclusive group in parent parser tests # =================================================