(self, cmd)
help ?COMMAND?: Shows list of commands and their usage.  If COMMAND is specified then shows detail about that COMMAND.  ('.help all' will show detailed help about all commands.)
help ?COMMAND?: Shows list of commands and their usage.  If COMMAND is specified then shows detail about that COMMAND.  ('.help all' will show detailed help about all commands.)
help ?COMMAND? : Shows list of commands and their usage . If COMMAND is specified then shows detail about that COMMAND . ( . help all will show detailed help about all commands . )
def command_help(self, cmd):
        """help ?COMMAND?: Shows list of commands and their usage.  If COMMAND is specified then shows detail about that COMMAND.  ('.help all' will show detailed help about all commands.)
        """
        if not self._help_info:
            # buildup help database
            self._help_info = {}
            for c in dir(self):
                if not c.startswith("command_"):
                    continue
                # help is 3 parts
                # - the syntax string (eg backup ?dbname? filename)
                # - the one liner description (eg saves database to filename)
                # - the multi-liner detailed description
                # We grab this from the doc string for the function in the form
                #   syntax: one liner\nmulti\nliner
                d = getattr(self, c).__doc__
                assert d, c + " command must have documentation"
                c = c[len("command_"):]
                if c in ("headers", "color"): continue
                while d[0] == "\n":
                    d = d[1:]
                parts = d.split("\n", 1)
                firstline = parts[0].strip().split(":", 1)
                assert len(firstline) == 2, c + " command must have usage: description doc"
                if len(parts) == 1 or len(parts[1].strip()) == 0:  # work around textwrap bug
                    multi = ""
                else:
                    multi = textwrap.dedent(parts[1])
                if c == "mode":
                    if not self._output_modes:
                        self._cache_output_modes()
                    firstline[1] = firstline[1] + " " + " ".join(self._output_modes)
                    multi = multi + "\n\n" + "\n\n".join(self._output_modes_detail)
                if c == "colour":
                    colours = list(self._colours.keys())
                    colours.sort()
                    firstline[1] = firstline[1] + " from " + ", ".join(colours)
                if len(multi.strip()) == 0:  # All whitespace
                    multi = None
                else:
                    multi = multi.strip("\n")
                    # we need to keep \n\n as a newline but turn all others into spaces
                    multi = multi.replace("\n\n", "\x00")
                    multi = multi.replace("\n", " ")
                    multi = multi.replace("\x00", "\n\n")
                    multi = multi.split("\n\n")
                self._help_info[c] = ('.' + firstline[0].strip(), firstline[1].strip(), multi)

        self.write(self.stderr, "\n")

        tw = self._terminal_width()
        if tw < 32:
            tw = 32
        if len(cmd) == 0:
            commands = list(self._help_info.keys())
            commands.sort()
            w = 0
            for command in commands:
                if len(self._help_info[command][0]) > w:
                    w = len(self._help_info[command][0])
            out = []
            for command in commands:
                hi = self._help_info[command]
                # usage string
                out.append(hi[0])
                # space padding (including 2 for between columns)
                out.append(" " * (2 + w - len(hi[0])))
                # usage message wrapped if need be
                out.append(("\n" + " " * (2 + w)).join(textwrap.wrap(hi[1], tw - w - 2)))
                # newline
                out.append("\n")
            self.write(self.stderr, "".join(out))
        else:
            if cmd[0] == "all":
                cmd = list(self._help_info.keys())
                cmd.sort()
            w = 0
            for command in self._help_info:
                if len(self._help_info[command][0]) > w:
                    w = len(self._help_info[command][0])

            for command in cmd:
                if command == "headers": command = "header"
                if command not in self._help_info:
                    raise self.Error("No such command \"%s\"" % (command, ))
                out = []
                hi = self._help_info[command]
                # usage string
                out.append(hi[0])
                # space padding (2)
                out.append(" " * (2 + w - len(hi[0])))
                # usage message wrapped if need be
                out.append(("\n" + " " * (2 + w)).join(textwrap.wrap(hi[1], tw - w - 2)) + "\n")
                if hi[2]:
                    # newlines
                    out.append("\n")
                    # detailed message
                    for i, para in enumerate(hi[2]):
                        out.append(textwrap.fill(para, tw) + "\n")
                        if i < len(hi[2]) - 1:
                            out.append("\n")
                # if not first one then print separator header
                if command != cmd[0]:
                    self.write(self.stderr, "\n" + "=" * tw + "\n")
                self.write(self.stderr, "".join(out))
        self.write(self.stderr, "\n")
