472,779 Members | 1,751 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,779 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 3222
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...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
0
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
0
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
0
by: lllomh | last post by:
How does React native implement an English player?
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

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.