import textwrap


def document_parser(script_name, reference):
    """
    Fancy function for documenting a parser easily. Runs the function to build
    the parser, then gets the parsers help texts and formats it into the
    function's doc string for sphinx. A bit hacky but works great!

    Args:
        script_name (str): Name of the script.
        reference (str): Where to point the reference function for the script 
            (e.g. the script that it runs).
    """
    def decorator(parser_func):
        parser = parser_func()
        help_text = parser.format_help()
        help_text = help_text.replace('sphinx-build', f'python -m scripts.{script_name} ')
        help_text = textwrap.indent(help_text, '\t\t  ')
        help_text = '   .. code-block:: none\n\n' + help_text

        doc = (
            f"""
            Builds the parser for :py:mod:`scripts.{script_name}`.
            
            {help_text}

            Also see the arguments to :py:func:`{reference}`.

            Returns:
                :class:`argparse.ArgumentParser`: The parser for this script.
            """
        )

        parser_func.__doc__ = doc        
        return parser_func
    return decorator