def command_help ( self , cmd ) : if not self . _help_info : # buildup help database self . _help_info = { } for c in dir ( self ) : if not c . startswith ( "command_" ) : continue # help is 3 parts # - the syntax string (eg backup ?dbname? filename) # - the one liner description (eg saves database to filename) # - the multi-liner detailed description # We grab this from the doc string for the function in the form #   syntax: one liner\nmulti\nliner d = getattr ( self , c ) . __doc__ assert d , c + " command must have documentation" c = c [ len ( "command_" ) : ] if c in ( "headers" , "color" ) : continue while d [ 0 ] == "\n" : d = d [ 1 : ] parts = d . split ( "\n" , 1 ) firstline = parts [ 0 ] . strip ( ) . split ( ":" , 1 ) assert len ( firstline ) == 2 , c + " command must have usage: description doc" if len ( parts ) == 1 or len ( parts [ 1 ] . strip ( ) ) == 0 : # work around textwrap bug multi = "" else : multi = textwrap . dedent ( parts [ 1 ] ) if c == "mode" : if not self . _output_modes : self . _cache_output_modes ( ) firstline [ 1 ] = firstline [ 1 ] + " " + " " . join ( self . _output_modes ) multi = multi + "\n\n" + "\n\n" . join ( self . _output_modes_detail ) if c == "colour" : colours = list ( self . _colours . keys ( ) ) colours . sort ( ) firstline [ 1 ] = firstline [ 1 ] + " from " + ", " . join ( colours ) if len ( multi . strip ( ) ) == 0 : # All whitespace multi = None else : multi = multi . strip ( "\n" ) # we need to keep \n\n as a newline but turn all others into spaces multi = multi . replace ( "\n\n" , "\x00" ) multi = multi . replace ( "\n" , " " ) multi = multi . replace ( "\x00" , "\n\n" ) multi = multi . split ( "\n\n" ) self . _help_info [ c ] = ( '.' + firstline [ 0 ] . strip ( ) , firstline [ 1 ] . strip ( ) , multi ) self . write ( self . stderr , "\n" ) tw = self . _terminal_width ( ) if tw < 32 : tw = 32 if len ( cmd ) == 0 : commands = list ( self . _help_info . keys ( ) ) commands . sort ( ) w = 0 for command in commands : if len ( self . _help_info [ command ] [ 0 ] ) > w : w = len ( self . _help_info [ command ] [ 0 ] ) out = [ ] for command in commands : hi = self . _help_info [ command ] # usage string out . append ( hi [ 0 ] ) # space padding (including 2 for between columns) out . append ( " " * ( 2 + w - len ( hi [ 0 ] ) ) ) # usage message wrapped if need be out . append ( ( "\n" + " " * ( 2 + w ) ) . join ( textwrap . wrap ( hi [ 1 ] , tw - w - 2 ) ) ) # newline out . append ( "\n" ) self . write ( self . stderr , "" . join ( out ) ) else : if cmd [ 0 ] == "all" : cmd = list ( self . _help_info . keys ( ) ) cmd . sort ( ) w = 0 for command in self . _help_info : if len ( self . _help_info [ command ] [ 0 ] ) > w : w = len ( self . _help_info [ command ] [ 0 ] ) for command in cmd : if command == "headers" : command = "header" if command not in self . _help_info : raise self . Error ( "No such command \"%s\"" % ( command , ) ) out = [ ] hi = self . _help_info [ command ] # usage string out . append ( hi [ 0 ] ) # space padding (2) out . append ( " " * ( 2 + w - len ( hi [ 0 ] ) ) ) # usage message wrapped if need be out . append ( ( "\n" + " " * ( 2 + w ) ) . join ( textwrap . wrap ( hi [ 1 ] , tw - w - 2 ) ) + "\n" ) if hi [ 2 ] : # newlines out . append ( "\n" ) # detailed message for i , para in enumerate ( hi [ 2 ] ) : out . append ( textwrap . fill ( para , tw ) + "\n" ) if i < len ( hi [ 2 ] ) - 1 : out . append ( "\n" ) # if not first one then print separator header if command != cmd [ 0 ] : self . write ( self . stderr , "\n" + "=" * tw + "\n" ) self . write ( self . stderr , "" . join ( out ) ) self . write ( self . stderr , "\n" )