如果另一个互斥参数为true,则将默认值设置为false [英] Set the default to false if another mutually exclusive argument is true

查看:97
本文介绍了如果另一个互斥参数为true,则将默认值设置为false的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我意识到这很像

I realise this is a lot like Setting default option in Python of two mutually exclusive options using the argparse module although from a different perspective (and the answers given there don't seem to help).

代码块(解析器是argparse.ArgumentParser的实例):

Code block (parser is an instance of argparse.ArgumentParser):

mutex_group = parser.add_mutually_exclusive_group()
mutex_group.add_argument("--show", action="store_true", 
    dest="show", default=True)
mutex_group.add_argument("--insert", action="store_true", 
    dest="insert")

opts = parser.parse_args()

如果未指定--show--insert我都希望默认为--show(因此为default=True),但是如果使用--insert,则opts.show仍设置为true(由于默认值),尽管是互斥块的一部分.

If neither --show or --insert are specified I want to default to --show (hence default=True) but if --insert is used then opts.show is still set true (because of the default), despite being part of a mutually exclusive block.

当测试opt.show是否为True时,当前代码检查是否没有设置其他任何选项,即:

The current code checks that none of the other options have been set when testing whether opt.show is True, i.e.:

if opts.show and not opts.insert:
    do_something()
elif opts.insert:
    do_something_else()

但这不能扩展(将--delete添加到互斥组时会发生什么情况,等等),所以我正在寻找一种更好的方法来使所有其他变量将opts.show设置为false,同时仍然它是默认设置.

but this doesn't scale (what happens when you add --delete to the mutually exclusive group, etc.) so I'm looking for a better way of causing every other variable to make opts.show false while still having it as the default.

在阅读argparse文档时,我认为可以采用自定义操作,但是看不到如何从中访问互斥组的其他成员(理论上,我可以对其进行迭代,然后翻转默认值(如果已设置其余任何值). 另一个选择是颠倒if条件,但这似乎很不干净(如果默认值更改,则if语句的顺序也必须更改).

Reading the argparse docs, I think a custom action would be the way to go but can't see how to access the other members of the mutually exclusive group from within that (the theory being I could iterate over them, and flip the default if any of the rest were set). The other option would be to reverse the if conditions, but that seems unclean (if the default changes, the order of the if statements would have to change also).

推荐答案

在我看来,'store_const'可能是一个更合适的操作(所有参数都指向同一目标).

It occurs to me that perhaps 'store_const' would be a more appropriate action (with all arguments pointing to the same destination).

import argparse
parser = argparse.ArgumentParser()
mutex_group = parser.add_mutually_exclusive_group()
mutex_group.add_argument("--show", action="store_const", 
    dest="mutex", const="show")
mutex_group.add_argument("--insert", action="store_const", 
    dest="mutex", const="insert")
mutex_group.add_argument('--delete', action="store_const",
    dest="mutex", const="delete")


parser.set_defaults(mutex='show')
args = parser.parse_args()
print(args)

现在,您可以使用args.mutex找出要执行的操作.

Now you can use args.mutex to figure out which action to perform.

这篇关于如果另一个互斥参数为true,则将默认值设置为false的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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