Hierarchical commnd line parsing / help texts

Michele Simionato michele.simionato at gmail.com
Wed Sep 28 14:45:27 CEST 2011


plac is based on argparser and it is intended to be much easier to use. See
http://plac.googlecode.com/hg/doc/plac.html

Here is an example of usage.

$ cat vcs.py
class VCS(object):
    "A fictitious version control tool"
    commands = ['checkout', 'commit']
    def checkout(self, url):
        return 'ok'
    def commit(self):
        return 'ok'

if __name__ == '__main__':
    import plac; plac.Interpreter.call(VCS)

The line plac.Interpreter.call instantiates the VCS class by passing to it the arguments in the command line and then calls the appropriate method.

You can use the script as follows:

$ python vcs.py -h
usage: vcs.py [-h] [args [args ...]]

positional arguments:
  args

optional arguments:
  -h, --help  show this help message and exit

$ python vcs.py checkout url
ok
$ python vcs.py commit
ok
plac takes care of parsing the command line, giving the correct error message if you pass wrong arguments or not enough arguments:

$ python vcs.py checkout
usage:  checkout url
 checkout: error: too few arguments

plac can also be used to write command interpreters.



More information about the Python-list mailing list