Comment 0 for bug 1619708

Revision history for this message
Stephen Finucane (stephenfinucane) wrote : "reserved" options don't work with subcommands

I'm reworking some code to use cliff. I've defined a custom Command:

    class ApiDbCommands(Command):
        """Class for managing the api database."""

        def get_parser(self, prog_name):
            parser = super(ApiDbCommands, self).get_parser(prog_name)

            subparsers = parser.add_subparsers(help='TODO')

            parser_a = subparsers.add_parser(
                'sync',
                help='Sync the database to the most recent version.')
            parser_a.add_argument(
                '--version', nargs='?', metavar='<version>', default=None,
                help='Database version')
            parser_a.set_defaults(cmd='sync')

            return parser

        def sync(self, version=None):
            print('syncing v%s' % version)

        def take_action(self, parsed_args):
            cmd = getattr(parsed_args, 'cmd', None)

            if cmd == 'sync':
                self.sync(parsed_args.version)

This is registered in the app, per the example in the docs:

    COMMANDS = {
        'api_db': ApiDbCommands}

    class ManageApp(App):
        def __init__(self):
            command = CommandManager('test.manage')
            super(ManageApp, self).__init__(
                description='test management',
                version='1.0',
                command_manager=command,
            )
            for k, v in COMMANDS.iteritems():
                command.add_command(k, v)

    def main():
        app = ManageApp()
        return app.run(sys.argv[1:])

When I call the 'sync' command defined above, I'd expect the '--version' parameter to be handled by that parser. However, it seems the root parser handles this instead:

    $ ./manage.py api_db sync --version 11
    manage.py 1.0

The '--help' parameter functions similarly:

    $ ./manage.py api_db --help
    usage: manage.py [--version] [-v | -q] [--log-file LOG_FILE] [-h] [--debug]

    test management

    optional arguments:
      --version show program's version number and exit
    ...

Have I done something wrong, or is this a bug on cliff's behalf?