使用argparse模块在Python中设置两个互斥选项的默认选项 [英] Setting default option in Python of two mutually exclusive options using the argparse module

查看:245
本文介绍了使用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屋!

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