code: def main():
      p = optparse.OptionParser(__doc__.strip())
      p.add_option(
          "-p",
          "--phantom",
          action="store",
          type="string",
          dest="phantom",
          default=None,
          help="Phantom import modules from a file",
      )
      p.add_option(
          "-o",
          "--output-dir",
          action="store",
          type="string",
          dest="output_dir",
          default=None,
          help=(
              "Write all output files to the given directory (instead "
              "of writing them as specified in the autosummary:: "
              "directives)"
          ),
      )
      options, args = p.parse_args()
  
      if len(args) == 0:
          p.error("wrong number of arguments")
  
      if options.phantom and os.path.isfile(options.phantom):
          import_phantom_module(options.phantom)
  
      # read
      names = {}
      for name, loc in get_documented(args).items():
          for filename, sec_title, keyword, toctree in loc:
              if toctree is not None:
                  path = os.path.join(
                      os.path.dirname(filename), toctree
                  )
                  names[name] = os.path.abspath(path)
  
      # write
      for name, path in sorted(names.items()):
          if options.output_dir is not None:
              path = options.output_dir
  
          if not os.path.isdir(path):
              os.makedirs(path)
  
          try:
              obj, name = import_by_name(name)
          except ImportError as e:
              print(f"Failed to import '{name}': {e}")
              continue
  
          fn = os.path.join(path, "%s.rst" % name)
  
          if os.path.exists(fn):
              # skip
              continue
  
          f = open(fn, "w")
  
          try:
              f.write("{}\n{}\n\n".format(name, "=" * len(name)))
  
              if inspect.isclass(obj):
                  if issubclass(obj, Exception):
                      f.write(
                          format_modulemember(name, "autoexception")
                      )
                  else:
                      f.write(format_modulemember(name, "autoclass"))
              elif inspect.ismodule(obj):
                  f.write(format_modulemember(name, "automodule"))
              elif inspect.ismethod(obj) or inspect.ismethoddescriptor(
                  obj
              ):
                  f.write(format_classmember(name, "automethod"))
              elif callable(obj):
                  f.write(format_modulemember(name, "autofunction"))
              elif hasattr(obj, "__get__"):
                  f.write(format_classmember(name, "autoattribute"))
              else:
                  f.write(format_modulemember(name, "autofunction"))
          finally:
              f.close()
  
file_path: nipy/doc/sphinxext/autosummary_generate.py
imports: import glob
  import inspect
  import optparse
  import os
  import pydoc
  import re
  from autosummary import import_by_name
label_desc: Use image node_id 1 for calling a global function with example usage: main() without return types