2014-07-22 92 views
1

我使用Python的cmd模塊爲應用程序創建自定義交互式提示。現在,當我在提示符下鍵入help時,它會自動顯示我的自定義命令列表,例如增強Python cmd模塊「幫助」打印輸出

[myPromt] help 

Documented commands (type help <topic>): 
======================================== 
cmd1 cmd2 cmd3 

我想用一些文字說明可以在提示符下使用的鍵盤快捷鍵。

[myPromt] help 

Documented commands (type help <topic>): 
======================================== 
cmd1 cmd2 cmd3 

(use Ctrl+l to clear screen, Ctrl+a to move cursor to line start, Ctrl+e to move cursor to line end) 

有沒有人知道一種方法來工具和修改發佈幫助命令時打印的鍋爐板文本?

回答

1

如何使用doc_header attribute

import cmd 

class MyCmd(cmd.Cmd): 
    def do_cmd1(self): pass 
    def do_cmd2(self): pass 
    def do_cmd3(self): pass 

d = MyCmd() 
d.doc_header = '(use Ctrl+l to clear screen, Ctrl+a ...)' # <--- 
d.cmdloop() 

輸出示例:

(Cmd) ? 

(use Ctrl+l to clear screen, Ctrl+a ...) 
======================================== 
help 

Undocumented commands: 
====================== 
cmd1 cmd2 cmd3 

如果你需要把自定義消息的正常幫助信息後,使用do_help

import cmd 

class MyCmd(cmd.Cmd): 
    def do_cmd1(self): pass 
    def do_cmd2(self): pass 
    def do_cmd3(self): pass 
    def do_help(self, *args): 
     cmd.Cmd.do_help(self, *args) 
     print 'use Ctrl+l to clear screen, Ctrl+a ...)' 

d = MyCmd() 
d.cmdloop() 

輸出:

(Cmd) ? 

Undocumented commands: 
====================== 
cmd1 cmd2 cmd3 help 

use Ctrl+l to clear screen, Ctrl+a ...) 
+0

完美。我使用了do_help()覆蓋,它的功能就像一個魅力,謝謝! – jeremiahbuddha