PyDocs

Nav

Command-line interface libraries

ArgumentParser.add_mutually_exclusive_group

argparse

method of ArgumentParser (source) Deprecated 3.11
  • ArgumentParser.add_mutually_exclusive_group(required=False)
Deprecated since 3.11, removed in 3.14. Calling add_argument_group() or add_mutually_exclusive_group() on a mutually exclusive group now raises an exception. This nesting was never supported, often failed to work correctly, and was unintentionally exposed through inheritance.
Create a mutually exclusive group. argparse will make sure that only one of the arguments in the mutually exclusive group was present on the command line:
1 Example
>>> parser = argparse.ArgumentParser(prog='PROG')
>>> group = parser.add_mutually_exclusive_group()
>>> group.add_argument('--foo', action='store_true')
>>> group.add_argument('--bar', action='store_false')
>>> parser.parse_args(['--foo'])
Namespace(bar=True, foo=True)
>>> parser.parse_args(['--bar'])
Namespace(bar=False, foo=False)
>>> parser.parse_args(['--foo', '--bar'])
usage: PROG [-h] [--foo | --bar]
PROG: error: argument --bar: not allowed with argument --foo
from the Python docs
See Also
No see-alsos for argparse/ArgumentParser.add_mutually_exclusive_group
0 Notes
No notes for ArgumentParser.add_mutually_exclusive_group