Build argument parser for a command/subcommand. :param parent: object which owns the command using the parser. When parser_builder is a classmethod, this function passes parent's class to it. :param parser_builder: an existing Cmd2Argume
(
self,
parent: CmdOrSet,
parser_builder: Cmd2ArgumentParser | Callable[[], Cmd2ArgumentParser] | StaticArgParseBuilder | ClassArgParseBuilder,
)
| 911 | raise |
| 912 | |
| 913 | def _build_parser( |
| 914 | self, |
| 915 | parent: CmdOrSet, |
| 916 | parser_builder: Cmd2ArgumentParser | Callable[[], Cmd2ArgumentParser] | StaticArgParseBuilder | ClassArgParseBuilder, |
| 917 | ) -> Cmd2ArgumentParser: |
| 918 | """Build argument parser for a command/subcommand. |
| 919 | |
| 920 | :param parent: object which owns the command using the parser. |
| 921 | When parser_builder is a classmethod, this function passes |
| 922 | parent's class to it. |
| 923 | :param parser_builder: an existing Cmd2ArgumentParser instance or a factory |
| 924 | (callable, staticmethod, or classmethod) that returns one. |
| 925 | :return: new parser |
| 926 | :raises TypeError: if parser_builder is an invalid type or if the factory fails |
| 927 | to return a Cmd2ArgumentParser |
| 928 | """ |
| 929 | if isinstance(parser_builder, Cmd2ArgumentParser): |
| 930 | parser = copy.deepcopy(parser_builder) |
| 931 | else: |
| 932 | # Try to build the parser with a factory |
| 933 | if isinstance(parser_builder, staticmethod): |
| 934 | parser = parser_builder.__func__() |
| 935 | elif isinstance(parser_builder, classmethod): |
| 936 | parser = parser_builder.__func__(parent.__class__) |
| 937 | elif callable(parser_builder): |
| 938 | parser = parser_builder() |
| 939 | else: |
| 940 | raise TypeError(f"Invalid type for parser_builder: {type(parser_builder)}") |
| 941 | |
| 942 | # Verify the factory returned the required type |
| 943 | if not isinstance(parser, Cmd2ArgumentParser): |
| 944 | builder_name = getattr(parser_builder, "__name__", str(parser_builder)) # type: ignore[unreachable] |
| 945 | raise TypeError(f"The parser returned by '{builder_name}' must be a Cmd2ArgumentParser or a subclass of it") |
| 946 | |
| 947 | return parser |
| 948 | |
| 949 | def _install_command_function(self, command_func_name: str, command_method: BoundCommandFunc, context: str = "") -> None: |
| 950 | """Install a new command function into the CLI. |
no outgoing calls