What does the add_mutually_exclusive_group function mean?

Reference What does the add_mutually_exclusive_group function mean? - Cloud + Community - Tencent Cloud

Create a mutually exclusive group. argparsewill ensure that only one parameter in the mutex group is available on the command line:

>>> 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

add_mutually_exclusive_group()The method also accepts a required parameter, indicating that at least one parameter in the mutex group is required:

>>> parser = argparse.ArgumentParser(prog='PROG')
>>> group = parser.add_mutually_exclusive_group(required=True)
>>> group.add_argument('--foo', action='store_true')
>>> group.add_argument('--bar', action='store_false')
>>> parser.parse_args([])
usage: PROG [-h] (--foo | --bar)
PROG: error: one of the arguments --foo --bar is required
 

Note that add_argument_group()the title and description parameters are currently not supported for mutually exclusive parameter groups.

Guess you like

Origin blog.csdn.net/weixin_36670529/article/details/113783239