@@ -190,7 +190,8 @@ def __init__(self,
190190 token_start_index : int = 1 ,
191191 arg_choices : Dict [str , Union [List , Tuple , Callable ]] = None ,
192192 subcmd_args_lookup : dict = None ,
193- tab_for_arg_help : bool = True ):
193+ tab_for_arg_help : bool = True ,
194+ cmd2_app = None ):
194195 """
195196 Create an AutoCompleter
196197
@@ -199,6 +200,8 @@ def __init__(self,
199200 :param arg_choices: dictionary mapping from argparse argument 'dest' name to list of choices
200201 :param subcmd_args_lookup: mapping a sub-command group name to a tuple to fill the child\
201202 AutoCompleter's arg_choices and subcmd_args_lookup parameters
203+ :param tab_for_arg_help: Enable of disable argument help when there's no completion result
204+ :param cmd2_app: reference to the Cmd2 application. Enables argparse argument completion with class methods
202205 """
203206 if not subcmd_args_lookup :
204207 subcmd_args_lookup = {}
@@ -209,6 +212,7 @@ def __init__(self,
209212 self ._arg_choices = arg_choices .copy () if arg_choices is not None else {}
210213 self ._token_start_index = token_start_index
211214 self ._tab_for_arg_help = tab_for_arg_help
215+ self ._cmd2_app = cmd2_app
212216
213217 self ._flags = [] # all flags in this command
214218 self ._flags_without_args = [] # all flags that don't take arguments
@@ -252,7 +256,8 @@ def __init__(self,
252256 subcmd_start = token_start_index + len (self ._positional_actions )
253257 sub_completers [subcmd ] = AutoCompleter (action .choices [subcmd ], subcmd_start ,
254258 arg_choices = subcmd_args ,
255- subcmd_args_lookup = subcmd_lookup )
259+ subcmd_args_lookup = subcmd_lookup ,
260+ cmd2_app = cmd2_app )
256261 sub_commands .append (subcmd )
257262 self ._positional_completers [action .dest ] = sub_completers
258263 self ._arg_choices [action .dest ] = sub_commands
@@ -492,7 +497,16 @@ def _resolve_choices_for_arg(self, action: argparse.Action, used_values=()) -> L
492497 args = self ._arg_choices [action .dest ]
493498
494499 if callable (args ):
495- args = args ()
500+ try :
501+ if self ._cmd2_app is not None :
502+ try :
503+ args = args (self ._cmd2_app )
504+ except TypeError :
505+ args = args ()
506+ else :
507+ args = args ()
508+ except TypeError :
509+ return []
496510
497511 try :
498512 iter (args )
0 commit comments