def invoke()

in knack/cli.py [0:0]


    def invoke(self, args, initial_invocation_data=None, out_file=None):
        """ Invoke a command.

        :param args: The arguments that represent the command
        :type args: list, tuple
        :param initial_invocation_data: Prime the in memory collection of key-value data for this invocation.
        :type initial_invocation_data: dict
        :param out_file: The file to send output to. If not used, we use out_file for knack.cli.CLI instance
        :type out_file: file-like object
        :return: The exit code of the invocation
        :rtype: int
        """
        from .util import CommandResultItem

        if not isinstance(args, (list, tuple)):
            raise TypeError('args should be a list or tuple.')
        exit_code = 0
        try:
            out_file = out_file or self.out_file
            if out_file is sys.stdout and self._should_init_colorama:
                self.init_debug_log.append("Init colorama.")
                import colorama
                colorama.init()
                # point out_file to the new sys.stdout which is overwritten by colorama
                out_file = sys.stdout

            args = self.completion.get_completion_args() or args

            self.logging.configure(args)
            logger.debug('Command arguments: %s', args)
            self._print_init_log()

            self.raise_event(EVENT_CLI_PRE_EXECUTE)
            if CLI._should_show_version(args):
                self.show_version()
                self.result = CommandResultItem(None)
            else:
                self.invocation = self.invocation_cls(cli_ctx=self,
                                                      parser_cls=self.parser_cls,
                                                      commands_loader_cls=self.commands_loader_cls,
                                                      help_cls=self.help_cls,
                                                      initial_data=initial_invocation_data)
                cmd_result = self.invocation.execute(args)
                self.result = cmd_result
                exit_code = self.result.exit_code
                output_type = self.invocation.data['output']
                if cmd_result and cmd_result.result is not None:
                    formatter = self.output.get_formatter(output_type)
                    self.output.out(cmd_result, formatter=formatter, out_file=out_file)
                self.raise_event(EVENT_CLI_SUCCESSFUL_EXECUTE, result=cmd_result)
        except KeyboardInterrupt as ex:
            exit_code = 1
            self.result = CommandResultItem(None, error=ex, exit_code=exit_code)
        except Exception as ex:  # pylint: disable=broad-except
            exit_code = self.exception_handler(ex)
            self.result = CommandResultItem(None, error=ex, exit_code=exit_code)
        except SystemExit as ex:
            exit_code = ex.code
            self.result = CommandResultItem(None, error=ex, exit_code=exit_code)
            raise ex
        finally:
            self.raise_event(EVENT_CLI_POST_EXECUTE)

            if self._should_init_colorama:
                import colorama
                colorama.deinit()

        return exit_code