# Buggy code
def main(argv=sys.argv):
    # type: (List[str]) -> int
    """Parse and check the command line arguments."""
    parser = optparse.OptionParser(
        usage="""\
usage: %prog [options] -o <output_path> <module_path> [exclude_pattern, ...]

Look recursively in <module_path> for Python modules and packages and create
one reST file with automodule directives per package in the <output_path>.

The <exclude_pattern>s can be file and/or directory patterns that will be
excluded from generation.

Note: By default this script will not overwrite already created files."""
    )

    parser.add_option(
        "-o",
        "--output-dir",
        action="store",
        dest="destdir",
        help="Directory to place all output",
        default="",
    )
    parser.add_option(
        "-d",
        "--maxdepth",
        action="store",
        dest="maxdepth",
        help="Maximum depth of submodules to show in the TOC (default: 4)",
        type="int",
        default=4,
    )
    parser.add_option(
        "-f",
        "--force",
        action="store_true",
        dest="force",
        help="Overwrite existing files",
    )
    parser.add_option(
        "-l",
        "--follow-links",
        action="store_true",
        dest="followlinks",
        default=False,
        help="Follow symbolic links. Powerful when combined "
        "with collective.recipe.omelette.",
    )
    parser.add_option(
        "-n",
        "--dry-run",
        action="store_true",
        dest="dryrun",
        help="Run the script without creating files",
    )
    parser.add_option(
        "-e",
        "--separate",
        action="store_true",
        dest="separatemodules",
        help="Put documentation for each module on its own page",
    )
    parser.add_option(
        "-P",
        "--private",
        action="store_true",
        dest="includeprivate",
        help='Include "_private" modules',
    )
    parser.add_option(
        "-T",
        "--no-toc",
        action="store_true",
        dest="notoc",
        help="Don't create a table of contents file",
    )
    parser.add_option(
        "-E",
        "--no-headings",
        action="store_true",
        dest="noheadings",
        help="Don't create headings for the module/package "
        "packages (e.g. when the docstrings already contain "
        "them)",
    )
    parser.add_option(
        "-M",
        "--module-first",
        action="store_true",
        dest="modulefirst",
        help="Put module documentation before submodule documentation",
    )
    parser.add_option(
        "--implicit-namespaces",
        action="store_true",
        dest="implicit_namespaces",
        help="Interpret module paths according to PEP-0420 "
        "implicit namespaces specification",
    )
    parser.add_option(
        "-s",
        "--suffix",
        action="store",
        dest="suffix",
        help="file suffix (default: rst)",
        default="rst",
    )
    parser.add_option(
        "-F",
        "--full",
        action="store_true",
        dest="full",
        help="Generate a full project with sphinx-quickstart",
    )
    parser.add_option(
        "-a",
        "--append-syspath",
        action="store_true",
        dest="append_syspath",
        help="Append module_path to sys.path, used when --full is given",
    )
    parser.add_option(
        "-H",
        "--doc-project",
        action="store",
        dest="header",
        help="Project name (default: root module name)",
    )
    parser.add_option(
        "-A",
        "--doc-author",
        action="store",
        dest="author",
        type="str",
        help="Project author(s), used when --full is given",
    )
    parser.add_option(
        "-V",
        "--doc-version",
        action="store",
        dest="version",
        help="Project version, used when --full is given",
    )
    parser.add_option(
        "-R",
        "--doc-release",
        action="store",
        dest="release",
        help="Project release, used when --full is given, defaults to --doc-version",
    )
    parser.add_option(
        "--version",
        action="store_true",
        dest="show_version",
        help="Show version information and exit",
    )
    group = parser.add_option_group("Extension options")
    for ext in EXTENSIONS:
        group.add_option(
            "--ext-" + ext,
            action="store_true",
            dest="ext_" + ext,
            default=False,
            help="enable %s extension" % ext,
        )

    (opts, args) = parser.parse_args(argv[1:])

    if opts.show_version:
        print("Sphinx (sphinx-apidoc) %s" % __display_version__)
        return 0

    if not args:
        parser.error("A package path is required.")

    rootpath, excludes = args[0], args[1:]
    if not opts.destdir:
        parser.error("An output directory is required.")
    if opts.header is None:
        opts.header = path.abspath(rootpath).split(path.sep)[-1]
    if opts.suffix.startswith("."):
        opts.suffix = opts.suffix[1:]
    if not path.isdir(rootpath):
        print("%s is not a directory." % rootpath, file=sys.stderr)
        sys.exit(1)
    if not path.isdir(opts.destdir):
        if not opts.dryrun:
            os.makedirs(opts.destdir)
    rootpath = path.abspath(rootpath)
    excludes = normalize_excludes(rootpath, excludes)
    modules = recurse_tree(rootpath, excludes, opts)
    if opts.full:
        from sphinx import quickstart as qs

        modules.sort()
        prev_module = ""  # type: unicode
        text = ""
        for module in modules:
            if module.startswith(prev_module + "."):
                continue
            prev_module = module
            text += "   %s\n" % module
        d = dict(
            path=opts.destdir,
            sep=False,
            dot="_",
            project=opts.header,
            author=opts.author or "Author",
            version=opts.version or "",
            release=opts.release or opts.version or "",
            suffix="." + opts.suffix,
            master="index",
            epub=True,
            ext_autodoc=True,
            ext_viewcode=True,
            ext_todo=True,
            makefile=True,
            batchfile=True,
            mastertocmaxdepth=opts.maxdepth,
            mastertoctree=text,
            language="en",
            module_path=rootpath,
            append_syspath=opts.append_syspath,
        )
        enabled_exts = {
            "ext_" + ext: getattr(opts, "ext_" + ext)
            for ext in EXTENSIONS
            if getattr(opts, "ext_" + ext)
        }
        d.update(enabled_exts)

        if isinstance(opts.header, binary_type):
            d["project"] = d["project"].decode("utf-8")
        if isinstance(opts.author, binary_type):
            d["author"] = d["author"].decode("utf-8")
        if isinstance(opts.version, binary_type):
            d["version"] = d["version"].decode("utf-8")
        if isinstance(opts.release, binary_type):
            d["release"] = d["release"].decode("utf-8")

        if not opts.dryrun:
            qs.generate(d, silent=True, overwrite=opts.force)
    elif not opts.notoc:
        create_modules_toc_file(modules, opts)
    return 0


