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

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

问题描述

我意识到这很像 使用 argparse 模块在 Python 中设置两个互斥选项的默认选项 尽管从不同的角度来看(并且那里给出的答案似乎没有帮助).

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 仍然设置为真(因为默认),尽管是互斥块的一部分.

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.

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

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