Saturday, 17 August 2013

Can command line options be passed in the interactive mode of the python interpreter?

Can command line options be passed in the interactive mode of the python
interpreter?

This is my program(test.py):
#!/usr/bin/python
import sys, getopt
def main(argv):
inputfile = ''
outputfile = ''
try:
opts, args = getopt.getopt(argv,"hi:o:",["ifile=","ofile="])
except getopt.GetoptError:
print 'test.py -i <inputfile> -o <outputfile>'
sys.exit(2)
for opt, arg in opts:
if opt == '-h':
print 'test.py -i <inputfile> -o <outputfile>'
sys.exit()
elif opt in ("-i", "--ifile"):
inputfile = arg
elif opt in ("-o", "--ofile"):
outputfile = arg
print 'Input file is "', inputfile
print 'Output file is "', outputfile
if __name__ == "__main__":
main(sys.argv[1:])
To run this program in msdos command line i would do this:
python test.py
To specify an option in msdos command line i would do this:
python test.py -h
To run the program in python interactive mode this works:
execfile('test.py')
But how would i specify the -h option in python interactive mode as was
done using msdos command line?

No comments:

Post a Comment