Interpreter-like help in cmd.Cmd

Discussion in 'Python' started by Sarir Khamsi, Jun 9, 2005.

  1. Sarir Khamsi

    Sarir Khamsi Guest

    Is there a way to get help the way you get it from the Python
    interpreter (eg, 'help(dir)' gives help on the 'dir' command) in the
    module cmd.Cmd? I know how to add commands and help text to cmd.Cmd
    but I would also like to get the man-page-like help for classes and
    functions. Does anyone know how to do that? Thanks.

    Sarir
     
    Sarir Khamsi, Jun 9, 2005
    #1
    1. Advertising

  2. Sarir Khamsi

    Peter Hansen Guest

    Sarir Khamsi wrote:
    > Is there a way to get help the way you get it from the Python
    > interpreter (eg, 'help(dir)' gives help on the 'dir' command) in the
    > module cmd.Cmd? I know how to add commands and help text to cmd.Cmd
    > but I would also like to get the man-page-like help for classes and
    > functions. Does anyone know how to do that? Thanks.


    >>> dir(help)

    ['__call__', '__class__', '__delattr__', '__dict__', ...

    [Hmm... unexpected result... let's see what it is.]

    >>> help.__class__

    <class 'site._Helper'>

    [ah... loading site.py and looking for "_Helper"...]


    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)


    HTH :)

    -Peter
     
    Peter Hansen, Jun 9, 2005
    #2
    1. Advertising

  3. Sarir Khamsi

    Sarir Khamsi Guest

    Peter Hansen <> 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?
     
    Sarir Khamsi, Jun 10, 2005
    #3
  4. Sarir Khamsi

    Peter Hansen Guest

    Sarir Khamsi wrote:
    > Peter Hansen <> writes:
    >
    >>class _Helper(object):

    ....
    >> def __call__(self, *args, **kwds):
    >> import pydoc
    >> return pydoc.help(*args, **kwds)

    >
    > Thanks, but how do I integrate this with cmd.Cmd?


    I can't say exactly. That might depend on what you are doing with it,
    and how you are currently handling other things with it. All I know is
    that the builtin "help" just passes its arguments to pydoc.help() and
    that seems to do the job.

    I'd suggest you work from there and experiment with your current cmd.Cmd
    mechanisms to see what you can get working, then post a snippet or to
    back here for further assistance. Almost working code is almost always
    better than starting with nothing. (I would help if I remembered
    anything about cmd.Cmd, really, but it's been years.)

    -Peter
     
    Peter Hansen, Jun 10, 2005
    #4
  5. On Thu, 09 Jun 2005 16:53:17 -0700, Sarir Khamsi <> wrote:

    >Peter Hansen <> 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
     
    Bengt Richter, Jun 26, 2005
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Tim
    Replies:
    2
    Views:
    298
    Peter Hansen
    Jul 7, 2003
  2. Achim Domma (Procoders)

    read input for cmd.Cmd from file

    Achim Domma (Procoders), Jun 3, 2005, in forum: Python
    Replies:
    2
    Views:
    8,084
    Peter Otten
    Jun 3, 2005
  3. =?ISO-8859-1?Q?Sch=FCle_Daniel?=

    [exec cmd for cmd in cmds]

    =?ISO-8859-1?Q?Sch=FCle_Daniel?=, Mar 8, 2006, in forum: Python
    Replies:
    3
    Views:
    393
    Scott David Daniels
    Mar 8, 2006
  4. Diez B. Roggisch

    pydb remote debugging/cmd.Cmd over socket?

    Diez B. Roggisch, May 28, 2008, in forum: Python
    Replies:
    2
    Views:
    552
    Diez B. Roggisch
    May 29, 2008
  5. Diez B. Roggisch

    cmd.Cmd bug or at least docu-bug

    Diez B. Roggisch, May 29, 2008, in forum: Python
    Replies:
    1
    Views:
    345
    Michele Simionato
    May 29, 2008
Loading...

Share This Page