这是我的程序(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:])
使用 msdos 命令行,我可以像这样传递 -h 选项(在 test.py 中定义):
python test.py -h
然后 msdos 命令行将输出以下内容:
test.py -i <inputfile> -o <outputfile>
但是我如何像使用 msdos 命令行那样在 python 交互模式下传递 -h 选项?