[getopt-sig] Parse arguments according to a usage message?

Ben Wolfson wolfson@midway.uchicago.edu
Sat, 23 Feb 2002 16:20:56 -0600 (CST)


On Tue, 19 Feb 2002, Greg Ward wrote:

[cmdsyntax.py]

>There are also some fairly obvious limitations:
>
>  * what about scripts with dozens of options?  the traditional
>    "[-v] [-q] [-o outfile]" notation is great for 3 or 4 options,
>    but loses its appeal pretty quickly.  (Try "man mpg123" for an
>    example of what I mean.)
>
>  * no strong typing -- everything's a string, and I have to explicitly
>    convert numeric option arguments and catch the errors
>
>  * how much say do I have in what happens to option arguments?  eg.
>    is there an equivalent to Optik's "append" action, where each
>    occurence of a particular option appends that occurence's argument
>    to a list, rather than replacing previous values?

It seems to me that you could get around these objections by adapting some
of Optik's ideas.

Instead instantiating a parser with a vanilla usage string, first
instantiate a bunch of instances of an option class, and then pass them,
along with plain strings, to the parser.  Something like:

outfile = Option('-o', '--output', type='string', action="store")
verbose = Option('-v')
s = Syntax("[-v] infile [-o outfile]", outfile, verbose)

The Syntax class would use the instances to find out what the options are
called, and match them up with the syntax string passed in.  For that
matter, for an uncomplicated command-line syntax, Syntax could infer
what the string must be from the Option instances passed in; for mpg123
(assuming the Options have already been instantiated) it could just be

synt = Syntax(None, t,s,c,v,q,y .... )

Or something like that.  That wouldn't notate that mpg123's -o option can
only have three values ([ -o s | -o h | -o l ])  but it would be easy (I
assume) to subclass Optik's Option class to indicate that a given option
can only take certain options, and then communicate that to the Syntax
class.

For not-too-complicated interfaces, though, you would have to type out the
entire interface string, and when it's not too complicated, it's not a
whole lot different from Optik anyway.  But I still think there's potential
here.

-- 
BTR    
Back in the day, the women would talk about Michelangelo.
 -- CRGRE