@@ -207,6 +207,13 @@ def my_completer(self, text, line, begidx, endidx, arg_tokens)
207207- ``argparse.Action.set_suppress_tab_hint()`` - See
208208 :func:`_action_set_suppress_tab_hint` for more details.
209209
210+ cmd2 has patched ``argparse.ArgumentParser`` to include the following accessor methods
211+
212+ - ``argparse.ArgumentParser.get_ap_completer_type()`` - See
213+ :func:`_ArgumentParser_get_ap_completer_type` for more details.
214+ - ``argparse.Action.set_ap_completer_type()`` - See
215+ :func:`_ArgumentParser_set_ap_completer_type` for more details.
216+
210217**Subcommand removal**
211218
212219cmd2 has patched ``argparse._SubParsersAction`` to include a ``remove_parser()``
@@ -232,6 +239,7 @@ def my_completer(self, text, line, begidx, endidx, arg_tokens)
232239)
233240from typing import (
234241 IO ,
242+ TYPE_CHECKING ,
235243 Any ,
236244 Callable ,
237245 Dict ,
@@ -264,6 +272,12 @@ def my_completer(self, text, line, begidx, endidx, arg_tokens)
264272 )
265273
266274
275+ if TYPE_CHECKING : # pragma: no cover
276+ from .argparse_completer import (
277+ ArgparseCompleter ,
278+ )
279+
280+
267281def generate_range_error (range_min : int , range_max : Union [int , float ]) -> str :
268282 """Generate an error message when the the number of arguments provided is not within the expected range"""
269283 err_str = "expected "
@@ -659,6 +673,7 @@ def register_argparse_argument_parameter(param_name: str, param_type: Optional[T
659673 and ``set_{param_name}(value)``.
660674
661675 :param param_name: Name of the parameter to add.
676+ :param param_type: Type of the parameter to add.
662677 """
663678 attr_name = f'{ _CUSTOM_ATTRIB_PFX } { param_name } '
664679 if param_name in CUSTOM_ACTION_ATTRIBS or hasattr (argparse .Action , attr_name ):
@@ -715,6 +730,7 @@ def _action_set_custom_parameter(self: argparse.Action, value: Any) -> None:
715730orig_actions_container_add_argument = argparse ._ActionsContainer .add_argument
716731
717732
733+ # noinspection PyProtectedMember
718734def _add_argument_wrapper (
719735 self : argparse ._ActionsContainer ,
720736 * args : Any ,
@@ -916,10 +932,54 @@ def _match_argument_wrapper(self: argparse.ArgumentParser, action: argparse.Acti
916932
917933
918934############################################################################################################
919- # Patch argparse._SubParsersAction to add remove_parser function
935+ # Patch argparse.ArgumentParser with accessors for ap_completer_type attribute
920936############################################################################################################
921937
938+ # An ArgumentParser attribute which specifies a subclass of ArgparseCompleter for custom tab completion behavior on a
939+ # given parser. If this is None or not present, then cmd2 will use argparse_completer.DEFAULT_AP_COMPLETER when tab
940+ # completing a parser's arguments
941+ ATTR_AP_COMPLETER_TYPE = 'ap_completer_type'
942+
943+
922944# noinspection PyPep8Naming
945+ def _ArgumentParser_get_ap_completer_type (self : argparse .ArgumentParser ) -> Optional [Type ['ArgparseCompleter' ]]:
946+ """
947+ Get the ap_completer_type attribute of an argparse ArgumentParser.
948+
949+ This function is added by cmd2 as a method called ``get_ap_completer_type()`` to ``argparse.ArgumentParser`` class.
950+
951+ To call: ``parser.get_ap_completer_type()``
952+
953+ :param self: ArgumentParser being queried
954+ :return: An ArgparseCompleter-based class or None if attribute does not exist
955+ """
956+ return cast (Optional [Type ['ArgparseCompleter' ]], getattr (self , ATTR_AP_COMPLETER_TYPE , None ))
957+
958+
959+ setattr (argparse .ArgumentParser , 'get_ap_completer_type' , _ArgumentParser_get_ap_completer_type )
960+
961+
962+ # noinspection PyPep8Naming
963+ def _ArgumentParser_set_ap_completer_type (self : argparse .ArgumentParser , ap_completer_type : Type ['ArgparseCompleter' ]) -> None :
964+ """
965+ Set the ap_completer_type attribute of an argparse ArgumentParser.
966+
967+ This function is added by cmd2 as a method called ``set_ap_completer_type()`` to ``argparse.ArgumentParser`` class.
968+
969+ :param self: ArgumentParser being edited
970+ :param ap_completer_type: the custom ArgparseCompleter-based class to use when tab completing arguments for this parser
971+ """
972+ setattr (self , ATTR_AP_COMPLETER_TYPE , ap_completer_type )
973+
974+
975+ setattr (argparse .ArgumentParser , 'set_ap_completer_type' , _ArgumentParser_set_ap_completer_type )
976+
977+
978+ ############################################################################################################
979+ # Patch argparse._SubParsersAction to add remove_parser function
980+ ############################################################################################################
981+
982+ # noinspection PyPep8Naming,PyProtectedMember
923983def _SubParsersAction_remove_parser (self : argparse ._SubParsersAction , name : str ) -> None :
924984 """
925985 Removes a sub-parser from a sub-parsers group. Used to remove subcommands from a parser.
@@ -964,6 +1024,7 @@ def _SubParsersAction_remove_parser(self: argparse._SubParsersAction, name: str)
9641024class Cmd2HelpFormatter (argparse .RawTextHelpFormatter ):
9651025 """Custom help formatter to configure ordering of help text"""
9661026
1027+ # noinspection PyProtectedMember
9671028 def _format_usage (
9681029 self ,
9691030 usage : Optional [str ],
@@ -1207,6 +1268,7 @@ def __init__(
12071268 allow_abbrev = allow_abbrev ,
12081269 )
12091270
1271+ # noinspection PyProtectedMember
12101272 def add_subparsers (self , ** kwargs : Any ) -> argparse ._SubParsersAction :
12111273 """
12121274 Custom override. Sets a default title if one was not given.
@@ -1321,10 +1383,10 @@ def set(self, new_val: Any) -> None:
13211383DEFAULT_ARGUMENT_PARSER : Type [argparse .ArgumentParser ] = Cmd2ArgumentParser
13221384
13231385
1324- def set_default_argument_parser ( parser : Type [argparse .ArgumentParser ]) -> None :
1386+ def set_default_argument_parser_type ( parser_type : Type [argparse .ArgumentParser ]) -> None :
13251387 """
13261388 Set the default ArgumentParser class for a cmd2 app. This must be called prior to loading cmd2.py if
13271389 you want to override the parser for cmd2's built-in commands. See examples/override_parser.py.
13281390 """
13291391 global DEFAULT_ARGUMENT_PARSER
1330- DEFAULT_ARGUMENT_PARSER = parser
1392+ DEFAULT_ARGUMENT_PARSER = parser_type
0 commit comments