diff options
author | Kevin Van Brunt <kmvanbrunt@gmail.com> | 2018-10-05 15:46:35 -0400 |
---|---|---|
committer | Kevin Van Brunt <kmvanbrunt@gmail.com> | 2018-10-05 15:46:35 -0400 |
commit | c480f3a2e940757a6698f9270db503ebf4111fd7 (patch) | |
tree | 67f1af30a0361be162587cc3c988dccdd72bdfaa /cmd2/pyscript_bridge.py | |
parent | 9fee6105210b36201b5d2edc610f20bd67eac9f2 (diff) | |
download | cmd2-git-c480f3a2e940757a6698f9270db503ebf4111fd7.tar.gz |
No longer using stderr and self.data together to determine truthiness of a CommandResult.
Either self.data is used or if that's not possible, then self.stderr is used.
This provided applications the ability to print to stderr even if an error didn't occur.
Diffstat (limited to 'cmd2/pyscript_bridge.py')
-rw-r--r-- | cmd2/pyscript_bridge.py | 13 |
1 files changed, 10 insertions, 3 deletions
diff --git a/cmd2/pyscript_bridge.py b/cmd2/pyscript_bridge.py index 2002ca6d..dcfd9116 100644 --- a/cmd2/pyscript_bridge.py +++ b/cmd2/pyscript_bridge.py @@ -33,9 +33,16 @@ class CommandResult(namedtuple_with_defaults('CommandResult', ['stdout', 'stderr NOTE: Named tuples are immutable. So the contents are there for access, not for modification. """ - def __bool__(self): - """If stderr is None and data is not None the command is considered a success""" - return not self.stderr and self.data is not None + def __bool__(self) -> bool: + """Returns True if the command succeeded, otherwise False""" + + # If data has a __bool__ method, then call it to determine success of command + if self.data is not None and callable(getattr(self.data, '__bool__', None)): + return True if self.data else False + + # Otherwise check if stderr was filled out + else: + return not self.stderr def _exec_cmd(cmd2_app, func: Callable, echo: bool) -> CommandResult: |