2016-10-12 14:49:08 |
Stephen Finucane |
description |
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? |
I'm reworking some code to use cliff. I've modified the sample app to add the following file:
$ cat demoapp/cliffdemo/api_db.py
from cliff.command import Command
class Sync(Command):
"""Sync the database up to the most recent version."""
def get_parser(self, prog_name):
parser = super(Sync, self).get_parser(prog_name)
parser.add_argument(
'--version', nargs='?', metavar='<version>', default=None,
help='Database version')
return parser
def take_action(self, parsed_args):
version = parsed_args.version
self.app.stdout.write('syncing version "%s"\n' % version)
$ git diff demoapp/setup.py
diff --git a/demoapp/setup.py b/demoapp/setup.py
index dd8695d..7562a7f 100644
--- a/demoapp/setup.py
+++ b/demoapp/setup.py
@@ -60,6 +60,7 @@ setup(
'file = cliffdemo.show:File',
'show file = cliffdemo.show:File',
'unicode = cliffdemo.encoding:Encoding',
+ 'api-db_sync = cliffdemo.api_db:Sync',
],
},
However, when I run this command it appears the '--version' parameter is parsed by the top level parser instead of being propagated down like the '--help' parser is.
$ demoapp api-db sync --help
usage: nova-manage api-db sync [-h] [--version [<version>]]
Sync the database up to the most recent version.
optional arguments:
-h, --help show this help message and exit
--version [<version>]
Database version
$ demoapp api-db sync --version 2
demoapp 0.1
This seems like a bug, to me: the '--version' parameter makes no sense in this location in a command and does not need to be reserved. |
|