如何使用 getopt 在命令行中传递参数

how to pass argument in command line using getopt

我正在尝试使用 getopt 在 Python 命令行中传递两个参数。我的代码是:

import sys, getopt

def main(argv):
   dataset = ''
   build = ''
   try:
      opts, args = getopt.getopt(argv,"hd:b:",["dataset=","build="])
   except getopt.GetoptError:
      print 'performance_test.py -d <dataset> -b <build>'
      sys.exit(2)
   for opt, arg in opts:
      if opt == '-h':
         print 'performance_test.py -d <dataset> -b <build>'
         sys.exit()
      elif opt in ("-d", "--dataset"):
         inputfile = arg
      elif opt in ("-b", "--build"):
         outputfile = arg
   print 'Dataset is "', dataset
   print 'Build version is "', build

if __name__ == "__main__":
    main(sys.argv[1:])

基本上我在命令行中传递了两个参数:(1)数据集(2)构建,但最后没有打印。有人可以帮忙吗?

您需要将命令行的值分配给您的变量。您现在将值分配给输入文件和输出文件,而不是数据库和构建。