ANN: a better getopt.py

Frank Stajano fstajano at uk.research.att.com
Thu Jun 29 11:25:02 EDT 2000


This is to announce an unofficial new version of the getopt.py module that 
helps parsing command line options. I wrote it because it scratched an 
itch; if it's useful to others too, so much the better.

http://www.cl.cam.ac.uk/~fms27/getopt/

This is a backwards compatible replacement that does not take away or 
change any existing functionality. It adds a new function getoptdict() 
which does the same things as getopt() but returns the result in a 
dictionary as opposed to a list of pairs. From my humble experience, the 
caller usually likes random (not sequential) access to the supplied 
options, so the dictionary appears to be the best match.

The only problem comes up with repeated options (e.g. "-v for verbose, -v 
-v for very verbose, -v -v -v for really very verbose indeed"). This of 
course doesn't quite fit in the dictionary model, since each key must be 
unique. The getoptdict() lets you choose what to do; you can REJECT 
repeated options (default behaviour) if you abhor them like I do; but you 
can also APPEND them to a list or KEEP_EARLIEST or KEEP_LATEST.

Another minor update concerns exceptions: the lonely GetoptError has been 
turned into a small class hierarchy. This allows the caller to distinguish 
the various errors if fine-grained control is desired. At the same time it 
is still possible to intercept all errors by simply calling the single 
GetoptError which is at the root of the hierarchy.

Note that the base file was the getopt.py that came with Python 1.6 alpha 
2. Therefore this module won't work with Python < 1.6, because the base 
file uses 1.6 features such as the index() method for a string.

This modification to getopt.py is released as open source under the same 
licence as the rest of Python. I, and my employer, disclaim all warranties 
just like Stichting Mathematisch Centrum and CNRI do. Feedback and bug 
reports to fstajano at uk.research.att.com.

   <P><A HREF="http://www.cl.cam.ac.uk/~fms27/getopt/">getopt.py</A> 1.3 - 
Compatible replacement for getopt that returns a dictionary instead of a 
list of pairs.  (29-Jun-2000)


  Frank (http://i.am/filologo.disneyano/)  http://www.uk.research.att.com/~fms/





More information about the Python-list mailing list