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’])
[‘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))
>>> CONF([‘list’, ’10’])
(‘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 thought on “cfg and argparse sub-commands”

Comments are closed.

Leave a Reply

Your email address will not be published. Required fields are marked *