这是我的程序(test.py):如何使用python的命令行shell传递命令行选项?
#!/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:])
使用MS-DOS命令行中,我可以通过这样的-h选项(在test.py定义):
python test.py -h
在MSDOS命令行会那么输出中这样:
test.py -i <inputfile> -o <outputfile>
但我怎么会在python interactive 模式中通过-h选项,就像使用msdos命令行一样?