Option 1: Use arguments instead of options
This might be a viable approach since it seems you might require all three inputs from the user to run normally.
According to the optparse docs
A program should be able to run just fine with no options whatsoever
In this case your solution would like
import optparse
import sys
if __name__ == '__main__':
parser = optparse.OptionParser()
(options, args) = parser.parse_args()
if len(args) != 3:
print 'please specify all required arguments - qtype qname output_file'
sys.exit(-1)
qtype, qname, output = args
if qtype == 'web':
pass
elif qtype == 'local':
pass
else:
print 'no qtype specified! exiting'
sys.exit(-1)
Then you can use all the arguments as strings and either process them directly or turn them into files / url for web requests.
Example command line:
program.py web blah blah
Option 2: Use options anyway
import optparse
import sys
if __name__ == '__main__':
parser = optparse.OptionParser()
parser.add_option('--qtype', action='store', dest='qtype', type='string')
parser.add_option('--qname', action='store', dest='qname', type='string')
parser.add_option('--output', action='store', type='string', dest='filename')
(options, args) = parser.parse_args()
if options.qtype == 'web':
pass
elif options.qtype == 'local':
pass
else:
print 'no qtype specified! exiting'
sys.exit(-1)
Example usage:
program.py --qtype web --qname blah --output blah