5

What is the command-line to print the docstrings in a given Python file?

I thought there was something like:

$ python --showhelp foo.py

Name                 Help
--------------------------------------------------
bar                  This is the docstring for bar().
baz                  This is the docstring for baz().
tony19
  • 503
  • 1
  • 8
  • 19
  • I have never come over anything that can do this while I have been coding in python. I think you have to either make a parser yourself or open Idle and use the built in help() function. – Mogget Aug 27 '13 at 01:06
  • Similar response to @Mogget, I have only seen print ClassName.__doc__ or print ClassName.Method.__doc__ - meaning that you could fairly easily make a parser. – nerdwaller Aug 27 '13 at 01:16

1 Answers1

5

Found it: pydoc

For this example file:

# foo.py

def bar():
  """this is the docstring for bar()"""
  print 'hello'


def baz():
  """this is the docstring for baz()"""
  print 'world'

you can print the docstrings to stdout with this command:

$ pydoc ./foo.py
Help on module foo:

NAME
    foo

FILE
    /path/to/foo.py

FUNCTIONS
    bar()
        this is the docstring for bar()

    baz()
        this is the docstring for baz()

You can also generate an HTML help file:

$ pydoc -w ./foo.py
wrote foo.html

which looks like this:

enter image description here

tony19
  • 503
  • 1
  • 8
  • 19