summaryrefslogtreecommitdiff
path: root/cmd2
diff options
context:
space:
mode:
authorKevin Van Brunt <kmvanbrunt@gmail.com>2018-09-27 20:01:02 -0400
committerKevin Van Brunt <kmvanbrunt@gmail.com>2018-09-27 20:01:02 -0400
commit74129347da01bae550a56f3ecc09be7aec64d5c8 (patch)
treef469a7a3ea264c05effc3560a826275cc641c1a2 /cmd2
parentb79790d5e8259376cbf0201d1d69f9895492aeef (diff)
downloadcmd2-git-74129347da01bae550a56f3ecc09be7aec64d5c8.tar.gz
Made _func_named() more reliable
Added unit tests
Diffstat (limited to 'cmd2')
-rw-r--r--cmd2/cmd2.py25
1 files changed, 9 insertions, 16 deletions
diff --git a/cmd2/cmd2.py b/cmd2/cmd2.py
index 09fec115..62c188b4 100644
--- a/cmd2/cmd2.py
+++ b/cmd2/cmd2.py
@@ -2009,11 +2009,8 @@ class Cmd(cmd.Cmd):
:param arg: command to look up method name which implements it
:return: method name which implements the given command
"""
- result = None
target = 'do_' + arg
- if target in dir(self):
- result = target
- return result
+ return target if callable(getattr(self, target, None)) else ''
def onecmd(self, statement: Union[Statement, str]) -> bool:
""" This executes the actual do_* method for a command.
@@ -2033,21 +2030,17 @@ class Cmd(cmd.Cmd):
stop = self._run_macro(statement)
else:
funcname = self._func_named(statement.command)
- if not funcname:
- self.default(statement)
- return False
+ if funcname:
+ func = getattr(self, funcname)
+ stop = func(statement)
- # Since we have a valid command store it in the history
- if statement.command not in self.exclude_from_history:
- self.history.append(statement.raw)
+ # Since we have a valid command store it in the history
+ if statement.command not in self.exclude_from_history:
+ self.history.append(statement.raw)
- try:
- func = getattr(self, funcname)
- except AttributeError:
+ else:
self.default(statement)
- return False
-
- stop = func(statement)
+ stop = False
return stop