From e8094528fb34003c6fe92d7d3956f5fe9d0938e7 Mon Sep 17 00:00:00 2001 From: Doug Hellmann Date: Fri, 20 Apr 2012 16:49:47 -0700 Subject: [PATCH] save commands using the name representation to be used in help output; don't modify the input arg list when searching for the command; return the name of the command found so the app can stuff it into the help text of the command --- cliff/commandmanager.py | 19 +++++++++---------- tests/test_commandmanager.py | 9 +++++---- 2 files changed, 14 insertions(+), 14 deletions(-) diff --git a/cliff/commandmanager.py b/cliff/commandmanager.py index 7f3d560..16420ea 100644 --- a/cliff/commandmanager.py +++ b/cliff/commandmanager.py @@ -21,25 +21,24 @@ class CommandManager(object): def _load_commands(self): for ep in pkg_resources.iter_entry_points(self.namespace): LOG.debug('found command %r', ep.name) - self.commands[ep.name] = ep + self.commands[ep.name.replace('_', ' ')] = ep return def find_command(self, argv): """Given an argument list, find a command and return the processor and any remaining arguments. """ - orig_args = argv[:] + search_args = argv[:] name = '' - while argv: - if argv[0].startswith('-'): - raise ValueError('Invalid command %r' % argv[0]) - next_val = argv.pop(0) - name = '%s_%s' % (name, next_val) if name else next_val + while search_args: + if search_args[0].startswith('-'): + raise ValueError('Invalid command %r' % search_args[0]) + next_val = search_args.pop(0) + name = '%s %s' % (name, next_val) if name else next_val if name in self.commands: cmd_ep = self.commands[name] cmd_factory = cmd_ep.load() - cmd = cmd_factory() - return (cmd, argv) + return (cmd_factory, name, search_args) else: raise ValueError('Did not find command processor for %r' % - (orig_args,)) + (argv,)) diff --git a/tests/test_commandmanager.py b/tests/test_commandmanager.py index 509ec89..6be244e 100644 --- a/tests/test_commandmanager.py +++ b/tests/test_commandmanager.py @@ -15,15 +15,16 @@ class TestCommandManager(CommandManager): def _load_commands(self): self.commands = { 'one': TestCommand, - 'two_words': TestCommand, - 'three_word_command': TestCommand, + 'two words': TestCommand, + 'three word command': TestCommand, } def test_lookup_and_find(): def check(mgr, argv): - cmd, remaining = mgr.find_command(argv) + cmd, name, remaining = mgr.find_command(argv) assert cmd + assert name == ' '.join(argv) assert not remaining mgr = TestCommandManager('test') for expected in [['one'], @@ -36,7 +37,7 @@ def test_lookup_and_find(): def test_lookup_with_remainder(): def check(mgr, argv): - cmd, remaining = mgr.find_command(argv) + cmd, name, remaining = mgr.find_command(argv) assert cmd assert remaining == ['--opt'] mgr = TestCommandManager('test')