2014-07-22 2 views
1

파이썬의 cmd 모듈을 사용하여 응용 프로그램에 대한 사용자 지정 대화식 프롬프트를 만듭니다. 현재 프롬프트에 help을 입력하면 내 맞춤 명령 목록이 자동으로 표시됩니다 (예 :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) 

도움말 명령을 실행할 때 인쇄되는 보일러 플레이트 텍스트를 툴인으로 수정하는 방법을 아는 사람이 있습니까?

답변

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