optparse alternative

Henry Ludemann lists at hl.id.au
Mon Mar 14 13:29:03 CET 2005


I've been writing an optparse alternative (using getopt) that is at a
stage where I'd be interested in people's opinions. It allows you to
easily creating command line interfaces to existing functions, using
flags (which are optional) and arguments. It will automatically print a
nicely formatted usage (eg: -h or --help), and easily & automatically
validates parameter existence and type.

You can download it, and read a bit more about it, at
<http://hl.id.au/Projects/CommandLine/>

I'm interested in comments and criticisms; I've tried to write it
according to the python coding guildlines.

Example usage of a cmdline usage;

    $ python test.py --help
    Usage: test.py [OPTIONS] SOMEARG
    An example to show usage of command line

    Arguments
        SOMEARG               Some float argument

    Mandatory arguments to long flags are mandatory for short options
too
        -h, --help            Show this help page
        -s, --setflag         Set the flag

    Email bug reports to bugs at example.com

Source for the above example;

    def someFunc(someArg, someFlag = False):
        print "Flag value =", someFlag
        print "Arg value =", someArg

    from cmdline.Command import Command
    command = Command(someFunc, "An example to show usage of command
line", "Email bug reports to bugs at example.com")
    command.addFlag('s', 'setflag', 'Set the flag', 'someFlag')
    command.addArgument('SOMEARG', 'Some float argument', 'someArg',
float)
    command.invoke()

Normal use of test.py would have given

    $ python test.py 3
    Flag value =  False
    Arg value = 3.0




More information about the Python-list mailing list