Looking for for some ideas for argument processing

Doug Hellmann doughellmann at mindspring.com
Mon Jun 7 09:48:54 EDT 1999


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 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?
> 
> Regards,
> Chuck
> 
> Sent via Deja.com http://www.deja.com/
> Share what you know. Learn what you don't.




More information about the Python-list mailing list