Search code examples
pythongetopt

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?


Solution

  • 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.