Transforms the signature of the function, and it's associated __doc__
        into an argparse-parser, then calls the function with the results of
        using said parser.

        The function returned takes an optional argument, which is the list of
        parameters, if they are not given, sys.argv[1:] is used instead.

        The function may raise a UsageError() if it wants to signal an error
        that the user has made with the parameters, this is done by
        @withuserfile for example.

        CAVEAT: this relies on the argument signature of the function, if that
                has been destroyed, perhaps by a badly behaved decorator, this
                won't work as expected.

        CAVEAT2: this destroys the argument signature of the function ;)

def acceptargv(func):
    """
        Transforms the signature of the function, and it's associated __doc__
        into an argparse-parser, then calls the function with the results of
        using said parser.

        The function returned takes an optional argument, which is the list of
        parameters, if they are not given, sys.argv[1:] is used instead.

        The function may raise a UsageError() if it wants to signal an error
        that the user has made with the parameters, this is done by
        @withuserfile for example.

        CAVEAT: this relies on the argument signature of the function, if that
                has been destroyed, perhaps by a badly behaved decorator, this
                won't work as expected.

        CAVEAT2: this destroys the argument signature of the function ;)
    """

    parser = signature_parser(func)

    def main(*args, **kw):
        argv = kw.get('argv', None)
        if argv == None:
            return func(*args, **kw)
        else:
            try:
                kwargs = parser.parse_args(argv).__dict__

                # special cli flags

                # --version is handled by ArgParse
                # if kwargs.get('version'):
                #    print module_version(func)
                #    return
                if 'version' in kwargs.keys():
                    del kwargs['version']

                # --debug
                if kwargs.get('debug'):
                    logging.basicConfig(level=logging.DEBUG)
                del kwargs['debug']

                if "__args" in kwargs:
                    return func(*_correct_args(func, kwargs))
                else:
                    return func(**kwargs)
            except UsageError, e:
                parser.error(e.message)

    main.__doc__ = func.__doc__
    main.__name__ = func.__name__
    main.__module__ = func.__module__
    main.__dict__ = func.__dict__.copy()

    return main