Setting default option in Python of two mutually exclusive options using the argparse module

限于喜欢 提交于 2019-12-25 08:16:12

问题


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

If the user does not specify any optional arguments I want update=False.

[Edit]: I changed my question to not be so general, it was confusing. Sorry.


回答1:


You should set different dest for the 2 options.

group.add_argument('-f', '--foo', dest="foo", action='store_false')



回答2:


I think that you want add_mutually_exclusive_group(). The documentation is here.




回答3:


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.



来源:https://stackoverflow.com/questions/9365486/setting-default-option-in-python-of-two-mutually-exclusive-options-using-the-arg

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!