python command to print docstrings in file

5

1

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

Posted 2013-08-26T22:32:47.643

Reputation: 463

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 – 2013-08-27T01:06:12.643

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 – 2013-08-27T01:16:36.383

Answers

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

Posted 2013-08-26T22:32:47.643

Reputation: 463

Note that the ./ is important; otherwise there will be no output. – dehmann – 2014-10-27T01:23:11.323