简体   繁体   English

如何在指定和参数时将默认的True切换为False?

[英]How to switch default True to False on specifying and argument?

#!/usr/bin/env python

import optparse

p = optparse.OptionParser()
p.add_option("-o", action="store", dest="outfile")
p.add_option("-d", action="store_true", dest="debugflag")
p.set_defaults(debugflag=True)

opts,args = p.parse_args()

print opts, " ", args
print opts.outfile, opts.debugflag

Output: 输出:

$ ./optparseexample.py -o myfile -d
{'outfile': 'myfile', 'debugflag': True}   []
myfile True

$ ./optparseexample.py -o myfile 
{'outfile': 'myfile', 'debugflag': True}   []
myfile True

Question: 题:

How to I switch the default value for debugflag from True to False ? 如何将debugflag的默认值从True切换为False

You should use action=store_false then. 然后,您应该使用action=store_false

p.add_option("-d", action="store_false", dest="debugflag")

Please try to read the documentation before asking. 在询问之前,请尝试阅读文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如果另一个互斥参数为true,则将默认值设置为false - Set the default to false if another mutually exclusive argument is true 函数的参数为​​False和True - Function's argument as False and True 在 parser.add_argument 中同时包含 action='store_true' 和 default=False 有什么意义? - What's the point of having both action='store_true' and default=False in parser.add_argument? python的默认参数值中的None和boolean(True,False)有什么区别? - What is the difference between None and boolean (True, False) in python's default argument values? 为什么 Python 从 1 或 0 切换到 True 或 False? - Why does Python switch from 1 or 0 to True or False? 将argparse参数设置为默认开关 - Set argparse argument as default switch 如何在初始值设定项中更改布尔参数,以便在有参数的情况下变为true,在Python3.6中没有则保持为false? - How to change a Boolean argument in the initializer so it becomes true if there is an argument and stays false if there isn't in Python3.6? 如何返回真假? - How to return True and False? 为每个日期维护默认值 True/False - Maintain a default of True/False for each date SQLALCHEMY 设置默认值 False 可为空 True - SQLALCHEMY set default False nullable True
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM