Skip to content

Commit d21ecf1

Browse files
committed
Refactored functions to use cmd2.cmd_func()
1 parent ed33f32 commit d21ecf1

File tree

1 file changed

+15
-15
lines changed

1 file changed

+15
-15
lines changed

cmd2/pyscript_bridge.py

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,10 @@ def __call__(self, *args, **kwargs):
179179

180180
def _run(self):
181181
# look up command function
182-
func = getattr(self._cmd2_app, 'do_' + self._command_name)
182+
func = self._cmd2_app.cmd_func(self._command_name)
183+
if func is None:
184+
raise AttributeError("{!r} object has no command called {!r}".format(self._cmd2_app.__class__.__name__,
185+
self._command_name))
183186

184187
# reconstruct the cmd2 command from the python call
185188
cmd_str = ['']
@@ -248,23 +251,20 @@ def __init__(self, cmd2_app):
248251

249252
def __getattr__(self, item: str):
250253
"""Check if the attribute is a command. If so, return a callable."""
251-
commands = self._cmd2_app.get_all_commands()
252-
if item in commands:
253-
func = getattr(self._cmd2_app, 'do_' + item)
254-
255-
try:
256-
# See if the command uses argparse
257-
parser = getattr(func, 'argparser')
258-
except AttributeError:
259-
# Command doesn't, we will accept parameters in the form of a command string
254+
func = self._cmd2_app.cmd_func(item)
255+
256+
if func:
257+
if hasattr(func, 'argparser'):
258+
# Command uses argparse, return an object that can traverse the argparse subcommands and arguments
259+
return ArgparseFunctor(self.cmd_echo, self._cmd2_app, item, getattr(func, 'argparser'))
260+
else:
261+
# Command doesn't use argparse, we will accept parameters in the form of a command string
260262
def wrap_func(args=''):
261263
return _exec_cmd(self._cmd2_app, functools.partial(func, args), self.cmd_echo)
262-
return wrap_func
263-
else:
264-
# Command does use argparse, return an object that can traverse the argparse subcommands and arguments
265-
return ArgparseFunctor(self.cmd_echo, self._cmd2_app, item, parser)
266264

267-
return super().__getattr__(item)
265+
return wrap_func
266+
else:
267+
return super().__getattr__(item)
268268

269269
def __dir__(self):
270270
"""Return a custom set of attribute names to match the available commands"""

0 commit comments

Comments
 (0)