我正在使用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)
有谁知道在发布帮助命令时可以插入和修改打印出来的样板文本的方法吗?
如何使用doc_header
属性:
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 ...)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句