Possible to auto-insert a prefix as a hack in cmd2?

64 Views Asked by At

I would like the default command in the repl to allow multi-lines. However, in cmd2 you have to specify the actual command that is multi-line, which defeats the purpose in the above. Here is an example I have thus far, but it requires inserting a character x at the start of each command:

import cmd2
class Repl(cmd2.Cmd):
    def __init__(self):
        super().__init__(multiline_commands=['x'])
        self.prompt = 'sql> '
        self.continuation_prompt = '  -> '
    def do_x(self, line):
        print ('**********************', line)

if __name__ == '__main__':
    Repl().cmdloop()

Is there a way in cmd2 to insert the letter x to the start of the command (or a way to actually handle this properly in the library?)

Desktop david$ python3 client.py

sql> x select 1,
  -> 2;
********************** select 1, 2

Update: this is a very hackish approach, but the following does work (until there's a better alternative) --

import cmd2, sys
class Repl(cmd2.Cmd):
    def __init__(self, prompt='sql'):
        super().__init__(multiline_commands=['default'])
        self.prompt = prompt + '> '
        self.continuation_prompt = ' ' * (len(prompt)-1) +'-> '
    def precmd(self, line):
        if line.raw == 'eof': sys.exit(0)
        return 'default ' + line.raw
    def do_default(self, line):
        print (line)

if __name__ == '__main__':
    Repl().cmdloop()

0

There are 0 best solutions below