Skip to content

Commit c838160

Browse files
committed
Deprecated CmdResult helper class and promoted CommandResult
These classes are subtly different, particularly in terms of their truthiness. CmdResult - attributes: out, err, war - truthy: if err is falsy CommandResult - attributes: stdout, stderr, data - truthy: if err is falsy AND data is not None So CmdResult was oriented to provide essentially info, error, and warning messages to the user (typically as stirngs), whereas CommandResult is geared towards providing info and error messages to the user as strings in addition to data to the user in a command-specific format which is arbitrary other than it should never be None if the command succeeds.
1 parent c7feaa6 commit c838160

File tree

8 files changed

+31
-21
lines changed

8 files changed

+31
-21
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@
99
* Added ``chop`` argument to ``cmd2.Cmd.ppaged()`` method for displaying output using a pager
1010
* If ``chop`` is ``False``, then ``self.pager`` is used as the pager
1111
* Otherwise ``self.pager_chop`` is used as the pager
12+
* Deprecations
13+
* The ``CmdResult`` helper class is *deprecated* and replaced by the improved ``CommandResult`` class
14+
* ``CommandResult`` has the following attributes: **stdout**, **stderr**, and **data**
15+
* ``CmdResult`` had attributes of: **out**, **err**, **war**
16+
* ``CmdResult`` will be deleted in the next release
1217

1318
## 0.8.8 (TBD, 2018)
1419
* Bug Fixes

cmd2/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#
22
# -*- coding: utf-8 -*-
33
"""This simply imports certain things for backwards compatibility."""
4-
from .cmd2 import __version__, Cmd, CmdResult, Statement, EmptyStatement, categorize
4+
from .cmd2 import __version__, Cmd, Statement, EmptyStatement, categorize
55
from .cmd2 import with_argument_list, with_argparser, with_argparser_and_unknown_args, with_category
6+
from .pyscript_bridge import CommandResult

cmd2/cmd2.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3219,7 +3219,7 @@ def restore(self) -> None:
32193219

32203220

32213221
class CmdResult(utils.namedtuple_with_two_defaults('CmdResult', ['out', 'err', 'war'])):
3222-
"""Derive a class to store results from a named tuple so we can tweak dunder methods for convenience.
3222+
"""DEPRECATED: Derive a class to store results from a named tuple so we can tweak dunder methods for convenience.
32233223
32243224
This is provided as a convenience and an example for one possible way for end users to store results in
32253225
the self._last_result attribute of cmd2.Cmd class instances. See the "python_scripting.py" example for how it can

cmd2/pyscript_bridge.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
from .utils import namedtuple_with_defaults
2323

2424

