Compile functions for argparsing.

def _compile(self, module=None):
        """
        Compile functions for argparsing.
        """
        # get the main module
        mod = module or sys.modules['__main__']
        self.mod = mod
        # setup the script version
        if hasattr(mod, '__version__'):
            version = "%(prog)s " + mod.__version__
            self.ap_args.append(
                ap_arg('-v', '--version', action='version', version=version))
        # setup the script description
        if module is None:
            if 'description' not in self.argparse_args and hasattr(mod, '__doc__'):
                self.argparse_args['description'] = mod.__doc__
        # setup main parser
        self.parser = argparse.ArgumentParser(
            formatter_class=self.formatter_class, **self.argparse_args)
        if self.error:
            # setup the error method if we have one
            setattr(
                self.parser, 'error', types.MethodType(self.error, self.parser))
        if self.add_output:
            # add the output options if we have the add_output true
            if isinstance(self.add_output, basestring):
                odefault = self.add_output
            else:
                odefault = None
            self.parser.add_argument(
                '-o', '--output', metavar="FILE", default=odefault,
                help="Output to file")
            self.parser.add_argument(
                '--encoding', default="utf-8", help="Encoding for output file.")
            self.parser.add_argument(
                '--write-mode', default=self.write_mode, help="Write mode")
        if self._cfg_factory:
            # if we have a config factory add the config options
            self.parser.add_argument(
                '-c', '--config', metavar="FILE", default=self.cfg_file,
                help="Configuration file")
        if self.auths:
            # if we have authorizations enabled add the auth options
            self.parser.add_argument(
                '--auth',
                help="Authorization phrase for special commands.")

        # let's exexcute plugins
        for plugin in ArgParseInator._plugins.values():
            plugin(self)

        if self.ap_args is not None:
            # if we have argment parser args we will add them to the main parser
            for aargs, akargs in self.ap_args:
                self.parser.add_argument(*aargs, **akargs)

        if len(self.commands) == 1 and self.never_single is False:
            # if we have only one command ad never_single is False
            # setup the command as the only command.
            func = six.next(six.itervalues(self.commands))
            # add the arguments to the main parser
            for args, kwargs in func.__arguments__:
                self.parser.add_argument(*args, **kwargs)
            # shortcut for the single command
            self._single = func
            if not self.parser.description:
                # replace the description if we dont' have one
                self.parser.description = func.__doc__ or self.parser.description
            else:
                # or add to the main description
                if func.__doc__:
                    self.parser.description += linesep + linesep + func.__doc__
            if not self.parser.epilog:
                # replace the description if we dont' have one
                self.parser.epilog = getattr(
                    func, "__epilog__", self.parser.epilog)
            else:
                # or add to the main description
                if hasattr(func, '__epilog__'):
                    self.parser.epilog += linesep + linesep + func.__epilog__
            utils.set_subcommands(func, self.parser)
        else:
            # if we have more than one command or we don't want a single command
            # set the single to None
            self._single = None
            # setup the subparsers
            self.subparsers = self.parser.add_subparsers(
                title=utils.COMMANDS_LIST_TITLE, dest='command',
                description=utils.COMMANDS_LIST_DESCRIPTION)
            # add all the commands
            for func in self.commands.values():
                parser = utils.get_parser(func, self.subparsers)
                utils.set_subcommands(func, parser)
        return self.parser