From df5f06f6951b89ae2e223274496ef87d796162fa Mon Sep 17 00:00:00 2001 From: Michael Becker Date: Tue, 2 Apr 2024 07:29:24 -0400 Subject: [PATCH] provide automagic command line splitting with quote support --- lib/mbs/framework/REPLApplication.py | 81 +++++++++++++++++++++++++--- 1 file changed, 75 insertions(+), 6 deletions(-) diff --git a/lib/mbs/framework/REPLApplication.py b/lib/mbs/framework/REPLApplication.py index 3c22e5e..e22d7f4 100644 --- a/lib/mbs/framework/REPLApplication.py +++ b/lib/mbs/framework/REPLApplication.py @@ -2,29 +2,98 @@ from .ConsoleApplication import ConsoleApplication class REPLApplication (ConsoleApplication): + def split_with_quotes(self, value : str): + + escaping = False + in_quote = 0 + next = "" + retval = [ ] + quote_types = [ ] + + for i in range(0, len(value)): + if value[i] == "\"" and not escaping: + if in_quote == 0: + in_quote = 1 + continue + elif in_quote == 1: + in_quote = 0 + + quote_types.append(1) + retval.append(next) + next = "" + continue + + elif value[i] == "'" and not escaping: + if in_quote == 0: + in_quote = 2 + continue + elif in_quote == 2: + in_quote = 0 + + quote_types.append(2) + retval.append(next) + next = "" + continue + + elif value[i] == "`" and not escaping: + if in_quote == 0: + in_quote = 3 + continue + elif in_quote == 3: + in_quote = 0 + + quote_types.append(3) + retval.append(next) + next = "" + continue + + elif value[i] == "\\" and not escaping: + escaping = True + continue + + elif value[i] == " " and in_quote == 0: + if not next == "": + quote_types.append(None) + retval.append(next) + + next = "" + escaping = False + continue + + next += value[i] + escaping = False + + if not next == "": + quote_types.append(None) + retval.append(next) + next = "" + + return (retval, quote_types) + def _start_internal(self): while (True): # print ("open instance attribute relationship close") - inp = input(self.get_prompt()) + input_str = input(self.get_prompt()) + (inp, quotes) = self.split_with_quotes(input_str) - if inp == "clear": + if inp[0] == "clear": self.clear() continue - elif inp == "exit": + elif inp[0] == "exit": break - elif inp == "help": + elif inp[0] == "help": continue - self.process_input(inp) + self.process_input(inp, quotes) exit(0) def get_prompt(self): return "> " - def process_input(self, value): + def process_input(self, value, quotes): pass \ No newline at end of file