Remove a subparser from a subparsers group. This function is added by cmd2 as a method called ``remove_parser()`` to ``argparse._SubParsersAction`` class. To call: ``action.remove_parser(name)`` :param self: instance of the _SubParsersAction being edited :param name: name of t
( # noqa: N802
self: argparse._SubParsersAction, # type: ignore[type-arg]
name: str,
)
| 508 | |
| 509 | |
| 510 | def _SubParsersAction_remove_parser( # noqa: N802 |
| 511 | self: argparse._SubParsersAction, # type: ignore[type-arg] |
| 512 | name: str, |
| 513 | ) -> argparse.ArgumentParser: |
| 514 | """Remove a subparser from a subparsers group. |
| 515 | |
| 516 | This function is added by cmd2 as a method called ``remove_parser()`` |
| 517 | to ``argparse._SubParsersAction`` class. |
| 518 | |
| 519 | To call: ``action.remove_parser(name)`` |
| 520 | |
| 521 | :param self: instance of the _SubParsersAction being edited |
| 522 | :param name: name of the subcommand for the subparser to remove |
| 523 | :return: the removed parser |
| 524 | :raises ValueError: if the subcommand doesn't exist |
| 525 | """ |
| 526 | if name not in self._name_parser_map: |
| 527 | raise ValueError(f"Subcommand '{name}' does not exist") |
| 528 | |
| 529 | subparser = self._name_parser_map[name] |
| 530 | |
| 531 | # Find all names (primary and aliases) that map to this subparser |
| 532 | all_names = [cur_name for cur_name, cur_parser in self._name_parser_map.items() if cur_parser is subparser] |
| 533 | |
| 534 | # Remove the help entry for this subparser. To handle the case where |
| 535 | # name is an alias, we remove the action whose 'dest' matches any of |
| 536 | # the names mapped to this subparser. |
| 537 | for choice_action in self._choices_actions: |
| 538 | if choice_action.dest in all_names: |
| 539 | self._choices_actions.remove(choice_action) |
| 540 | break |
| 541 | |
| 542 | # Remove all references to this subparser, including aliases. |
| 543 | for cur_name in all_names: |
| 544 | del self._name_parser_map[cur_name] |
| 545 | |
| 546 | return cast(argparse.ArgumentParser, subparser) |
| 547 | |
| 548 | |
| 549 | argparse._SubParsersAction.remove_parser = _SubParsersAction_remove_parser # type: ignore[attr-defined] |
nothing calls this directly
no test coverage detected
searching dependent graphs…