def _patch_argument_parser(self):
active_parsers = [self._parser]<EOL>parsed_args = argparse.Namespace()<EOL>visited_actions = []<EOL>def patch(parser):<EOL><INDENT>parser.__class__ = IntrospectiveArgumentParser<EOL>for action in parser._actions:<EOL><INDENT>class IntrospectAction(action.__class__):<EOL><INDENT>def __call__(self, parser, namespace, values, option_string=None):<EOL><INDENT>debug('<STR_LIT>', self)<EOL>debug('<STR_LIT>', parser, namespace, values, option_string)<EOL>debug('<STR_LIT>', self._orig_class)<EOL>debug('<STR_LIT>', self._orig_callable)<EOL>visited_actions.append(self)<EOL>if self._orig_class == argparse._SubParsersAction:<EOL><INDENT>debug('<STR_LIT>')<EOL>active_subparser = self._name_parser_map[values[<NUM_LIT:0>]]<EOL>patch(active_subparser)<EOL>active_parsers.append(active_subparser)<EOL>self._orig_callable(parser, namespace, values, option_string=option_string)<EOL><DEDENT>elif self._orig_class in safe_actions:<EOL><INDENT>self._orig_callable(parser, namespace, values, option_string=option_string)<EOL><DEDENT><DEDENT><DEDENT>if getattr(action, "<STR_LIT>", None):<EOL><INDENT>debug("<STR_LIT>", action, "<STR_LIT>")<EOL><DEDENT>action._orig_class = action.__class__<EOL>action._orig_callable = action.__call__<EOL>action.__class__ = IntrospectAction<EOL><DEDENT><DEDENT>patch(self._parser)<EOL>debug("<STR_LIT>", active_parsers)<EOL>debug("<STR_LIT>", visited_actions)<EOL>debug("<STR_LIT>", parsed_args)<EOL>return active_parsers, parsed_args<EOL>
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.