@@ -207,6 +207,13 @@ def my_completer(self, text, line, begidx, endidx, arg_tokens)
207
207
- ``argparse.Action.set_suppress_tab_hint()`` - See
208
208
:func:`_action_set_suppress_tab_hint` for more details.
209
209
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
+
210
217
**Subcommand removal**
211
218
212
219
cmd2 has patched ``argparse._SubParsersAction`` to include a ``remove_parser()``
@@ -232,6 +239,7 @@ def my_completer(self, text, line, begidx, endidx, arg_tokens)
232
239
)
233
240
from typing import (
234
241
IO ,
242
+ TYPE_CHECKING ,
235
243
Any ,
236
244
Callable ,
237
245
Dict ,
@@ -264,6 +272,12 @@ def my_completer(self, text, line, begidx, endidx, arg_tokens)
264
272
)
265
273
266
274
275
+ if TYPE_CHECKING : # pragma: no cover
276
+ from .argparse_completer import (
277
+ ArgparseCompleter ,
278
+ )
279
+
280
+
267
281
def generate_range_error (range_min : int , range_max : Union [int , float ]) -> str :
268
282
"""Generate an error message when the the number of arguments provided is not within the expected range"""
269
283
err_str = "expected "
@@ -659,6 +673,7 @@ def register_argparse_argument_parameter(param_name: str, param_type: Optional[T
659
673
and ``set_{param_name}(value)``.
660
674
661
675
:param param_name: Name of the parameter to add.
676
+ :param param_type: Type of the parameter to add.
662
677
"""
663
678
attr_name = f'{ _CUSTOM_ATTRIB_PFX } { param_name } '
664
679
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:
715
730
orig_actions_container_add_argument = argparse ._ActionsContainer .add_argument
716
731
717
732
733
+ # noinspection PyProtectedMember
718
734
def _add_argument_wrapper (
719
735
self : argparse ._ActionsContainer ,
720
736
* args : Any ,
@@ -916,10 +932,54 @@ def _match_argument_wrapper(self: argparse.ArgumentParser, action: argparse.Acti
916
932
917
933
918
934
############################################################################################################
919
- # Patch argparse._SubParsersAction to add remove_parser function
935
+ # Patch argparse.ArgumentParser with accessors for ap_completer_type attribute
920
936
############################################################################################################
921
937
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
+
922
944
# 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
923
983
def _SubParsersAction_remove_parser (self : argparse ._SubParsersAction , name : str ) -> None :
924
984
"""
925
985
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)
964
1024
class Cmd2HelpFormatter (argparse .RawTextHelpFormatter ):
965
1025
"""Custom help formatter to configure ordering of help text"""
966
1026
1027
+ # noinspection PyProtectedMember
967
1028
def _format_usage (
968
1029
self ,
969
1030
usage : Optional [str ],
@@ -1207,6 +1268,7 @@ def __init__(
1207
1268
allow_abbrev = allow_abbrev ,
1208
1269
)
1209
1270
1271
+ # noinspection PyProtectedMember
1210
1272
def add_subparsers (self , ** kwargs : Any ) -> argparse ._SubParsersAction :
1211
1273
"""
1212
1274
Custom override. Sets a default title if one was not given.
@@ -1321,10 +1383,10 @@ def set(self, new_val: Any) -> None:
1321
1383
DEFAULT_ARGUMENT_PARSER : Type [argparse .ArgumentParser ] = Cmd2ArgumentParser
1322
1384
1323
1385
1324
- def set_default_argument_parser ( parser : Type [argparse .ArgumentParser ]) -> None :
1386
+ def set_default_argument_parser_type ( parser_type : Type [argparse .ArgumentParser ]) -> None :
1325
1387
"""
1326
1388
Set the default ArgumentParser class for a cmd2 app. This must be called prior to loading cmd2.py if
1327
1389
you want to override the parser for cmd2's built-in commands. See examples/override_parser.py.
1328
1390
"""
1329
1391
global DEFAULT_ARGUMENT_PARSER
1330
- DEFAULT_ARGUMENT_PARSER = parser
1392
+ DEFAULT_ARGUMENT_PARSER = parser_type
0 commit comments