@@ -890,7 +890,7 @@ def _find_parser(self, subcommand_path: Iterable[str]) -> 'Cmd2ArgumentParser':
890890 """Find a parser in the hierarchy based on a sequence of subcommand names.
891891
892892 :param subcommand_path: sequence of subcommand names leading to the target parser
893- :return: the discovered Cmd2ArgumentParser
893+ :return: the discovered parser
894894 :raises ValueError: if any subcommand in the path is not found or a level doesn't support subcommands
895895 """
896896 parser = self
@@ -905,34 +905,43 @@ def attach_subcommand(
905905 self ,
906906 subcommand_path : Iterable [str ],
907907 subcommand : str ,
908- parser : 'Cmd2ArgumentParser' ,
908+ subcommand_parser : 'Cmd2ArgumentParser' ,
909909 ** add_parser_kwargs : Any ,
910910 ) -> None :
911911 """Attach a parser as a subcommand to a command at the specified path.
912912
913913 :param subcommand_path: sequence of subcommand names leading to the parser that will
914914 host the new subcommand. An empty sequence indicates this parser.
915915 :param subcommand: name of the new subcommand
916- :param parser : the parser to attach
916+ :param subcommand_parser : the parser to attach
917917 :param add_parser_kwargs: additional arguments for the subparser registration (e.g. help, aliases)
918+ :raises TypeError: if the subcommand parser's type does not match the 'parser_class' configured
919+ for the target subcommand group.
918920 :raises ValueError: if the command path is invalid or doesn't support subcommands
919921 """
920922 target_parser = self ._find_parser (subcommand_path )
921923 subparsers_action = target_parser ._get_subparsers_action ()
922924
925+ if type (subcommand_parser ) is not subparsers_action ._parser_class :
926+ raise TypeError (
927+ f"The attached parser must be of type '{ subparsers_action ._parser_class .__name__ } ' "
928+ f"to match the 'parser_class' configured for this subparsers action. "
929+ f"Received '{ type (subcommand_parser ).__name__ } '."
930+ )
931+
923932 # Use add_parser to register the subcommand name and any aliases
924- new_parser = subparsers_action .add_parser (subcommand , ** add_parser_kwargs )
933+ placeholder_parser = subparsers_action .add_parser (subcommand , ** add_parser_kwargs )
925934
926935 # To ensure accurate usage strings, recursively update 'prog' values
927936 # within the injected parser to match its new location in the command hierarchy.
928- parser .update_prog (new_parser .prog )
937+ subcommand_parser .update_prog (placeholder_parser .prog )
929938
930939 # Replace the parser created by add_parser() with our pre-configured one
931- subparsers_action ._name_parser_map [subcommand ] = parser
940+ subparsers_action ._name_parser_map [subcommand ] = subcommand_parser
932941
933942 # Remap any aliases to our pre-configured parser
934943 for alias in add_parser_kwargs .get ("aliases" , ()):
935- subparsers_action ._name_parser_map [alias ] = parser
944+ subparsers_action ._name_parser_map [alias ] = subcommand_parser
936945
937946 def detach_subcommand (self , subcommand_path : Iterable [str ], subcommand : str ) -> 'Cmd2ArgumentParser' :
938947 """Detach a subcommand from a command at the specified path.
0 commit comments