Michael Pasternak has uploaded a new change for review. Change subject: cli: mismatch between restore snapshot syntax and auto completion #1027298 ......................................................................
cli: mismatch between restore snapshot syntax and auto completion #1027298 Change-Id: Ibf7bb40e8fe2f933ccbfae75a0e031235b2dc07d Signed-off-by: Michael pasternak <mpast...@redhat.com> Bug-Url: https://bugzilla.redhat.com/show_bug.cgi?id=1027298 --- M src/ovirtcli/infrastructure/historymanager.py M src/ovirtcli/shell/actioncmdshell.py M src/ovirtcli/shell/engineshell.py 3 files changed, 80 insertions(+), 4 deletions(-) git pull ssh://gerrit.ovirt.org:29418/ovirt-engine-cli refs/changes/82/22082/1 diff --git a/src/ovirtcli/infrastructure/historymanager.py b/src/ovirtcli/infrastructure/historymanager.py index fa2336a..7edb515 100644 --- a/src/ovirtcli/infrastructure/historymanager.py +++ b/src/ovirtcli/infrastructure/historymanager.py @@ -86,6 +86,10 @@ if self.length() > 0: readline.remove_history_item(entry) + def add(self, entry): + if self.enabled: + readline.add_history(entry) + def __unregister_dump_callback(self): for item in atexit._exithandlers: if hasattr(item[0], 'func_name') and \ diff --git a/src/ovirtcli/shell/actioncmdshell.py b/src/ovirtcli/shell/actioncmdshell.py index 60593f7..cf16f1e 100644 --- a/src/ovirtcli/shell/actioncmdshell.py +++ b/src/ovirtcli/shell/actioncmdshell.py @@ -20,7 +20,7 @@ from ovirtcli.utils.autocompletionhelper import AutoCompletionHelper from ovirtsdk.infrastructure import brokers from ovirtcli.utils.methodhelper import MethodHelper - +import sys class ActionCmdShell(CmdShell): NAME = 'action' @@ -31,7 +31,76 @@ self.identifier_template = '--%s-identifier' def do_action(self, args): - return self.context.execute_string(ActionCmdShell.NAME + ' ' + args + '\n') + nargs = self.__reformat_args(args) + return self.context.execute_string(ActionCmdShell.NAME + ' ' + nargs + '\n') + + def __update_history(self, nargs): + """ + replaces history entry with reformatted content + + @param nargs: new entry to add + """ + if self.context.history.length() >= 1: + self.context.history.remove(self.context.history.length() - 1) + self.context.history.add(ActionCmdShell.NAME + " " + nargs) + + def __reformat_args(self, args): + """ + 1. Replaces current line with new content formated according + to command syntax, use-case: + + * action could not be determinated unless object parents is specified + + from: + action object xxx --parent-identifier yyy action-name + to: + action object xxx action-name --parent-identifier yyy + + 2. replaces history entry with reformatted content + + @param args: original args + @return: reformatted args + """ + cursor_up = '\x1b[1A' + erase_line = '\x1b[K' + sys.stdout.write(cursor_up + erase_line) + nargs = self.__reformat_command(args) + self.print_line( + self.prompt + ActionCmdShell.NAME + " " + nargs, + no_prompt=True + ) + self.__update_history(nargs) + return nargs + + def __reformat_command(self, s): + """ + Reformats command according to the default command syntax, + use-case: + + * action name could not be determinated unless object parents is specified + + from: + action object xxx --parent-identifier yyy action-name + to: + action object xxx action-name --parent-identifier yyy + + """ + line = '' + arguments = '' + tokens = s.split(' ') + tokens_len = len(tokens) + i = 0 + while i < tokens_len: + if tokens[i].startswith('--'): + arguments = arguments + ' ' + tokens[i].strip() + i = i + 1 + if i < tokens_len and not tokens[i].startswith('--'): + arguments = arguments + ' ' + tokens[i].strip() + i = i + 1 + else: + line = line + ' ' + tokens[i].strip() + i = i + 1 + return line.strip() + ' ' + arguments.strip() def __add_resource_specific_options(self, obj, specific_options, line, key=None): obj_type = TypeHelper.getDecoratorType(TypeHelper.to_singular(obj)) diff --git a/src/ovirtcli/shell/engineshell.py b/src/ovirtcli/shell/engineshell.py index def916b..188b876 100644 --- a/src/ovirtcli/shell/engineshell.py +++ b/src/ovirtcli/shell/engineshell.py @@ -120,8 +120,11 @@ self._error(str(e)) return self.cmdloop(intro, clear=False) - def print_line(self, line): - print self.prompt + line + def print_line(self, line, no_prompt=False): + if no_prompt: + print line + else: + print self.prompt + line def emptyline(self, no_prompt=False): if no_prompt: -- To view, visit http://gerrit.ovirt.org/22082 To unsubscribe, visit http://gerrit.ovirt.org/settings Gerrit-MessageType: newchange Gerrit-Change-Id: Ibf7bb40e8fe2f933ccbfae75a0e031235b2dc07d Gerrit-PatchSet: 1 Gerrit-Project: ovirt-engine-cli Gerrit-Branch: master Gerrit-Owner: Michael Pasternak <mpast...@redhat.com> _______________________________________________ Engine-patches mailing list Engine-patches@ovirt.org http://lists.ovirt.org/mailman/listinfo/engine-patches