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

标签 python argparse

我使用 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添加到“解决”中,但似乎没有效果

最佳答案

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

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

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

https://bugs.python.org/issue10984

mutually_exclusive_group 做了两件事 - 它修改了usage - 如果可能的话。它进行“互斥”测试。否则它不会修改解析。解析后您可以执行相同的测试。

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

if args.a is not None and args.z is not None:
   parse.error('cannot use both -a and -z')
<小时/>

在错误/问题中,我修改了 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

关于Python argparse 与多个组中的相同参数互斥,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47670008/

相关文章:

python - 如何在 Python3 中使用 argparse 从两组命令行参数中解析出一组?

python - Argparse 可选标准输入读取和/或标准输出输出

python - argparse add_argument 来自字典

python - Pandas to_datetime 解析错误的年份

python - 外壳 : insert a blank/new line two lines above pattern

python - 尝试用 python 编写 "1-x+x^2-x^3+x^4-x^5..."系列,有人有什么想法吗?

python - argparse.ArgumentParser() 函数的描述参数

python - 如何显示自定义消息而不是 Argparse 生成的默认帮助消息?

python - 使用 Google Cloud Stackdriver 在 Kubernetes Engine 上记录 Python 代码的重复日志条目

python - 测试 : excessive memory usage with large number of tests