Merge "Support multiple sub commands in completion"

This commit is contained in:
Jenkins 2016-06-15 20:21:40 +00:00 committed by Gerrit Code Review
commit 21f133f17b
2 changed files with 34 additions and 2 deletions

View File

@ -21,9 +21,21 @@ class CompleteDictionary:
optstr = ' '.join(opt for action in actions
for opt in action.option_strings)
dicto = self._dictionary
last_cmd = command[-1]
for subcmd in command[:-1]:
dicto = dicto.setdefault(subcmd, {})
dicto[command[-1]] = optstr
subdata = dicto.get(subcmd)
# If there is a string in corresponding dictionary, it means the
# verb used for the command exists already.
# For example, {'cmd': 'action'}, and we add the command
# 'cmd_other'. We want the result to be
# {'cmd': 'action other', 'cmd_other': 'sub_action'}
if isinstance(subdata, six.string_types):
subdata += ' ' + last_cmd
dicto[subcmd] = subdata
last_cmd = subcmd + '_' + last_cmd
else:
dicto = dicto.setdefault(subcmd, {})
dicto[last_cmd] = optstr
def get_commands(self):
return ' '.join(k for k in sorted(self._dictionary.keys()))

View File

@ -32,6 +32,26 @@ def test_complete_dictionary():
assert "2" == result[3][1]
def test_complete_dictionary_subcmd():
sot = complete.CompleteDictionary()
sot.add_command("image delete".split(),
[mock.Mock(option_strings=["1"])])
sot.add_command("image list".split(),
[mock.Mock(option_strings=["2"])])
sot.add_command("image list better".split(),
[mock.Mock(option_strings=["3"])])
assert "image" == sot.get_commands()
result = sot.get_data()
assert "image" == result[0][0]
assert "delete list list_better" == result[0][1]
assert "image_delete" == result[1][0]
assert "1" == result[1][1]
assert "image_list" == result[2][0]
assert "2 better" == result[2][1]
assert "image_list_better" == result[3][0]
assert "3" == result[3][1]
class FakeStdout:
def __init__(self):
self.content = []