2014-07-22 52 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