Skip to content

Commit 652122f

Browse files
committed
Made last_result public and restored the initialization of it in __init__ and associated comment
1 parent 181cecb commit 652122f

File tree

6 files changed

+26
-22
lines changed

6 files changed

+26
-22
lines changed

cmd2/cmd2.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -423,6 +423,10 @@ def __init__(self, completekey: str = 'tab', stdin=None, stdout=None, *,
423423
# True if running inside a Python script or interactive console, False otherwise
424424
self._in_py = False
425425

426+
# Stores results from the last command run to enable usage of results in a Python script or interactive console
427+
# Built-in commands don't make use of this. It is purely there for user-defined commands and convenience.
428+
self.last_result = None
429+
426430
# Used by run_script command to store current script dir as a LIFO queue to support _relative_run_script command
427431
self._script_dir = []
428432

cmd2/pyscript_bridge.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class CommandResult(namedtuple_with_defaults('CommandResult', ['stdout', 'stderr
2323
2424
Any combination of these fields can be used when developing a scripting API for a given command.
2525
By default stdout, stderr, and stop will be captured for you. If there is additional command specific data,
26-
then write that to cmd2's _last_result member. That becomes the data member of this tuple.
26+
then write that to cmd2's last_result member. That becomes the data member of this tuple.
2727
2828
In some cases, the data member may contain everything needed for a command and storing stdout
2929
and stderr might just be a duplication of data that wastes memory. In that case, the StdSim can
@@ -88,7 +88,7 @@ def __call__(self, command: str, echo: Optional[bool] = None) -> CommandResult:
8888
# This will be used to capture sys.stderr
8989
copy_stderr = StdSim(sys.stderr, echo)
9090

91-
self._cmd2_app._last_result = None
91+
self._cmd2_app.last_result = None
9292

9393
stop = False
9494
try:
@@ -105,5 +105,5 @@ def __call__(self, command: str, echo: Optional[bool] = None) -> CommandResult:
105105
result = CommandResult(stdout=copy_cmd_stdout.getvalue(),
106106
stderr=copy_stderr.getvalue() if copy_stderr.getvalue() else None,
107107
stop=stop,
108-
data=self._cmd2_app._last_result)
108+
data=self._cmd2_app.last_result)
109109
return result

docs/argument_processing.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ Here's what it looks like::
267267
if unknown:
268268
self.perror("dir does not take any positional arguments:", traceback_war=False)
269269
self.do_help('dir')
270-
self._last_result = CommandResult('', 'Bad arguments')
270+
self.last_result = CommandResult('', 'Bad arguments')
271271
return
272272

273273
# Get the contents as a list

examples/python_scripting.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ def do_cd(self, arglist):
5858
if not arglist or len(arglist) != 1:
5959
self.perror("cd requires exactly 1 argument:", traceback_war=False)
6060
self.do_help('cd')
61-
self._last_result = cmd2.CommandResult('', 'Bad arguments')
61+
self.last_result = cmd2.CommandResult('', 'Bad arguments')
6262
return
6363

6464
# Convert relative paths to absolute paths
@@ -84,7 +84,7 @@ def do_cd(self, arglist):
8484

8585
if err:
8686
self.perror(err, traceback_war=False)
87-
self._last_result = cmd2.CommandResult(out, err, data)
87+
self.last_result = cmd2.CommandResult(out, err, data)
8888

8989
# Enable tab completion for cd command
9090
def complete_cd(self, text, line, begidx, endidx):
@@ -100,7 +100,7 @@ def do_dir(self, args, unknown):
100100
if unknown:
101101
self.perror("dir does not take any positional arguments:", traceback_war=False)
102102
self.do_help('dir')
103-
self._last_result = cmd2.CommandResult('', 'Bad arguments')
103+
self.last_result = cmd2.CommandResult('', 'Bad arguments')
104104
return
105105

106106
# Get the contents as a list
@@ -113,7 +113,7 @@ def do_dir(self, args, unknown):
113113
self.stdout.write(fmt.format(f))
114114
self.stdout.write('\n')
115115

116-
self._last_result = cmd2.CommandResult(data=contents)
116+
self.last_result = cmd2.CommandResult(data=contents)
117117

118118

119119
if __name__ == '__main__':

examples/scripts/conditional.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,10 @@
2727
app('cd {}'.format(directory))
2828

2929
# Conditionally do something based on the results of the last command
30-
if self._last_result:
30+
if self.last_result:
3131
print('\nContents of directory {!r}:'.format(directory))
3232
app('dir -l')
33-
print('{}\n'.format(self._last_result.data))
33+
print('{}\n'.format(self.last_result.data))
3434

3535
# Change back to where we were
3636
print('Changing back to original directory: {!r}'.format(original_dir))

tests/test_cmd2.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1241,16 +1241,16 @@ def __init__(self, *args, **kwargs):
12411241
super().__init__(*args, **kwargs)
12421242

12431243
def do_affirmative(self, arg):
1244-
self._last_result = cmd2.CommandResult(arg, data=True)
1244+
self.last_result = cmd2.CommandResult(arg, data=True)
12451245

12461246
def do_negative(self, arg):
1247-
self._last_result = cmd2.CommandResult(arg, data=False)
1247+
self.last_result = cmd2.CommandResult(arg, data=False)
12481248

12491249
def do_affirmative_no_data(self, arg):
1250-
self._last_result = cmd2.CommandResult(arg)
1250+
self.last_result = cmd2.CommandResult(arg)
12511251

12521252
def do_negative_no_data(self, arg):
1253-
self._last_result = cmd2.CommandResult('', arg)
1253+
self.last_result = cmd2.CommandResult('', arg)
12541254

12551255
@pytest.fixture
12561256
def commandresult_app():
@@ -1260,22 +1260,22 @@ def commandresult_app():
12601260
def test_commandresult_truthy(commandresult_app):
12611261
arg = 'foo'
12621262
run_cmd(commandresult_app, 'affirmative {}'.format(arg))
1263-
assert commandresult_app._last_result
1264-
assert commandresult_app._last_result == cmd2.CommandResult(arg, data=True)
1263+
assert commandresult_app.last_result
1264+
assert commandresult_app.last_result == cmd2.CommandResult(arg, data=True)
12651265

12661266
run_cmd(commandresult_app, 'affirmative_no_data {}'.format(arg))
1267-
assert commandresult_app._last_result
1268-
assert commandresult_app._last_result == cmd2.CommandResult(arg)
1267+
assert commandresult_app.last_result
1268+
assert commandresult_app.last_result == cmd2.CommandResult(arg)
12691269

12701270
def test_commandresult_falsy(commandresult_app):
12711271
arg = 'bar'
12721272
run_cmd(commandresult_app, 'negative {}'.format(arg))
1273-
assert not commandresult_app._last_result
1274-
assert commandresult_app._last_result == cmd2.CommandResult(arg, data=False)
1273+
assert not commandresult_app.last_result
1274+
assert commandresult_app.last_result == cmd2.CommandResult(arg, data=False)
12751275

12761276
run_cmd(commandresult_app, 'negative_no_data {}'.format(arg))
1277-
assert not commandresult_app._last_result
1278-
assert commandresult_app._last_result == cmd2.CommandResult('', arg)
1277+
assert not commandresult_app.last_result
1278+
assert commandresult_app.last_result == cmd2.CommandResult('', arg)
12791279

12801280

12811281
def test_is_text_file_bad_input(base_app):

0 commit comments

Comments
 (0)