CLI 选项是 Python 中其他两个选项和优先级的聚合 [英] CLI option that is an aggregation of two other options and precedence, in python
问题描述
我使用 argparse
来解析 Python 脚本中的 CLI 选项.我将创建一个等同于指定另外两个标志的标志.所以
I'm using argparse
to parse CLI options in my Python scripts. I would create a flag that is equivalent to specifying two other flags. So
python myscript.py --flagc
相当于
python myscript.py --flaga --flagb
这是我的代码:
args = parser.parse_args()
if args.flagc:
args.flaga = True
args.flagb = True
问题是flaga
和flagb
也有相反的标志,no-flaga
和no-flagb
:
The problem is flaga
and flagb
have also opposite flags, no-flaga
and no-flagb
:
parser.add_argument("--flaga", dest="flaga", action="store_true")
parser.add_argument("--no-flaga", dest="flaga", action="store_false")
parser.set_defaults(flaga=False)
等等.所以我可能会遇到标志优先级的问题.例如,如果我调用脚本:
and so on. So I could have trouble with flags precedences. For example, if I call the script with:
python myscript.py --flagc --no-flaga
我得到 args.flaga
的 True
,但通常你想要 False
,因为最后一个标志优先.
I get True
for args.flaga
, but normally you want False
, since last flag have precedence.
我该如何管理?
推荐答案
是的,通过在解析后处理 flag_c
来丢失优先级信息.通常这个问题支持这种后期处理,但在这里自定义操作可能是更好的选择.
Right, by processing flag_c
after parsing you loose the precedence information. Often that issue argues in favor of this post processing, but here a custom Action might be the better choice.
class MyAction(argparse._StoreTrueAction):
def __call__(self, parser, namespace, values, option_string=None):
# adapt from _StoreConst
# leave the self.dest set; could deactivate
setattr(namespace, self.dest, self.const)
setattr(namespace, 'flaga', True)
setattr(namespace, 'flagb', True)
parser.add_argument("--flagc", action=MyAction)
print(parser.parse_args('--flaga'.split()))
print(parser.parse_args('--flagc'.split()))
print(parser.parse_args('--flagc --no-flaga'.split()))
印刷:
1027:~/mypy$ python stack33264649.py
Namespace(flaga=True, flagb=False, flagc=False)
Namespace(flaga=True, flagb=True, flagc=True)
Namespace(flaga=False, flagb=True, flagc=True)
这是完成这项工作的最简单的自定义类.它利用其父级设置的默认值(特别是 nargs=0
).或者,它可以基于 _StoreConst
甚至 Action
.
It's the simplest custom class that does the job. It takes advantage of the defaults set by its parent (esp. nargs=0
). Alternatively it could be based off of _StoreConst
or even Action
.
我将 setattr(namespace, self.dest, self.const)
留在原处.对 --flagc
是否被调用有一个准确的指示没有什么坏处.而且它比试图抑制 flagc
默认更简单.
I'm leaving the setattr(namespace, self.dest, self.const)
in place. There's little harm in having an accurate indicator of whether --flagc
was invoked or not. And it is simpler than trying to suppress flagc
default.
好的,取消默认设置并不难:
OK, suppressing the default isn't that hard:
class MyAction(argparse._StoreTrueAction):
def __init__(self, *args, **kwargs):
super(MyAction, self).__init__(*args,**kwargs)
self.default=argparse.SUPPRESS
def __call__(self, parser, namespace, values, option_string=None):
setattr(namespace, 'flaga', True)
setattr(namespace, 'flagb', True)
这篇关于CLI 选项是 Python 中其他两个选项和优先级的聚合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!