25-
class CommandResult(namedtuple_with_defaults('CmdResult', ['stdout', 'stderr', 'data'])):
25+
class CommandResult(namedtuple_with_defaults('CommandResult', ['stdout', 'stderr', 'data'])):
2626
"""Encapsulates the results from a command.
2727
2828
Named tuple attributes

docs/argument_processing.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,7 @@ Here's what it looks like::
333333
if unknown:
334334
self.perror("dir does not take any positional arguments:", traceback_war=False)
335335
self.do_help('dir')
336-
self._last_result = CmdResult('', 'Bad arguments')
336+
self._last_result = CommandResult('', 'Bad arguments')
337337
return
338338

339339
# Get the contents as a list

examples/python_scripting.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,15 +56,16 @@ def do_cd(self, arglist):
5656
if not arglist or len(arglist) != 1:
5757
self.perror("cd requires exactly 1 argument:", traceback_war=False)
5858
self.do_help('cd')
59-
self._last_result = cmd2.CmdResult('', 'Bad arguments')
59+
self._last_result = cmd2.CommandResult('', 'Bad arguments')
6060
return
6161

6262
# Convert relative paths to absolute paths
6363
path = os.path.abspath(os.path.expanduser(arglist[0]))
6464

6565
# Make sure the directory exists, is a directory, and we have read access
6666
out = ''
67-
err = ''
67+
err = None
68+
data = None
6869
if not os.path.isdir(path):
6970
err = '{!r} is not a directory'.format(path)
7071
elif not os.access(path, os.R_OK):
@@ -77,10 +78,11 @@ def do_cd(self, arglist):
7778
else:
7879
out = 'Successfully changed directory to {!r}\n'.format(path)
7980
self.stdout.write(out)
81+
data = path
8082

8183
if err:
8284
self.perror(err, traceback_war=False)
83-
self._last_result = cmd2.CmdResult(out, err)
85+
self._last_result = cmd2.CommandResult(out, err, data)
8486

8587
# Enable tab completion for cd command
8688
def complete_cd(self, text, line, begidx, endidx):
@@ -96,7 +98,7 @@ def do_dir(self, args, unknown):
9698
if unknown:
9799
self.perror("dir does not take any positional arguments:", traceback_war=False)
98100
self.do_help('dir')
99-
self._last_result = cmd2.CmdResult('', 'Bad arguments')
101+
self._last_result = cmd2.CommandResult('', 'Bad arguments')
100102
return
101103

102104
# Get the contents as a list
@@ -109,7 +111,7 @@ def do_dir(self, args, unknown):
109111
self.stdout.write(fmt.format(f))
110112
self.stdout.write('\n')
111113

112-
self._last_result = cmd2.CmdResult(contents)
114+
self._last_result = cmd2.CommandResult(data=contents)
113115

114116

115117
if __name__ == '__main__':

examples/scripts/conditional.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
if self._last_result:
3131
print('\nContents of directory {!r}:'.format(directory))
3232
app('dir -l')
33+
print('{}\n'.format(self._last_result.data))
3334

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

tests/test_cmd2.py

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1468,32 +1468,33 @@ def test_clipboard_failure(base_app, capsys):
14681468
assert "Cannot redirect to paste buffer; install 'pyperclip' and re-run to enable" in err
14691469

14701470

1471-
class CmdResultApp(cmd2.Cmd):
1471+
class CommandResultApp(cmd2.Cmd):
14721472
def __init__(self, *args, **kwargs):
14731473
super().__init__(*args, **kwargs)
14741474

14751475
def do_affirmative(self, arg):
1476-
self._last_result = cmd2.CmdResult(arg)
1476+
self._last_result = cmd2.CommandResult(arg, data=True)
14771477

14781478
def do_negative(self, arg):
1479-
self._last_result = cmd2.CmdResult('', arg)
1479+
self._last_result = cmd2.CommandResult(arg)
14801480

14811481
@pytest.fixture
1482-
def cmdresult_app():
1483-
app = CmdResultApp()
1482+
def commandresult_app():
1483+
app = CommandResultApp()
14841484
app.stdout = StdOut()
14851485
return app
14861486

1487-
def test_cmdresult(cmdresult_app):
1487+
def test_commandresult_truthy(commandresult_app):
14881488
arg = 'foo'
1489-
run_cmd(cmdresult_app, 'affirmative {}'.format(arg))
1490-
assert cmdresult_app._last_result
1491-
assert cmdresult_app._last_result == cmd2.CmdResult(arg)
1489+
run_cmd(commandresult_app, 'affirmative {}'.format(arg))
1490+
assert commandresult_app._last_result
1491+
assert commandresult_app._last_result == cmd2.CommandResult(arg, data=True)
14921492

1493+
def test_commandresult_falsy(commandresult_app):
14931494
arg = 'bar'
1494-
run_cmd(cmdresult_app, 'negative {}'.format(arg))
1495-
assert not cmdresult_app._last_result
1496-
assert cmdresult_app._last_result == cmd2.CmdResult('', arg)
1495+
run_cmd(commandresult_app, 'negative {}'.format(arg))
1496+
assert not commandresult_app._last_result
1497+
assert commandresult_app._last_result == cmd2.CommandResult(arg)
14971498

14981499

14991500
def test_is_text_file_bad_input(base_app):

0 commit comments

Comments
 (0)