473,405 Members | 2,261 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

Interpreter-like help in cmd.Cmd

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
4 3307
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Donnie Leen | last post by:
I wrote a program embbed boost.python, each thread running a sub-interpreter, i made a module implement by boost.python, and i wish this module imported in each sub-interpreter, i find that the...
6
by: DE | last post by:
Hello, Some long time ago, I used to use Tcl/Tk. I had an tcl embedded into my app. The coolest thing was however, I was able to attach to the interpreter (built in to my app) via a tcl shell...
12
by: Rex Eastbourne | last post by:
Hi, I'm interested in running a Python interpreter in Emacs. I have Python extensions for Emacs, and my python menu lists "C-c !" as the command to run the interpreter. Yet when I run it I get...
12
by: ozbear | last post by:
If one were writing a C interpreter, is there anything in the standard standard that requires the sizeof operator to yield the same value for two different variables of the same type? Let's...
5
by: Russ | last post by:
I wrote a simple little function for exiting with an error message: def error ( message ): print_stack(); exit ("\nERROR: " + message + "\n") It works fine for executing as a script, but when...
0
by: Ole Nielsby | last post by:
I just wonder if this is possible with C++/clr. I'm trying to .NET-enable an interpreter for a Lisp flavoured language, the interpreter written in NASM which I have managed to port to MASM....
3
by: Robin Becker | last post by:
As part of some django usage I need to get some ReportLab C extensions into a state where they can be safely used with mod_python. Unfortunately we have C code that seems incompatible with...
3
by: Nils Overas Bergen | last post by:
I have created a Python application in Windows XP which uses WxWidgets. When I start the application from the Python interpreter I get one empty interpreter window in addition to the application...
16
by: lawrence k | last post by:
I've never before written a PHP script to run on my home Ubuntu machine (though I've written dozens of PHP cron jobs for my web server), so I thought I'd start off with a very simple test: ...
1
by: John Boy | last post by:
First post and very much a newbie to Python. Have 2.5 on Linux (GG). I think I have set up PYTHONPATH correctly in that I can import a module apply_bp and it complains about line 20 in apply_bp...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.