diff options
author | Todd Leonhardt <todd.leonhardt@gmail.com> | 2018-06-23 19:43:42 -0400 |
---|---|---|
committer | Todd Leonhardt <todd.leonhardt@gmail.com> | 2018-06-23 19:43:42 -0400 |
commit | c8381601e4e3b31969ea6108617356572a7d1ca3 (patch) | |
tree | 023e67165ce45eba33739f248c1ee53b7c975d8f /examples/python_scripting.py | |
parent | c7feaa60e46563cd60c217d27f888a05c9c3439d (diff) | |
download | cmd2-git-c8381601e4e3b31969ea6108617356572a7d1ca3.tar.gz |
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.
Diffstat (limited to 'examples/python_scripting.py')
-rwxr-xr-x | examples/python_scripting.py | 12 |
1 files changed, 7 insertions, 5 deletions
diff --git a/examples/python_scripting.py b/examples/python_scripting.py index fd2d7e8f..069bcff5 100755 --- a/examples/python_scripting.py +++ b/examples/python_scripting.py @@ -56,7 +56,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.CmdResult('', 'Bad arguments') + self._last_result = cmd2.CommandResult('', 'Bad arguments') return # Convert relative paths to absolute paths @@ -64,7 +64,8 @@ class CmdLineApp(cmd2.Cmd): # Make sure the directory exists, is a directory, and we have read access out = '' - err = '' + err = None + data = None if not os.path.isdir(path): err = '{!r} is not a directory'.format(path) elif not os.access(path, os.R_OK): @@ -77,10 +78,11 @@ class CmdLineApp(cmd2.Cmd): else: out = 'Successfully changed directory to {!r}\n'.format(path) self.stdout.write(out) + data = path if err: self.perror(err, traceback_war=False) - self._last_result = cmd2.CmdResult(out, err) + self._last_result = cmd2.CommandResult(out, err, data) # Enable tab completion for cd command def complete_cd(self, text, line, begidx, endidx): @@ -96,7 +98,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.CmdResult('', 'Bad arguments') + self._last_result = cmd2.CommandResult('', 'Bad arguments') return # Get the contents as a list @@ -109,7 +111,7 @@ class CmdLineApp(cmd2.Cmd): self.stdout.write(fmt.format(f)) self.stdout.write('\n') - self._last_result = cmd2.CmdResult(contents) + self._last_result = cmd2.CommandResult(data=contents) if __name__ == '__main__': |