Interpreter-like help in cmd.Cmd

Bengt Richter bokr at oz.net
Sun Jun 26 14:57:59 EDT 2005


On Thu, 09 Jun 2005 16:53:17 -0700, Sarir Khamsi <sarir.khamsi at raytheon.com> wrote:

>Peter Hansen <peter at engcorp.com> writes:
>
>> class _Helper(object):
>>      """Define the built-in 'help'.
>>      This is a wrapper around pydoc.help (with a twist).
>>
>>      """
>>
>>      def __repr__(self):
>>          return "Type help() for interactive help, " \
>>                 "or help(object) for help about object."
>>      def __call__(self, *args, **kwds):
>>          import pydoc
>>          return pydoc.help(*args, **kwds)
>
>Thanks, but how do I integrate this with cmd.Cmd?

Have you read the docs for the cmd module? E.g.,
    http://www.python.org/doc/current/lib/Cmd-objects.html

"""
All subclasses of Cmd inherit a predefined do_help().
This method, called with an argument 'bar', invokes
the corresponding method help_bar().

With no argument, do_help() lists all available help topics
(that is, all commands with corresponding help_*() methods),
and also lists any undocumented commands.
"""

This suggests that typing "help xxx" might call do_help('xxx')
and that will call help_xxx if it exists. So if you want help
on a non-cmd.Cmd command, you might want to modify do_help to
call pydoc as above instead of generating its default
have-no-help repsonse, whatever that is.

Just guessing, but that should get you closer.

You can look at the source in <wherever>python<versionstuff>\Lib\cmd.py
E.g.,
    D:\Python23\Lib\cmd.py
or
    D:\Python-2.4b1\Lib\cmd.py
for me.


Look in cmd.py at class Cmd method do_help:

    def do_help(self, arg):
        if arg:
            # XXX check arg syntax
            try:
                func = getattr(self, 'help_' + arg)
            except AttributeError:
                try:
                    doc=getattr(self, 'do_' + arg).__doc__
                    if doc:
                        self.stdout.write("%s\n"%str(doc))
                        return
                except AttributeError:
                    pass
[1] -->         self.stdout.write("%s\n"%str(self.nohelp % (arg,)))
                return
            func()
        else:
            # ... generates topic listing

Probably you can write your own Cmd subclass and override do_help
and modify at [1] to do the pydoc call as in Peter's snippet.
Hopefully no weird interactions, but it should be easy to try ;-)

HTH

Regards,
Bengt Richter



More information about the Python-list mailing list