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

StackOverflow https://stackoverflow.com/questions/13612437

  •  03-12-2021
  •  | 
  •  

سؤال

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.

هل كانت مفيدة؟

المحلول

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

نصائح أخرى

OK, I got an easy way. just call 'man' by subprocess and make my help documents to man page separately

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top