简体   繁体   中英

how to pass argument in command line using getopt

I am trying to pass two argument in Python command line using getopt. My code is:

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:])

Basically I am passing two arguments in command line: (1) dataset (2) build, but it is not printing at the end. Can someone help?

You need to assign the values of the command line to your variables. You now assign the values to input file and output file instead of database and build.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM