木蘭編譯器技術驗證——用 cmd 模塊實現最簡單交互控制檯

木蘭逆向工程中用了 cmd 模塊實現控制檯,而非像前文那樣用 Interpreter 模塊。猜測是因為前者的可定製性更強,走著看。

作為技術驗證的第一步,同樣採用 cmd 模塊,完成一個最簡單控制檯:打開後顯示提示,quit 退出,僅此而已。

<code>import sys, cmd

class 木蘭(cmd.Cmd):
intro = "Welcome to ulang's REPL..\\nType 'help' for more informations."
prompt = '> '

def do_quit(self, arg):
sys.exit()

if __name__ == '__main__':
木蘭().cmdloop()
/<code>

運行效果:

<code>$ python3 交互環境.py 
Welcome to ulang's REPL..
Type 'help' for more informations.
> quit
$
/<code>

參考: cmd — Support for line-oriented command interpreters¶


分享到:


相關文章: