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