参数组之间的互斥 [英] Mutual exclusion between argument groups

查看:20
本文介绍了参数组之间的互斥的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 argparse 模块实现以下参数依赖项:<代码>./prog [-h |[-v schema] 文件]这意味着用户必须传递 -h 或文件,如果传递了文件,则用户可以选择传递 -v 架构.

I'm trying to implement the following argument dependency using the argparse module: ./prog [-h | [-v schema] file] meaning the user must pass either -h or a file, if a file is passed the user can optionally pass -v schema.

这就是我现在所拥有的,但似乎不起作用:

That's what I have now but that doesn't seem to be working:

import argparse

parser = argparse.ArgumentParser()
mtx = parser.add_mutually_exclusive_group()
mtx.add_argument('-h', ...)  
grp = mtx.add_argument_group()
grp.add_argument('-v', ...)
grp.add_argument('file', ...)   
args = parser.parse_args()

看来您无法将 arg 组添加到互斥组中,还是我遗漏了什么?

It looks like you can't add an arg group to a mutex group or am I missing something?

推荐答案

如果 -h 表示默认帮助,那么这就是你所需要的(这个帮助已经是独占的)

If -h means the default help, then this is all you need (this help is already exclusive)

import argparse
parser = argparse.ArgumentParser()
parser.add_argument('file')
parser.add_argument('-s','--schema')
parser.parse_args('-h'.split())  # parser.print_help()

生产

usage: stack23951543.py [-h] [-s SCHEMA] file
...

如果 -h 是指其他操作,让我们将其重命名为 -x.这将接近您所描述的

If by -h you mean some other action, lets rename it -x. This would come close to what you describe

parser = argparse.ArgumentParser()
parser.add_argument('-s','--schema', default='meaningful default value')
mxg = parser.add_mutually_exclusive_group(required=True)
mxg.add_argument('-x','--xxx', action='store_true')
mxg.add_argument('file', nargs='?')
parser.parse_args('-h'.split())

用法是:

usage: stack23951543.py [-h] [-s SCHEMA] (-x | file)

现在 -xfile 是必需的(但不是两者都需要).-s 在任何一种情况下都是可选的,但是对于有意义的默认值,是否省略它并不重要.如果给出了 -x,你可以忽略 -s 值.

Now -x or file is required (but not both). -s is optional in either case, but with a meaningful default, it doesn't matter if it is omitted. And if -x is given, you can just ignore the -s value.

如有必要,您可以在解析后测试args,以确认如果args.file 不是None,则args.schema 可以'要么.

If necessary you could test args after parsing, to confirm that if args.file is not None, then args.schema can't be either.

之前我写过(可能是想多了):

Earlier I wrote (maybe over thinking the question):

argument_group 不能添加到 mutually_exclusive_group.这两种组具有不同的目的和功能.之前有关于此的 SO 讨论(请参阅相关"),以及一些相关的 Python 错误问题.如果您想要超越简单的互斥组的测试,您可能应该在 parse_args 之后进行自己的测试.这可能还需要您自己的 usage 行.

An argument_group cannot be added to a mutually_exclusive_group. The two kinds of groups have different purposes and functions. There are previous SO discussions of this (see 'related'), as well as a couple of relevant Python bug issues. If you want tests that go beyond a simple mutually exclusive group, you probably should do your own testing after parse_args. That may also require your own usage line.

argument_group 只是帮助部分中对参数进行分组和标记的一种方式.

An argument_group is just a means of grouping and labeling arguments in the help section.

mutually_exclusive_group 会影响 usage 格式(如果可以的话),并且还会在 parse_args 期间运行测试.对两者都使用组"意味着它们的联系比实际情况要多.

A mutually_exclusive_group affects the usage formatting (if it can), and also runs tests during parse_args. The use of 'group' for both implies that they are more connected than they really are.

http://bugs.python.org/issue11588 要求嵌套组,以及还要测试包容性".我试图证明组"不足以表达用户想要的所有类型的测试.但是,概括测试机制是一回事,提出直观的 API 则是另一回事.像这样的问题表明 argparse 确实需要某种嵌套组"语法.

http://bugs.python.org/issue11588 asks for nested groups, and the ability to test for 'inclusivity' as well. I tried to make the case that 'groups' aren't general enough to express all the kinds of testing that users want. But it's one thing to generalize the testing mechanism, and quite another to come up with an intuitive API. Questions like this suggest that argparse does need some sort of 'nested group' syntax.

这篇关于参数组之间的互斥的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