Mailing List Archive

Looking for for some ideas for argument processing
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?

Regards,
Chuck


Sent via Deja.com http://www.deja.com/
Share what you know. Learn what you don't.
Looking for for some ideas for argument processing [ In reply to ]
cmedcoff@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.

I usually just use Python's getopt or DPyGetOpt.

The first is in the standard library.

The second is modeled after Perl's GetOpt::Long, which is very
flexible and supports many options. DPyGetOpt is available at
python.org:
http://www.python.org/ftp/python/contrib/All/DPyGetOpt.py
http://www.python.org/ftp/python/contrib/All/DPyGetOpt.README
Looking for for some ideas for argument processing [ In reply to ]
Chuck <cmedcoff@my-deja.com> writes:

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

Try getopts from the standard distribution. If that doesn't fit your
needs, there's at least one enhanced version in the contrib section
(follow the links from python.org).

- Gordon
Looking for for some ideas for argument processing [ In reply to ]
[Chuck <medcoff@my-deja.com>]
> ...
> 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?

Some problems are better approached from a Fortran frame of mind <wink>:

# process arguments
i, n = 1, len(sys.argv)
while i < n:
x = sys.argv[1:]
opt = x[1:2]
# etc; each branch needs to bump i appropriately

Or you could build your own class to fake a multi-increment (or even random
access) iterator: you'd have a lot of fun, but nobody else would understand
it.

as-much-like-c++-as-you-want<wink>-ly y'rs - tim
Looking for for some ideas for argument processing [ In reply to ]
"Thomas A. Bryan" <tbryan@arlut.utexas.edu> writes:

> cmedcoff@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.
>
> I usually just use Python's getopt or DPyGetOpt.
>
> The first is in the standard library.
>
> The second is modeled after Perl's GetOpt::Long, which is very
> flexible and supports many options.

Hey! I've been meaning to write such a thing -- glad it's already
been written.

The chief problem with Python's getopt is that it doesn't support some
nice features of GNU getopt, such as naming the options after the
arguments (e.g. wget URL -o outfile).
Looking for for some ideas for argument processing [ In reply to ]
I didn't even know that was there. Still a newbie I guess.

I also just realized I can simulate a C-style for loop using a while
loop in Python. Silly me..

Regards,
Chuck


Sent via Deja.com http://www.deja.com/
Share what you know. Learn what you don't.
Looking for for some ideas for argument processing [ In reply to ]
Several people have suggested getopt as a solution, and if you think it
will help, you might want to check out a class library I've written to
make it easier for me to use getopt. Basically, you create a class to
define your application. By defining methods of the form:

def optionHandler_<option_name>(self[, <arg_name>]):
"""
<help message for option_name>
"""
<process arg_name>

You can automatically specify which arguments your application calls,
and quickly identify which code processes which argument. Your script
will also have automatic -h and --help arguments which produce usage
messages (short and long, respectively). The optionHandlers are called
in order based on how the arguments come in on the command line, so if
your app takes "commands" as arguments, you option handlers can assume
that state is maintained. Your options can also take multiple
arguments, as any strings with commas will be split into multiple args
before being passed into the option handler.

Check out CommandLineApp.py in:

http://www.mindspring.com/~doughellmann/Projects/pybox/pybox.r1_3.tar.gz

Let me know if you find it useful,
Doug


cmedcoff@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?
>
> Regards,
> Chuck
>
> Sent via Deja.com http://www.deja.com/
> Share what you know. Learn what you don't.
Looking for for some ideas for argument processing [ In reply to ]
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@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@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
----------------------------------------------