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):
|
def _load_commands(self):
|
||||||
for ep in pkg_resources.iter_entry_points(self.namespace):
|
for ep in pkg_resources.iter_entry_points(self.namespace):
|
||||||
LOG.debug('found command %r', ep.name)
|
LOG.debug('found command %r', ep.name)
|
||||||
self.commands[ep.name] = ep
|
self.commands[ep.name.replace('_', ' ')] = ep
|
||||||
return
|
return
|
||||||
|
|
||||||
def find_command(self, argv):
|
def find_command(self, argv):
|
||||||
"""Given an argument list, find a command and
|
"""Given an argument list, find a command and
|
||||||
return the processor and any remaining arguments.
|
return the processor and any remaining arguments.
|
||||||
"""
|
"""
|
||||||
orig_args = argv[:]
|
search_args = argv[:]
|
||||||
name = ''
|
name = ''
|
||||||
while argv:
|
while search_args:
|
||||||
if argv[0].startswith('-'):
|
if search_args[0].startswith('-'):
|
||||||
raise ValueError('Invalid command %r' % argv[0])
|
raise ValueError('Invalid command %r' % search_args[0])
|
||||||
next_val = argv.pop(0)
|
next_val = search_args.pop(0)
|
||||||
name = '%s_%s' % (name, next_val) if name else next_val
|
name = '%s %s' % (name, next_val) if name else next_val
|
||||||
if name in self.commands:
|
if name in self.commands:
|
||||||
cmd_ep = self.commands[name]
|
cmd_ep = self.commands[name]
|
||||||
cmd_factory = cmd_ep.load()
|
cmd_factory = cmd_ep.load()
|
||||||
cmd = cmd_factory()
|
return (cmd_factory, name, search_args)
|
||||||
return (cmd, argv)
|
|
||||||
else:
|
else:
|
||||||
raise ValueError('Did not find command processor for %r' %
|
raise ValueError('Did not find command processor for %r' %
|
||||||
(orig_args,))
|
(argv,))
|
||||||
|
|||||||
@@ -15,15 +15,16 @@ class TestCommandManager(CommandManager):
|
|||||||
def _load_commands(self):
|
def _load_commands(self):
|
||||||
self.commands = {
|
self.commands = {
|
||||||
'one': TestCommand,
|
'one': TestCommand,
|
||||||
'two_words': TestCommand,
|
'two words': TestCommand,
|
||||||
'three_word_command': TestCommand,
|
'three word command': TestCommand,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
def test_lookup_and_find():
|
def test_lookup_and_find():
|
||||||
def check(mgr, argv):
|
def check(mgr, argv):
|
||||||
cmd, remaining = mgr.find_command(argv)
|
cmd, name, remaining = mgr.find_command(argv)
|
||||||
assert cmd
|
assert cmd
|
||||||
|
assert name == ' '.join(argv)
|
||||||
assert not remaining
|
assert not remaining
|
||||||
mgr = TestCommandManager('test')
|
mgr = TestCommandManager('test')
|
||||||
for expected in [['one'],
|
for expected in [['one'],
|
||||||
@@ -36,7 +37,7 @@ def test_lookup_and_find():
|
|||||||
|
|
||||||
def test_lookup_with_remainder():
|
def test_lookup_with_remainder():
|
||||||
def check(mgr, argv):
|
def check(mgr, argv):
|
||||||
cmd, remaining = mgr.find_command(argv)
|
cmd, name, remaining = mgr.find_command(argv)
|
||||||
assert cmd
|
assert cmd
|
||||||
assert remaining == ['--opt']
|
assert remaining == ['--opt']
|
||||||
mgr = TestCommandManager('test')
|
mgr = TestCommandManager('test')
|
||||||
|
|||||||
Reference in New Issue
Block a user