Converts a well-formed docstring into documentation
        to be fed into argparse.

        See signature_parser for details.

        shorts: (-k for --keyword -k, or "from" for "frm/from")
        metavars: (FILE for --input=FILE)
        helps: (docs for --keyword: docs)
        description: the stuff before
        epilog: the stuff after

def _parse_doc(docs):
    """
        Converts a well-formed docstring into documentation
        to be fed into argparse.

        See signature_parser for details.

        shorts: (-k for --keyword -k, or "from" for "frm/from")
        metavars: (FILE for --input=FILE)
        helps: (docs for --keyword: docs)
        description: the stuff before
        epilog: the stuff after
    """

    name = "(?:[a-zA-Z][a-zA-Z0-9-_]*)"

    re_var = re.compile(r"^ *(%s)(?: */(%s))? *:(.*)$" % (name, name))
    re_opt = re.compile(r"^ *(?:(-[a-zA-Z0-9]),? +)?--(%s)(?: *=(%s))? *:(.*)$"
                        % (name, name))

    shorts, metavars, helps, description, epilog = {}, {}, {}, "", ""

    if docs:

        for line in docs.split("\n"):

            line = line.strip()

            # remove starting ':param'
            if line.startswith(':param'):
                line = line[len(':param'):]

            # skip ':rtype:' row
            if line.startswith(':rtype:'):
                continue

            if line.strip() == "----":
                break

            m = re_var.match(line)
            if m:
                if epilog:
                    helps[prev] += epilog.strip()
                    epilog = ""

                if m.group(2):
                    shorts[m.group(1)] = m.group(2)

                helps[m.group(1)] = m.group(3).strip()
                prev = m.group(1)
                previndent = len(line) - len(line.lstrip())
                continue

            m = re_opt.match(line)
            if m:
                if epilog:
                    helps[prev] += epilog.strip()
                    epilog = ""
                name = m.group(2).replace("-", "_")
                helps[name] = m.group(4)
                prev = name

                if m.group(1):
                    shorts[name] = m.group(1)
                if m.group(3):
                    metavars[name] = m.group(3)

                previndent = len(line) - len(line.lstrip())
                continue

            if helps:
                if line.startswith(" " * (previndent + 1)):
                    helps[prev] += "\n" + line.strip()
                else:
                    epilog += "\n" + line.strip()
            else:
                description += "\n" + line.strip()

            if line.strip():
                previndent = len(line) - len(line.lstrip())

    return shorts, metavars, helps, description, epilog