我试着用
import sys, getopt
def main(argv):
inputfile = ''
outputfile = ''
dpd = ''
opts, args = getopt.getopt(argv,"hi:o:",["ifile=","ofile=","dpd="])
print(opts)
for opt, arg in opts:
print(opt)
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
elif opt in ("-d","--dpd"):
dpd = arg
print ('Input file is ', inputfile)
print ('Output file is ', outputfile)
print ('DPD', dpd)
if __name__ == "__main__":
main(sys.argv[1:])
并运行,python3 demo.py -i 65 -o ale -d 45
但它给出了错误
getopt.GetoptError: 选项 -d 无法识别
我想通过 6 pass 参数我该怎么做??
您应该使用argparse
,它比getopt
.
但问题是你忘记声明-d
为一个选项:
opts, args = getopt.getopt(argv,"hi:o:d:",["ifile=","ofile=","dpd="])
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句