Parsing command line arguments in python script (getopt woes)

Can anyone determine why the following script does not print the arguments passed?

import sys, getopt def usage(): print 'Unknown arguments' def main(argv): try: opts, args = getopt.getopt(argv,'fdmse:d',['files=','data-source=','mode=','start','end']) except getopt.GetoptError: usage() sys.exit(999) for opt, arg in opts: # print opt,arg if opt in('-f','--files'): print 'files: ', arg # if __name__ == "__main__": main(sys.argv[1:]) 

When I run the script on the command line and pass the arguments -f=dummy.csv , usage() is called instead - WHY?

By the way, I find the program flow logic a bit strange (I copied it from here ). As a rule, I would think that the logic would be implemented in the try branch, and then AFTER the exception handler arrives.

Is this (as shown in the above code) a Pythonic way of writing try / catch blocks?

+4
source share
3 answers

As a rule, I would think that the logic would be implemented in the try branch

"Usually"? What usually means?

What should the program do? What exceptions make sense? What the program does in response to exceptions.

No normal". More than a normal assignment statement or definition of a normal function.

Your program does what makes sense to achieve the desired final state. There is no "normal".

+1
source

Did you get any answers?

One way to debug python exceptions is to move (or temporarily copy for debugging) the code from the try block. You will get a complete trace.

And, of course, another way is to reduce the test case. Here I reduced the problems to three lines and tried the solution targeted at @ s.lott (using "f:" in the getopts call) and also finally show how the call behaves with some different test data: / p>

 $ cat x1.py import sys, getopt opts, args = getopt.getopt(sys.argv[1:],'fdmse:d',['files=','data-source=','mode=','start','end']) print "opts=", opts, "args=", args $ python x1.py -f=dummy.csv argblah Traceback (most recent call last): File "x1.py", line 2, in <module> opts, args = getopt.getopt(sys.argv[1:],'fdmse:d',['files=','data-source=','mode=','start','end']) File "/usr/lib/python2.6/getopt.py", line 91, in getopt opts, args = do_shorts(opts, args[0][1:], shortopts, args[1:]) File "/usr/lib/python2.6/getopt.py", line 191, in do_shorts if short_has_arg(opt, shortopts): File "/usr/lib/python2.6/getopt.py", line 207, in short_has_arg raise GetoptError('option -%s not recognized' % opt, opt) getopt.GetoptError: option -= not recognized $ sed 's/fdm/f:dm/' <x1.py >x2.py $ diff x1.py x2.py 2c2 < opts, args = getopt.getopt(sys.argv[1:],'fdmse:d',['files=','data-source=','mode=','start','end']) --- > opts, args = getopt.getopt(sys.argv[1:],'f:dmse:d',['files=','data-source=','mode=','start','end']) $ python x2.py -f=dummy.csv argblah opts= [('-f', '=dummy.csv')] args= ['argblah'] $ python x1.py -f dummy.csv argblah opts= [('-f', '')] args= ['dummy.csv', 'argblah'] 
+2
source

Import and use argparse instead of getopt. It is much easier to use and has almost everything you need to run from the command line built into it.

Example,

  parser = argparse.ArgumentParser( description='Description of what the module does when run.') parser.add_argument("-o", "--output", help='Path of log file.') args = parser.parse_args() 

So simple. Of course, you need to import argparse at the top of your file for this to work.

0
source

All Articles