MCPcopy Create free account
hub / github.com/python-cmd2/cmd2 / _SubParsersAction_remove_parser

Function _SubParsersAction_remove_parser

cmd2/argparse_utils.py:510–546  ·  view source on GitHub ↗

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,
)

Source from the content-addressed store, hash-verified

508
509
510def _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
549argparse._SubParsersAction.remove_parser = _SubParsersAction_remove_parser # type: ignore[attr-defined]

Callers

nothing calls this directly

Calls 1

removeMethod · 0.80

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…