Merge "Fix command order"

This commit is contained in:
Jenkins
2017-01-03 19:00:05 +00:00
committed by Gerrit Code Review
2 changed files with 28 additions and 8 deletions

View File

@@ -73,14 +73,10 @@ class CommandManager(object):
"""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.
""" """
search_args = argv[:] start = self._get_last_possible_command_index(argv)
name = '' for i in range(start, 0, -1):
while search_args: name = ' '.join(argv[:i])
if search_args[0].startswith('-'): search_args = argv[i:]
name = '%s %s' % (name, search_args[0])
raise ValueError('Invalid command %r' % name)
next_val = search_args.pop(0)
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]
if hasattr(cmd_ep, 'resolve'): if hasattr(cmd_ep, 'resolve'):
@@ -97,3 +93,12 @@ class CommandManager(object):
else: else:
raise ValueError('Unknown command %r' % raise ValueError('Unknown command %r' %
(argv,)) (argv,))
def _get_last_possible_command_index(self, argv):
"""Returns the index after the last argument
in argv that can be a command word
"""
for i, arg in enumerate(argv):
if arg.startswith('-'):
return i
return len(argv)

View File

@@ -81,6 +81,21 @@ def test_add_command():
assert found_cmd is mock_cmd assert found_cmd is mock_cmd
def test_intersected_commands():
def foo(arg):
pass
def foo_bar():
pass
mgr = utils.TestCommandManager(utils.TEST_NAMESPACE)
mgr.add_command('foo', foo)
mgr.add_command('foo bar', foo_bar)
assert mgr.find_command(['foo', 'bar'])[0] is foo_bar
assert mgr.find_command(['foo', 'arg0'])[0] is foo
def test_load_commands(): def test_load_commands():
testcmd = mock.Mock(name='testcmd') testcmd = mock.Mock(name='testcmd')
testcmd.name.replace.return_value = 'test' testcmd.name.replace.return_value = 'test'