覆盖Python的cmd模块中未记录的帮助区域 [英] Override undocumented help area in Python's cmd module
问题描述
我正在使用Python的cmd模块来构建一个小的CLI工具。我不喜欢显示未列出的命令。因此,当我键入'help'时,我只想显示文档中的命令。
I am using Python's cmd module to build a little CLI tool. I am not a fan of showing the undocumented commands listed. So when I type 'help' I would like to just show the documented commands.
当前键入help会显示以下内容:
Currently typing help shows this:
Documented commands (type help <topic>):
========================================
exit help projects
Undocumented commands:
======================
EOF
我有那个EOF位在那里,因为我需要正常退出,如cmd示例所示。但我不想列出它。如果我记录了它-没有任何意义。我该如何覆盖而不显示未记录的命令?
I have that EOF bit in there because I need to exit gracefully, as is documented by the cmd examples. But I don't want it listed. If I do document it - it makes no sense. How can I override and not show 'undocumented commands'?
我的代码:
from cmd import Cmd
from ptcli import Ptcli
from termcolor import colored
class Pt(Cmd):
Cmd.intro = colored("Welcome to pt CLI","yellow")
Cmd.prompt = colored(">> ","cyan")
def do_projects(self,line):
'Choose current project from a list'
pt = Ptcli()
result = pt.get_projects()
for i in result:
print i['name']
def do_exit(self,line):
'Exit pt cli'
return True
def do_EOF(self, line):
return True
def default(self, arg):
''' Print a command not recognized error message '''
如果 name ==' main ':
Pt()。cmdloop()
if name == 'main': Pt().cmdloop()
推荐答案
class Pt(Cmd):
__hiden_methods = ('do_EOF',)
def do_EOF(self, arg):
return True
def get_names(self):
return [n for n in dir(self.__class__) if n not in self.__hiden_methods]
这也将使该方法无法完成。
That will hide the method from the completion also.
这篇关于覆盖Python的cmd模块中未记录的帮助区域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!