# Traceback (OSError)
# Sphinx version: 1.6.3
# Python version: 2.7.14 (CPython)
# Docutils version: 0.12 release
# Jinja2 version: 2.9.5
# Last messages:

# Loaded extensions:
Traceback (most recent call last):
File "/usr/lib/python2.7/site-packages/sphinx/cmdline.py", line 305, in main
opts.warningiserror, opts.tags, opts.verbosity, opts.jobs)
File "/usr/lib/python2.7/site-packages/sphinx/application.py", line 163, in __init__
os.makedirs(outdir)
File "/usr/lib/python2.7/os.py", line 157, in makedirs
mkdir(name, mode)
OSError: [Errno 17] File exists: '/var/tmp/portage/sys-devel/llvm-9999/work/llvm-9999-abi_x86_32.x86/docs/man'

# Fixed code
def main(argv=sys.argv):
    # type: (List[str]) -> int
    """Parse and check the command line arguments."""
    parser = optparse.OptionParser(
        usage="""\
usage: %prog [options] -o <output_path> <module_path> [exclude_pattern, ...]

Look recursively in <module_path> for Python modules and packages and create
one reST file with automodule directives per package in the <output_path>.

The <exclude_pattern>s can be file and/or directory patterns that will be
excluded from generation.

Note: By default this script will not overwrite already created files."""
    )

    parser.add_option(
        "-o",
        "--output-dir",
        action="store",
        dest="destdir",
        help="Directory to place all output",
        default="",
    )
    parser.add_option(
        "-d",
        "--maxdepth",
        action="store",
        dest="maxdepth",
        help="Maximum depth of submodules to show in the TOC (default: 4)",
        type="int",
        default=4,
    )
    parser.add_option(
        "-f",
        "--force",
        action="store_true",
        dest="force",
        help="Overwrite existing files",
    )
    parser.add_option(
        "-l",
        "--follow-links",
        action="store_true",
        dest="followlinks",
        default=False,
        help="Follow symbolic links. Powerful when combined "
        "with collective.recipe.omelette.",
    )
    parser.add_option(
        "-n",
        "--dry-run",
        action="store_true",
        dest="dryrun",
        help="Run the script without creating files",
    )
    parser.add_option(
        "-e",
        "--separate",
        action="store_true",
        dest="separatemodules",
        help="Put documentation for each module on its own page",
    )
    parser.add_option(
        "-P",
        "--private",
        action="store_true",
        dest="includeprivate",
        help='Include "_private" modules',
    )
    parser.add_option(
        "-T",
        "--no-toc",
        action="store_true",
        dest="notoc",
        help="Don't create a table of contents file",
    )
    parser.add_option(
        "-E",
        "--no-headings",
        action="store_true",
        dest="noheadings",
        help="Don't create headings for the module/package "
        "packages (e.g. when the docstrings already contain "
        "them)",
    )
    parser.add_option(
        "-M",
        "--module-first",
        action="store_true",
        dest="modulefirst",
        help="Put module documentation before submodule documentation",
    )
    parser.add_option(
        "--implicit-namespaces",
        action="store_true",
        dest="implicit_namespaces",
        help="Interpret module paths according to PEP-0420 "
        "implicit namespaces specification",
    )
    parser.add_option(
        "-s",
        "--suffix",
        action="store",
        dest="suffix",
        help="file suffix (default: rst)",
        default="rst",
    )
    parser.add_option(
        "-F",
        "--full",
        action="store_true",
        dest="full",
        help="Generate a full project with sphinx-quickstart",
    )
    parser.add_option(
        "-a",
        "--append-syspath",
        action="store_true",
        dest="append_syspath",
        help="Append module_path to sys.path, used when --full is given",
    )
    parser.add_option(
        "-H",
        "--doc-project",
        action="store",
        dest="header",
        help="Project name (default: root module name)",
    )
    parser.add_option(
        "-A",
        "--doc-author",
        action="store",
        dest="author",
        type="str",
        help="Project author(s), used when --full is given",
    )
    parser.add_option(
        "-V",
        "--doc-version",
        action="store",
        dest="version",
        help="Project version, used when --full is given",
    )
    parser.add_option(
        "-R",
        "--doc-release",
        action="store",
        dest="release",
        help="Project release, used when --full is given, defaults to --doc-version",
    )
    parser.add_option(
        "--version",
        action="store_true",
        dest="show_version",
        help="Show version information and exit",
    )
    group = parser.add_option_group("Extension options")
    for ext in EXTENSIONS:
        group.add_option(
            "--ext-" + ext,
            action="store_true",
            dest="ext_" + ext,
            default=False,
            help="enable %s extension" % ext,
        )

    (opts, args) = parser.parse_args(argv[1:])

    if opts.show_version:
        print("Sphinx (sphinx-apidoc) %s" % __display_version__)
        return 0

    if not args:
        parser.error("A package path is required.")

    rootpath, excludes = args[0], args[1:]
    if not opts.destdir:
        parser.error("An output directory is required.")
    if opts.header is None:
        opts.header = path.abspath(rootpath).split(path.sep)[-1]
    if opts.suffix.startswith("."):
        opts.suffix = opts.suffix[1:]
    if not path.isdir(rootpath):
        print("%s is not a directory." % rootpath, file=sys.stderr)
        sys.exit(1)
    if not opts.dryrun:
        ensuredir(opts.destdir)
    rootpath = path.abspath(rootpath)
    excludes = normalize_excludes(rootpath, excludes)
    modules = recurse_tree(rootpath, excludes, opts)
    if opts.full:
        from sphinx import quickstart as qs

        modules.sort()
        prev_module = ""  # type: unicode
        text = ""
        for module in modules:
            if module.startswith(prev_module + "."):
                continue
            prev_module = module
            text += "   %s\n" % module
        d = dict(
            path=opts.destdir,
            sep=False,
            dot="_",
            project=opts.header,
            author=opts.author or "Author",
            version=opts.version or "",
            release=opts.release or opts.version or "",
            suffix="." + opts.suffix,
            master="index",
            epub=True,
            ext_autodoc=True,
            ext_viewcode=True,
            ext_todo=True,
            makefile=True,
            batchfile=True,
            mastertocmaxdepth=opts.maxdepth,
            mastertoctree=text,
            language="en",
            module_path=rootpath,
            append_syspath=opts.append_syspath,
        )
        enabled_exts = {
            "ext_" + ext: getattr(opts, "ext_" + ext)
            for ext in EXTENSIONS
            if getattr(opts, "ext_" + ext)
        }
        d.update(enabled_exts)

        if isinstance(opts.header, binary_type):
            d["project"] = d["project"].decode("utf-8")
        if isinstance(opts.author, binary_type):
            d["author"] = d["author"].decode("utf-8")
        if isinstance(opts.version, binary_type):
            d["version"] = d["version"].decode("utf-8")
        if isinstance(opts.release, binary_type):
            d["release"] = d["release"].decode("utf-8")

        if not opts.dryrun:
            qs.generate(d, silent=True, overwrite=opts.force)
    elif not opts.notoc:
        create_modules_toc_file(modules, opts)
    return 0