[英]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.