summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTodd Leonhardt <todd.leonhardt@gmail.com>2019-06-23 20:49:27 -0400
committerTodd Leonhardt <todd.leonhardt@gmail.com>2019-06-23 20:49:27 -0400
commit652122f3c9907a652a9c3a14581bb2aef90bc996 (patch)
tree9bcb0c07c2e2cee26553a1e11a1d9dbcb7225954
parent181cecb217dd73056b72874d225e34528d484de8 (diff)
downloadcmd2-git-652122f3c9907a652a9c3a14581bb2aef90bc996.tar.gz
Made last_result public and restored the initialization of it in __init__ and associated comment
-rw-r--r--cmd2/cmd2.py4
-rw-r--r--cmd2/pyscript_bridge.py6
-rw-r--r--docs/argument_processing.rst2
-rwxr-xr-xexamples/python_scripting.py8
-rw-r--r--examples/scripts/conditional.py4
-rw-r--r--tests/test_cmd2.py24
6 files changed, 26 insertions, 22 deletions
diff --git a/cmd2/cmd2.py b/cmd2/cmd2.py
index 6a518978..d44e6df4 100644
--- a/cmd2/cmd2.py
+++ b/cmd2/cmd2.py
@@ -423,6 +423,10 @@ class Cmd(cmd.Cmd):
# True if running inside a Python script or interactive console, False otherwise
self._in_py = False
+ # Stores results from the last command run to enable usage of results in a Python script or interactive console
+ # Built-in commands don't make use of this. It is purely there for user-defined commands and convenience.
+ self.last_result = None
+
# Used by run_script command to store current script dir as a LIFO queue to support _relative_run_script command
self._script_dir = []
diff --git a/cmd2/pyscript_bridge.py b/cmd2/pyscript_bridge.py
index 01d283ea..ac3dfd40 100644
--- a/cmd2/pyscript_bridge.py
+++ b/cmd2/pyscript_bridge.py
@@ -23,7 +23,7 @@ class CommandResult(namedtuple_with_defaults('CommandResult', ['stdout', 'stderr
Any combination of these fields can be used when developing a scripting API for a given command.
By default stdout, stderr, and stop will be captured for you. If there is additional command specific data,
- then write that to cmd2's _last_result member. That becomes the data member of this tuple.
+ then write that to cmd2's last_result member. That becomes the data member of this tuple.
In some cases, the data member may contain everything needed for a command and storing stdout
and stderr might just be a duplication of data that wastes memory. In that case, the StdSim can
@@ -88,7 +88,7 @@ class PyscriptBridge(object):
# This will be used to capture sys.stderr
copy_stderr = StdSim(sys.stderr, echo)
- self._cmd2_app._last_result = None
+ self._cmd2_app.last_result = None
stop = False
try:
@@ -105,5 +105,5 @@ class PyscriptBridge(object):
result = CommandResult(stdout=copy_cmd_stdout.getvalue(),
stderr=copy_stderr.getvalue() if copy_stderr.getvalue() else None,
stop=stop,
- data=self._cmd2_app._last_result)
+ data=self._cmd2_app.last_result)
return result
diff --git a/docs/argument_processing.rst b/docs/argument_processing.rst
index 4bd917cf..599e4cf0 100644
--- a/docs/argument_processing.rst
+++ b/docs/argument_processing.rst
@@ -267,7 +267,7 @@ Here's what it looks like::
if unknown:
self.perror("dir does not take any positional arguments:", traceback_war=False)
self.do_help('dir')
- self._last_result = CommandResult('', 'Bad arguments')
+ self.last_result = CommandResult('', 'Bad arguments')
return
# Get the contents as a list
diff --git a/examples/python_scripting.py b/examples/python_scripting.py
index 3e8f64ef..c45648bc 100755
--- a/examples/python_scripting.py
+++ b/examples/python_scripting.py
@@ -58,7 +58,7 @@ class CmdLineApp(cmd2.Cmd):
if not arglist or len(arglist) != 1:
self.perror("cd requires exactly 1 argument:", traceback_war=False)
self.do_help('cd')
- self._last_result = cmd2.CommandResult('', 'Bad arguments')
+ self.last_result = cmd2.CommandResult('', 'Bad arguments')
return
# Convert relative paths to absolute paths
@@ -84,7 +84,7 @@ class CmdLineApp(cmd2.Cmd):
if err:
self.perror(err, traceback_war=False)
- self._last_result = cmd2.CommandResult(out, err, data)
+ self.last_result = cmd2.CommandResult(out, err, data)
# Enable tab completion for cd command
def complete_cd(self, text, line, begidx, endidx):
@@ -100,7 +100,7 @@ class CmdLineApp(cmd2.Cmd):
if unknown:
self.perror("dir does not take any positional arguments:", traceback_war=False)
self.do_help('dir')
- self._last_result = cmd2.CommandResult('', 'Bad arguments')
+ self.last_result = cmd2.CommandResult('', 'Bad arguments')
return
# Get the contents as a list
@@ -113,7 +113,7 @@ class CmdLineApp(cmd2.Cmd):
self.stdout.write(fmt.format(f))
self.stdout.write('\n')
- self._last_result = cmd2.CommandResult(data=contents)
+ self.last_result = cmd2.CommandResult(data=contents)
if __name__ == '__main__':
diff --git a/examples/scripts/conditional.py b/examples/scripts/conditional.py
index 724bb3ee..2e307cb4 100644
--- a/examples/scripts/conditional.py
+++ b/examples/scripts/conditional.py
@@ -27,10 +27,10 @@ original_dir = os.getcwd()
app('cd {}'.format(directory))
# Conditionally do something based on the results of the last command
-if self._last_result:
+if self.last_result:
print('\nContents of directory {!r}:'.format(directory))
app('dir -l')
- print('{}\n'.format(self._last_result.data))
+ print('{}\n'.format(self.last_result.data))
# Change back to where we were
print('Changing back to original directory: {!r}'.format(original_dir))
diff --git a/tests/test_cmd2.py b/tests/test_cmd2.py
index d4cf7a2a..9a5b2b47 100644
--- a/tests/test_cmd2.py
+++ b/tests/test_cmd2.py
@@ -1241,16 +1241,16 @@ class CommandResultApp(cmd2.Cmd):
super().__init__(*args, **kwargs)
def do_affirmative(self, arg):
- self._last_result = cmd2.CommandResult(arg, data=True)
+ self.last_result = cmd2.CommandResult(arg, data=True)
def do_negative(self, arg):
- self._last_result = cmd2.CommandResult(arg, data=False)
+ self.last_result = cmd2.CommandResult(arg, data=False)
def do_affirmative_no_data(self, arg):
- self._last_result = cmd2.CommandResult(arg)
+ self.last_result = cmd2.CommandResult(arg)
def do_negative_no_data(self, arg):
- self._last_result = cmd2.CommandResult('', arg)
+ self.last_result = cmd2.CommandResult('', arg)
@pytest.fixture
def commandresult_app():
@@ -1260,22 +1260,22 @@ def commandresult_app():
def test_commandresult_truthy(commandresult_app):
arg = 'foo'
run_cmd(commandresult_app, 'affirmative {}'.format(arg))
- assert commandresult_app._last_result
- assert commandresult_app._last_result == cmd2.CommandResult(arg, data=True)
+ assert commandresult_app.last_result
+ assert commandresult_app.last_result == cmd2.CommandResult(arg, data=True)
run_cmd(commandresult_app, 'affirmative_no_data {}'.format(arg))
- assert commandresult_app._last_result
- assert commandresult_app._last_result == cmd2.CommandResult(arg)
+ assert commandresult_app.last_result
+ assert commandresult_app.last_result == cmd2.CommandResult(arg)
def test_commandresult_falsy(commandresult_app):
arg = 'bar'
run_cmd(commandresult_app, 'negative {}'.format(arg))
- assert not commandresult_app._last_result
- assert commandresult_app._last_result == cmd2.CommandResult(arg, data=False)
+ assert not commandresult_app.last_result
+ assert commandresult_app.last_result == cmd2.CommandResult(arg, data=False)
run_cmd(commandresult_app, 'negative_no_data {}'.format(arg))
- assert not commandresult_app._last_result
- assert commandresult_app._last_result == cmd2.CommandResult('', arg)
+ assert not commandresult_app.last_result
+ assert commandresult_app.last_result == cmd2.CommandResult('', arg)
def test_is_text_file_bad_input(base_app):