Tokenize a command string and resolve the associated root parser and relative subcommand path. This helper handles the initial resolution of a command string (e.g., 'foo bar baz') by identifying 'foo' as the root command, retrieving its associated parser, and returning any r
(self, command: str)
| 1166 | self.detach_subcommand(full_command_name, subcommand_name) |
| 1167 | |
| 1168 | def get_root_parser_and_subcmd_path(self, command: str) -> tuple[Cmd2ArgumentParser, list[str]]: |
| 1169 | """Tokenize a command string and resolve the associated root parser and relative subcommand path. |
| 1170 | |
| 1171 | This helper handles the initial resolution of a command string (e.g., 'foo bar baz') by |
| 1172 | identifying 'foo' as the root command, retrieving its associated parser, and returning |
| 1173 | any remaining tokens (['bar', 'baz']) as a path relative to that parser for further traversal. |
| 1174 | |
| 1175 | :param command: full space-delimited command path leading to a parser (e.g. 'foo' or 'foo bar') |
| 1176 | :return: a tuple containing the Cmd2ArgumentParser for the root command and a list of |
| 1177 | strings representing the relative path to the desired hosting parser. |
| 1178 | :raises ValueError: if the command is empty, the root command is not found, or |
| 1179 | the root command does not use an argparse parser. |
| 1180 | """ |
| 1181 | tokens = command.split() |
| 1182 | if not tokens: |
| 1183 | raise ValueError("Command path cannot be empty") |
| 1184 | |
| 1185 | root_command = tokens[0] |
| 1186 | subcommand_path = tokens[1:] |
| 1187 | |
| 1188 | # Search for the base command function and verify it has an argparser defined |
| 1189 | command_func = self.get_command_func(root_command) |
| 1190 | if command_func is None: |
| 1191 | raise ValueError(f"Root command '{root_command}' does not exist") |
| 1192 | |
| 1193 | root_parser = self.command_parsers.get(command_func) |
| 1194 | if root_parser is None: |
| 1195 | raise ValueError(f"Command '{root_command}' does not use argparse") |
| 1196 | |
| 1197 | return root_parser, subcommand_path |
| 1198 | |
| 1199 | def attach_subcommand( |
| 1200 | self, |
no test coverage detected