Python argparser 与多个组中的相同参数互斥

Python argparser mutually exclusive with same argument in multiple groups

我使用 argparser 作为一种通用方式来提供对多个不同文件的输入,这些文件用于在末尾生成 json,并发送到数据库。 话虽如此,我想使用多个相互排斥的组,并选择一个标志作为多个不同组的一部分(如下例所示)。

parser = argparser.argumentParser

group1 = parser.add_mutually_exclusive_group()
group2 = parser.add_mutually_exclusive_group(required=True)
group3 = parser.add_mutually_exclusive_group()

group1.add_argument('-a', type=int) 
group1.add_argument('-d', type=int)

group2.add_argument('-z', type=int)
group2.add_argument('-x', type=int)

group3.add_argument('-a', type=int)
group3.add_argument('-z', type=int)

这意味着 -d 和 -z 可以一起使用(但 -z 或 -x 是强制性的),让我可以选择 -a -d -x 或 -z -d

出于某种原因,argparser 认为每个 -a 或 -z 标志都是冲突的,所以我将 conflict_handler 添加到 'resolve',但似乎没有效果

我认为您正在寻找的是 argparse 的 sub_parsers 选项。 请检查 python 文档中的 link add_subparsers.

当您将参数添加到 group 时,它也会添加到 parserargument_groupmutually_exclusive_group 组都是定义一些特殊操作的方法(在帮助和测试中),但它们不会改变基本解析。

因此您尝试通过 group3 定义的参数与已经通过其他组定义的参数冲突。我还应该注意到 add_argument 创建了一个参数 Action 对象。

对于 bug/issue,我想出了一种将预先存在的操作添加到新组的方法。也就是将之前创建的-a-z添加到group3的方法。实际上,我将其编写为一种定义具有现有操作列表的组的方式。这并不难做到。但是显示这样的组需要对 usage 格式化程序进行重大重写。

https://bugs.python.org/issue10984

mutually_exclusive_group 做了两件事——它修改了 usage——如果可能的话。它进行 'mutually-exclusive' 测试。否则它不会修改解析。您可以在解析后执行相同的测试。

在您的示例中,所有参数的默认值为 None。所以在解析之后,你可以这样做:

if args.a is not None and args.z is not None:
   parse.error('cannot use both -a and -z')

在 bug/issue 中我修改了 add_mutually_exclusive_group 以有效地做到:

group1 = parser.add_mutually_exclusive_group()
group2 = parser.add_mutually_exclusive_group(required=True)

a1 = group1.add_argument('-a', type=int) # hang onto the newly created Action
group1.add_argument('-d', type=int)

a2 = group2.add_argument('-z', type=int)
group2.add_argument('-x', type=int)

group3 = parser.add_mutually_exclusive_group()
group3._group_actions.append(a1)      # add existing Action to group
group3._group_actions.append(a2)
#group3.add_argument('-a', type=int)
#group3.add_argument('-z', type=int)

即指向现有Actions的指针直接添加到新组中,而不经过add_argument

测试group3:

2347:~/mypy$ python3 stack47670008.py -z 3 -a3
usage: stack47670008.py [-h] [-a A | -d D] (-z Z | -x X)
stack47670008.py: error: argument -a: not allowed with argument -z

2347:~/mypy$ python3 stack47670008.py -z 3 -d3
Namespace(a=None, d=3, x=None, z=3)

2347:~/mypy$ python3 stack47670008.py -h
usage: stack47670008.py [-h] [-a A | -d D] (-z Z | -x X)

optional arguments:
  -h, --help  show this help message and exit
  -a A
  -d D
  -z Z
  -x X

group1group2 出现在用法中,但没有出现 group3