我希望 repl 中的默认命令允许多行。但是,cmd2
您必须指定多行的实际命令,这违背了上述目的。这是我到目前为止的一个示例,但它需要x
在每个命令的开头插入一个字符:
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()
有没有办法cmd2
将字母插入x
命令的开头(或者有一种方法可以在库中正确处理这个问题?)
Desktop david$ python3 client.py
sql> x select 1,
-> 2;
********************** select 1, 2
更新:这是一种非常老套的方法,但以下方法确实有效(直到有更好的选择)——
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()