Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Override undocumented help area in Python's cmd module

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.

Currently typing help shows this:

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

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

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

My code:

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()

like image 797
Mreider Avatar asked Mar 20 '23 13:03

Mreider


2 Answers

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.

like image 62
SMich Avatar answered Mar 23 '23 09:03

SMich


You can use the hack below

In Pt class, set undoc_header to None and override print_topic method not to print section if header is 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")    
like image 45
3r1d Avatar answered Mar 23 '23 09:03

3r1d