从命令行传递多个参数

用户14784453

我试着用

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] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章