简体   繁体   中英

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 ?

You should use action=store_false then.

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

Please try to read the documentation before asking.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM