使用 argparse 模块在 Python 中设置两个互斥选项的默认选项 [英] Setting default option in Python of two mutually exclusive options using the argparse module
本文介绍了使用 argparse 模块在 Python 中设置两个互斥选项的默认选项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
import argparse
parser = argparse.ArgumentParser(description="List or update! That is the question!")
group = parser.add_mutually_exclusive_group()
group.add_argument('-l', '--list', dest="update", action='store_false')
group.add_argument('-u', '--update', dest="update", action='store_true')
args = parser.parse_args()
print args
如果用户没有指定任何可选参数,我想要update=False
.
If the user does not specify any optional arguments I want update=False
.
:我把我的问题改成不那么笼统,这很令人困惑.对不起.
: I changed my question to not be so general, it was confusing. Sorry.
推荐答案
为 --list
选项的参数添加 default=False
让它做你想做的事.我不确定为什么,并注意将它添加到 --update
选项的参数中,而不是什么都不做.
Adding default=False
for the --list
option's parameters makes it do what you want. I am not exactly sure why, and note that adding it to the --update
option's parameters instead does nothing.
这篇关于使用 argparse 模块在 Python 中设置两个互斥选项的默认选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文