Search code examples
pythonmanpage

How to implement man-like help page in python(python shell already has it)


in python interactive shell, if you do

>>> import os
>>> help(os)

you will get a linux man-like help page. anybody has ideas how to do it in pure python? Now I have implemented a similar shell by raw_input and python readline module. But I totally have no idea how to do the help page.

thanks.


Solution

  • Look at the code for pydoc, i.e.:

        Python27\Lib\pydoc.py
    

    (This is for Windows, of course everywhere else the slashes go the other way.)

    Helper class's help member function calls doc function calls render_doc, which is probably the function you want.

    import sys
    import pydoc
    
    plainSysDoc = pydoc.plain((pydoc.render_doc(sys)))
    print plainSysDoc
    

    pydoc.plain is a formatting function (that removes bold formatting).

    As a side note, while fact checking this answer I learned that pydoc can be called from the command line:

    pydoc sys