我正在使用Python的cmd
模块为应用程序创建自定义交互式提示。现在,当我在提示符下键入help
时,它会自动显示我的自定义命令列表,例如
[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)
有没有人知道一种工具输入的方法并修改在发出帮助命令时打印的样板文本?
答案 0 :(得分: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 ...)