cfg and argparse sub-commands

Laurence Miao did some great work recently to port cfg from optparse to argparse.

The only significant API impact was that we could no longer have CONF() return unparsed command line arguments.

We chose to offer two alternatives – firstly, support for positional arguments:

>>> CONF.register_cli_opt(MultiStrOpt(‘bar’, positional=True))
True >>> CONF(['a', 'b'])
>>> CONF.bar
['a', 'b']

and, secondly, integration with argparse’s sub-commands:

>>> def add_parsers(subparsers):
… list_action = subparsers.add_parser(‘list’)
… list_action.add_argument(‘id’)

>>> CONF.register_cli_opt(SubCommandOpt(‘action’, handler=add_parsers))
True
>>> CONF(['list', '10'])
>>> CONF.action.name, CONF.action.id
(‘list’, ’10′)

After porting the CLIs in Nova (nova-manage), Glance (glance-control and glance-manage) and Keystone (keystone-manage) over to this sub-parsers stuff, it looks like it’s going to work out quite nicely.

One Response to “cfg and argparse sub-commands”