@@ -231,6 +231,10 @@ def get_choices(self) -> Choices:
231231sub-parser from a sub-parsers group. See _SubParsersAction_remove_parser for
232232more details.
233233
234+ ``argparse._SubParsersAction.add_existing_parser`` - new function which allows you to attach
235+ an existing ArgumentParser to a sub-parsers group. See _SubParsersAction_add_existing_parser
236+ for more details.
237+
234238**Added accessor methods**
235239
236240cmd2 has patched ``argparse.Action`` to include the following accessor methods
@@ -948,7 +952,10 @@ def _ArgumentParser_check_value(_self: argparse.ArgumentParser, action: argparse
948952############################################################################################################
949953
950954
951- def _SubParsersAction_remove_parser (self : argparse ._SubParsersAction , name : str ) -> None : # type: ignore[type-arg] # noqa: N802
955+ def _SubParsersAction_remove_parser ( # noqa: N802
956+ self : argparse ._SubParsersAction , # type: ignore[type-arg]
957+ name : str ,
958+ ) -> None :
952959 """Remove a sub-parser from a sub-parsers group. Used to remove subcommands from a parser.
953960
954961 This function is added by cmd2 as a method called ``remove_parser()`` to ``argparse._SubParsersAction`` class.
@@ -977,6 +984,42 @@ def _SubParsersAction_remove_parser(self: argparse._SubParsersAction, name: str)
977984
978985setattr (argparse ._SubParsersAction , 'remove_parser' , _SubParsersAction_remove_parser )
979986
987+ ############################################################################################################
988+ # Patch argparse._SubParsersAction to add add_existing_parser function
989+ ############################################################################################################
990+
991+
992+ def _SubParsersAction_add_existing_parser ( # noqa: N802
993+ self : argparse ._SubParsersAction , # type: ignore[type-arg]
994+ name : str ,
995+ subcmd_parser : argparse .ArgumentParser ,
996+ ** add_parser_kwargs : Any ,
997+ ) -> None :
998+ """Attach an existing ArgumentParser to a sub-parsers group.
999+
1000+ This is useful when a parser is pre-configured (e.g. by cmd2's subcommand decorator)
1001+ and needs to be attached to a parent parser.
1002+
1003+ This function is added by cmd2 as a method called ``add_existing_parser()``
1004+ to ``argparse._SubParsersAction`` class.
1005+
1006+ :param self: instance of the _SubParsersAction being edited
1007+ :param name: name of the subcommand to add
1008+ :param subcmd_parser: the parser for this new subcommand
1009+ :param add_parser_kwargs: registration-specific kwargs for add_parser() (e.g. help, aliases, deprecated)
1010+ """
1011+ # Use add_parser to register the subcommand name and any aliases
1012+ self .add_parser (name , ** add_parser_kwargs )
1013+
1014+ # Replace the parser created by add_parser() with our pre-configured one
1015+ self ._name_parser_map [name ] = subcmd_parser
1016+
1017+ # Remap any aliases to our pre-configured parser
1018+ for alias in add_parser_kwargs .get ("aliases" , []):
1019+ self ._name_parser_map [alias ] = subcmd_parser
1020+
1021+
1022+ setattr (argparse ._SubParsersAction , 'add_existing_parser' , _SubParsersAction_add_existing_parser )
9801023
9811024############################################################################################################
9821025# Unless otherwise noted, everything below this point are copied from Python's
0 commit comments