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

时间:2013-09-24 01:47:28

标签: python

#!/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

输出:

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

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

问题:

如何将debugflag的默认值从True切换为False

1 个答案:

答案 0 :(得分:1)

您应该使用action=store_false

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

请在询问前尝试阅读documentation