1010import argparse
1111import functools
1212import sys
13- from typing import List , Callable
13+ from typing import List , Callable , Optional
1414
1515from .argparse_completer import _RangeAction
1616from .utils import namedtuple_with_defaults , StdSim
@@ -38,7 +38,7 @@ def __bool__(self):
3838 return not self .stderr and self .data is not None
3939
4040
41- def _exec_cmd (cmd2_app , func : Callable , echo : bool ):
41+ def _exec_cmd (cmd2_app , func : Callable , echo : bool ) -> CommandResult :
4242 """Helper to encapsulate executing a command and capturing the results"""
4343 copy_stdout = StdSim (sys .stdout , echo )
4444 copy_stderr = StdSim (sys .stderr , echo )
@@ -65,7 +65,7 @@ def _exec_cmd(cmd2_app, func: Callable, echo: bool):
6565
6666class ArgparseFunctor :
6767 """
68- Encapsulates translating python object traversal
68+ Encapsulates translating Python object traversal
6969 """
7070 def __init__ (self , echo : bool , cmd2_app , command_name : str , parser : argparse .ArgumentParser ):
7171 self ._echo = echo
@@ -250,7 +250,7 @@ def __init__(self, cmd2_app):
250250 self .cmd_echo = False
251251
252252 def __getattr__ (self , item : str ):
253- """Check if the attribute is a command. If so , return a callable."""
253+ """If attribute is a command, return a callable. Otherwise return the attribute ."""
254254 func = self ._cmd2_app .cmd_func (item )
255255
256256 if func :
@@ -264,14 +264,23 @@ def wrap_func(args=''):
264264
265265 return wrap_func
266266 else :
267- return super (). __getattr__ ( item )
267+ return getattr ( self . _cmd2_app , item )
268268
269269 def __dir__ (self ):
270270 """Return a custom set of attribute names to match the available commands"""
271271 commands = list (self ._cmd2_app .get_all_commands ())
272272 commands .insert (0 , 'cmd_echo' )
273273 return commands
274274
275- def __call__ (self , args : str ):
275+ def __call__ (self , args : str , echo : Optional [bool ]= None ) -> CommandResult :
276+ """
277+ Call a command function (ex: do_help)
278+ :param args: The string being passed to the command
279+ :param echo: If True, output will be echoed while the command runs
280+ This temporarily overrides the value of self.cmd_echo
281+ """
282+ if echo is None :
283+ echo = self .cmd_echo
284+
276285 return _exec_cmd (self ._cmd2_app , functools .partial (self ._cmd2_app .onecmd_plus_hooks , args + '\n ' ),
277- self . cmd_echo )
286+ echo )
0 commit comments