Looking for for some ideas for argument processing

Ivan Van Laningham ivanlan at callware.com
Wed Jun 9 10:26:12 EDT 1999


Hi All--

(I've been off email for a week, just getting it back today, so forgive
me for posting replies to letters that might be out of date. ...)

Doug Hellmann wrote:
> 
> cmedcoff at my-deja.com wrote:
> >
> > I'm writing a script with is going to make heavy use of script
> > arguments.  I'd like to hear some ideas on some ways to support this.
> >
> > One idea I've had so far is to write a class which is a dictionary
> > of "directives" to function objects, where a "directive" might be '-
> > c', '/f', or whatever.  This seems to work well if all arguments are of
> > this simple form.  However I would also like to support arguments
> > like "python myscript.py /a /f /x "bip" "bop", /g"; the point here
> > being that "directives" may take arguments.  This kind of messes up the
> > simple loop you'd otherwise use to process arguments.
> >
> >         a = BuildArgumentProcessor()
> >
> >         # process arguments
> >         for x in sys.argv[1:]:
> >             opt = x[1:2]
> >             optArg = x[2:]
> >             a.Process(opt, optArg)
> >
> > This would work if somehow inside the for loop one could "increment
> > this list iterator more that once" (sorry I'm coming from a C++
> > background).  I don't see anyway of doing this.  Is it possible?  Any
> > other ideas?
> >

Check out
	
	ftp://www.pauahtun.org/pub/getargspy.zip

I think you may find it gets pretty close to what you want.  Arguments
provided on the command-line can be ints, strings, lists, tuples; IIRC
it can do dictionaries, too.  Documentation is sketchy, so you'll have
to read the code & look at the docstrings, but if it can't do what you
want I may have time to fix it so it will.  Some of these days, in my
copious free time, I will HTML-ize the documentation;-)  The short
version is that you make a dictionary out of the command-line args, and
by the time you instantiate the dictionary describing the args, most
processing has been done.  I.e., if one of the args is a filename ("-f
foofile"), when you access the "f" key in the dict, the value of it is
the opened file.  And so on.

<yes-you-can-change-the-leadin-char-from-dash-to-/-or-whatever>-ly y'rs,
Ivan

----------------------------------------------
Ivan Van Laningham
Callware Technologies, Inc.
ivanlan at callware.com
http://www.pauahtun.org
See also: 
http://www.foretec.com/python/workshops/1998-11/proceedings.html
Army Signal Corps:  Cu Chi, Class of '70
----------------------------------------------




More information about the Python-list mailing list