diff --git a/client/solo/commands/command.py b/client/solo/commands/command.py index 0f9796874..539844961 100644 --- a/client/solo/commands/command.py +++ b/client/solo/commands/command.py @@ -10,10 +10,11 @@ """ import os +import sys import argparse from entropy.i18n import _ -from entropy.output import darkgreen, print_error +from entropy.output import darkgreen, teal, purple, print_error from entropy.exceptions import PermissionDenied from entropy.client.interfaces import Client from entropy.core.settings.base import SystemSettings @@ -100,6 +101,44 @@ class SoloCommand(object): """ raise NotImplementedError() + def _hierarchical_bashcomp(self, last_arg, outcome, commands): + """ + This method implements bash completion through + a hierarchical (commands) dictionary object. + """ + parser = self._get_parser() + + # navigate through commands, finding the list of commands + + if not self._args: + # show all the commands + outcome += sorted(commands.keys()) + + for index, item in enumerate(self._args): + if item in commands: + commands = commands[item] + if index == (len(self._args) - 1): + # if this is the last one, generate + # proper outcome elements. + outcome += sorted(commands.keys()) + # reset last_arg so that outcome list + # won't be filtered + last_arg = "" + elif index == (len(self._args) - 1): + # if this is the last one, and item + # is not in commands, outcome becomes + # commands.keys() + outcome += sorted(commands.keys()) + # no need to break here + else: + # item not in commands, but that's not the + # last one, we must generate proper outcome + # elements and stop right after + outcome += sorted(commands.keys()) + break + + return self._bashcomp(sys.stdout, last_arg, outcome) + def _bashcomp(self, stdout, last_arg, available_args): """ This method must be called from inside bashcomp() and diff --git a/client/solo/commands/pkg.py b/client/solo/commands/pkg.py index 24ba4ef4d..7e3296f6d 100644 --- a/client/solo/commands/pkg.py +++ b/client/solo/commands/pkg.py @@ -174,40 +174,7 @@ Execute advanced tasks on Entropy packages and the running system. """ Overridden from SoloCommand. """ - outcome = [] - parser = self._get_parser() - - # navigate through commands, finding the list of commands - - if not self._args: - # show all the commands - outcome += sorted(self._commands.keys()) - - commands = self._commands - for index, item in enumerate(self._args): - if item in commands: - commands = self._commands[item] - if index == (len(self._args) - 1): - # if this is the last one, generate - # proper outcome elements. - outcome += sorted(commands.keys()) - # reset last_arg so that outcome list - # won't be filtered - last_arg = "" - elif index == (len(self._args) - 1): - # if this is the last one, and item - # is not in commands, outcome becomes - # commands.keys() - outcome += sorted(commands.keys()) - # no need to break here - else: - # item not in commands, but that's not the - # last one, we must generate proper outcome - # elements and stop right after - outcome += sorted(commands.keys()) - break - - return self._bashcomp(sys.stdout, last_arg, outcome) + return self._hierarchical_bashcomp(last_arg, [], self._commands) def _scan_packages(self, entropy_client, packages, installed=False): """ diff --git a/client/solo/commands/query.py b/client/solo/commands/query.py index fe95356fb..3cc58d6d0 100644 --- a/client/solo/commands/query.py +++ b/client/solo/commands/query.py @@ -341,39 +341,8 @@ Repository query tools. Overridden from SoloCommand. """ outcome = ["--quiet", "--verbose"] - parser = self._get_parser() - - # navigate through commands, finding the list of commands - - if not self._args: - # show all the commands - outcome += sorted(self._commands.keys()) - - commands = self._commands - for index, item in enumerate(self._args): - if item in commands: - commands = self._commands[item] - if index == (len(self._args) - 1): - # if this is the last one, generate - # proper outcome elements. - outcome += sorted(commands.keys()) - # reset last_arg so that outcome list - # won't be filtered - last_arg = "" - elif index == (len(self._args) - 1): - # if this is the last one, and item - # is not in commands, outcome becomes - # commands.keys() - outcome += sorted(commands.keys()) - # no need to break here - else: - # item not in commands, but that's not the - # last one, we must generate proper outcome - # elements and stop right after - outcome += sorted(commands.keys()) - break - - return self._bashcomp(sys.stdout, last_arg, outcome) + return self._hierarchical_bashcomp( + last_arg, outcome, self._commands) def _belongs(self, entropy_client): """ diff --git a/client/solo/commands/rescue.py b/client/solo/commands/rescue.py index 66e523c20..8ab118843 100644 --- a/client/solo/commands/rescue.py +++ b/client/solo/commands/rescue.py @@ -155,40 +155,7 @@ Tools to rescue the running system. """ Overridden from SoloCommand. """ - outcome = [] - parser = self._get_parser() - - # navigate through commands, finding the list of commands - - if not self._args: - # show all the commands - outcome += sorted(self._commands.keys()) - - commands = self._commands - for index, item in enumerate(self._args): - if item in commands: - commands = self._commands[item] - if index == (len(self._args) - 1): - # if this is the last one, generate - # proper outcome elements. - outcome += sorted(commands.keys()) - # reset last_arg so that outcome list - # won't be filtered - last_arg = "" - elif index == (len(self._args) - 1): - # if this is the last one, and item - # is not in commands, outcome becomes - # commands.keys() - outcome += sorted(commands.keys()) - # no need to break here - else: - # item not in commands, but that's not the - # last one, we must generate proper outcome - # elements and stop right after - outcome += sorted(commands.keys()) - break - - return self._bashcomp(sys.stdout, last_arg, outcome) + return self._hierarchical_bashcomp(last_arg, [], self._commands) def _check_repository(self, entropy_client, repo): """ diff --git a/client/solo/commands/ugc.py b/client/solo/commands/ugc.py index 567c80bde..7e1b1d03c 100644 --- a/client/solo/commands/ugc.py +++ b/client/solo/commands/ugc.py @@ -178,40 +178,7 @@ Manage User Generate Content (votes, comments, files). """ Overridden from SoloCommand. """ - outcome = [] - parser = self._get_parser() - - # navigate through commands, finding the list of commands - - if not self._args: - # show all the commands - outcome += sorted(self._commands.keys()) - - commands = self._commands - for index, item in enumerate(self._args): - if item in commands: - commands = self._commands[item] - if index == (len(self._args) - 1): - # if this is the last one, generate - # proper outcome elements. - outcome += sorted(commands.keys()) - # reset last_arg so that outcome list - # won't be filtered - last_arg = "" - elif index == (len(self._args) - 1): - # if this is the last one, and item - # is not in commands, outcome becomes - # commands.keys() - outcome += sorted(commands.keys()) - # no need to break here - else: - # item not in commands, but that's not the - # last one, we must generate proper outcome - # elements and stop right after - outcome += sorted(commands.keys()) - break - - return self._bashcomp(sys.stdout, last_arg, outcome) + return self._hierarchical_bashcomp(last_arg, [], self._commands) def _login(self, entropy_client): """