Overriding the undocumented help area in cmd module in Python

I am using the cmd module for Python to create a small CLI tool. I'm not a fan of displaying undocumented commands. Therefore, when I type "help", I would just like to show the documented commands.

By typing help, you will see the following:

Documented commands (type help <topic>):
========================================
exit  help  projects

Undocumented commands:
======================
EOF

I have an EOF bit because I need to exit gracefully, as described in cmd examples. But I do not want this to be listed. If I do this, it makes no sense. How can I override and not show "undocumented commands"?

My code is:

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 '''

if name == ' main : Pt (). Cmdloop ()

+4
3

Pt undoc_header None print_topic, , None

undoc_header = None 

def print_topics(self, header, cmds, cmdlen, maxcol):                                                                                                                   
    if header is not None:                                                                                                                                              
        if cmds:                                                                                                                                                        
            self.stdout.write("%s\n"%str(header))                                                                                                                       
            if self.ruler:                                                                                                                                              
                self.stdout.write("%s\n"%str(self.ruler * len(header)))                                                                                                 
            self.columnize(cmds, maxcol-1)                                                                                                                              
            self.stdout.write("\n")    
+3
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]

.

+2

Improving answer @ user933589:

It would be somewhat better to override the method print_topics, but still call the base method defined in the class Cmdas follows:

undoc_header = None

def print_topics(self, header, cmds, cmdlen, maxcol):
    if header is not None:
        Cmd.print_topics(self, header, cmds, cmdlen, maxcol)
0
source

Source: https://habr.com/ru/post/1541195/


All Articles