Hierarchical commnd line parsing / help texts

G

Gelonida N

Hi,

So far I used optparse.OptionParser for parsing command line arguments
for my python scripts. So far I was happy, with a one level approach,
where I get only one help text

Now I'd like to create a slightly different python script and wondered
which approach / module might be best for implementing the parsing and
generation of help texts.

Please look at my example (let's emulate a shell in python)

$ ./sh.py
or
$ ./sh.py -h
Should create the top level help text and list possible commands
..e.g.
ls
rm
cat
and list some commands with potential sub commands
..e.g.
net
db


then if I typed ./sh.py <command> -h
I'd like to get the helptext for the specified command.

For some more complex commands I'd even like to be able to type


/sh.py <command> <subcommand> -h


Ideally, I would like to be able to parse generic options before
commands / sub commands and less generic options after the commands


../sh.py -v ls -a


Thanks in advance for suggestions
 
M

Michele Simionato

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.
 
M

Michele Simionato

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top