Since argparse doesn't support much introspection, we monkey-patch it to replace the parse_known_args method and
        all actions with hooks that tell us which action was last taken or about to be taken, and let us have the parser
        figure out which subparsers need to be activated (then recursively monkey-patch those).
        We save all active ArgumentParsers to extract all their possible option names later.

def _patch_argument_parser(self):
        '''
        Since argparse doesn't support much introspection, we monkey-patch it to replace the parse_known_args method and
        all actions with hooks that tell us which action was last taken or about to be taken, and let us have the parser
        figure out which subparsers need to be activated (then recursively monkey-patch those).
        We save all active ArgumentParsers to extract all their possible option names later.
        '''
        active_parsers = [self._parser]
        parsed_args = argparse.Namespace()
        visited_actions = []

        def patch(parser):
            parser.__class__ = IntrospectiveArgumentParser
            for action in parser._actions:
                # TODO: accomplish this with super
                class IntrospectAction(action.__class__):
                    def __call__(self, parser, namespace, values, option_string=None):
                        debug('Action stub called on', self)
                        debug('\targs:', parser, namespace, values, option_string)
                        debug('\torig class:', self._orig_class)
                        debug('\torig callable:', self._orig_callable)

                        visited_actions.append(self)

                        if self._orig_class == argparse._SubParsersAction:
                            debug('orig class is a subparsers action: patching and running it')
                            active_subparser = self._name_parser_map[values[0]]
                            patch(active_subparser)
                            active_parsers.append(active_subparser)
                            self._orig_callable(parser, namespace, values, option_string=option_string)
                        elif self._orig_class in safe_actions:
                            self._orig_callable(parser, namespace, values, option_string=option_string)
                if getattr(action, "_orig_class", None):
                    debug("Action", action, "already patched")
                action._orig_class = action.__class__
                action._orig_callable = action.__call__
                action.__class__ = IntrospectAction

        patch(self._parser)

        debug("Active parsers:", active_parsers)
        debug("Visited actions:", visited_actions)
        debug("Parse result namespace:", parsed_args)

        return active_parsers, parsed_args