By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,619 Members | 1,712 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,619 IT Pros & Developers. It's quick & easy.

Interpreter-like help in cmd.Cmd

P: n/a
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
Jul 19 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
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
Jul 19 '05 #2

P: n/a
Peter Hansen <pe***@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?
Jul 19 '05 #3

P: n/a
Sarir Khamsi wrote:
Peter Hansen <pe***@engcorp.com> 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
Jul 19 '05 #4

P: n/a
On Thu, 09 Jun 2005 16:53:17 -0700, Sarir Khamsi <sa**********@raytheon.com> wrote:
Peter Hansen <pe***@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
Jul 19 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.