[getopt-sig] my vote is for automatic usage message generation

Doug Hellmann doug@hellfly.net
Wed, 13 Feb 2002 23:31:59 -0500


On Wednesday 13 February 2002 20:03, Piers Lauder wrote:
> While I agree with Russ Cox that the declaration of arguments should
> (and can) be simple, and, as a programmer, I really appreciate the clean
> interface he demonstrates, my overriding need is for automatic generation
> of usage messages (the response to --help).

You might be interested in my CommandLineApp class.  If you create a new 
application by subclassing from it, you can then define a main method and 
option handlers for the options you want to provide.  The base class does all 
the boring work for you.

It automatically builds the arguments to getopt, calls the parser, and 
dispatches your handlers when options are encountered on the command line.  
And the other nice thing it does is provide -h and --help handling 
automatically based on docstrings provided with the handler methods.  There 
are a couple of class attributes that can be used to provide descriptions of 
the arguments (non-option parameters on the command line), as well as 
examples of how to use the program.

http://www.hellfly.net/PythonProjects/CommandLineApp

A quick example:

class MyApp(CommandLineApp):

    def optionHandler_a(self):
        "Short option, no argument."
        self.a = 1
    
    def optionHandler_b(self, withArgument):
        "Short option, with argument."
        self.b = withArgument

    def optionHandler_long_name(self):
        "Translates to --long-name"
        self.long = 1

    def optionHandler_long_with_arg(self, argumentValue):
        "Translates to --long-with-arg=argumentValue"
        self.long_arg = argumentValue

    def main(self, *args):
        for filename in args:
           print filename

This class would support -h, --help, -a, -b <val>, --long-name, and 
--long-with-arg=<val>.  Any extra values on the command line after the 
options would be passed as a vector to the main() method.

One of the reasons I joined the discussion list was to see what might be 
coming up as a getopt replacement, so I can incorporate support in this 
class.  I'm not sure if that's going to be necessary, but it could prove 
interesting.

Doug