def _get_subparser()

in knack/parser.py [0:0]


    def _get_subparser(self, path, group_table=None):
        """For each part of the path, walk down the tree of
        subparsers, creating new ones if one doesn't already exist.
        """
        group_table = group_table or {}
        for length in range(0, len(path)):
            parent_path = path[:length]
            parent_subparser = self.subparsers.get(tuple(parent_path), None)
            if not parent_subparser:
                # No subparser exists for the given subpath - create and register
                # a new subparser.
                # Since we know that we always have a root subparser (we created)
                # one when we started loading the command table, and we walk the
                # path from left to right (i.e. for "cmd subcmd1 subcmd2", we start
                # with ensuring that a subparser for cmd exists, then for subcmd1,
                # subcmd2 and so on), we know we can always back up one step and
                # add a subparser if one doesn't exist
                command_group = group_table.get(' '.join(parent_path))
                if command_group:
                    deprecate_info = command_group.group_kwargs.get('deprecate_info', None)
                    if deprecate_info and deprecate_info.expired():
                        continue
                grandparent_path = path[:length - 1]
                grandparent_subparser = self.subparsers[tuple(grandparent_path)]
                new_path = path[length - 1]
                new_parser = grandparent_subparser.add_parser(new_path, cli_help=self.cli_help)

                # Due to http://bugs.python.org/issue9253, we have to give the subparser
                # a destination and set it to required in order to get a meaningful error
                parent_subparser = new_parser.add_subparsers(dest='_subcommand')
                command_group = group_table.get(' '.join(parent_path), None)
                deprecate_info = None
                if command_group:
                    deprecate_info = command_group.group_kwargs.get('deprecate_info', None)
                parent_subparser.required = True
                parent_subparser.deprecate_info = deprecate_info
                self.subparsers[tuple(path[0:length])] = parent_subparser
        return parent_subparser