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
This commit is contained in:
@@ -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,))
|
||||
|
||||
@@ -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')
|
||||
|
||||
Reference in New Issue
Block a user