CLI选项,它是python中其他两个选项和优先级的集合 [英] CLI option that is an aggregation of two other options and precedence, in python

查看:81
本文介绍了CLI选项,它是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

问题是flagaflagb也具有相反的标志no-flagano-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屋!

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