summaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorTodd Leonhardt <tleonhardt@gmail.com>2017-03-14 16:53:22 -0400
committerTodd Leonhardt <tleonhardt@gmail.com>2017-03-14 16:53:22 -0400
commit28eba1722b12f1a270fffd9a8596044c5be6e7cd (patch)
treee85991423f473cbff3f31ca0814602c20ff49b51 /examples
parent7786709db04be8385226d6a56046a74983e8c983 (diff)
downloadcmd2-git-28eba1722b12f1a270fffd9a8596044c5be6e7cd.tar.gz
Improved the CmdResult namedtuple subclass
The last two arguments (err and war) are now optional. Only the 1st argument (out) is required. err and war default to empty strings.
Diffstat (limited to 'examples')
-rwxr-xr-xexamples/python_scripting.py10
-rw-r--r--examples/script_conditional.py8
2 files changed, 12 insertions, 6 deletions
diff --git a/examples/python_scripting.py b/examples/python_scripting.py
index ea556952..cecd7ec7 100755
--- a/examples/python_scripting.py
+++ b/examples/python_scripting.py
@@ -57,7 +57,7 @@ class CmdLineApp(Cmd):
if not arg or len(arg) != 1:
self.perror("cd requires exactly 1 argument:", traceback_war=False)
self.do_help('cd')
- self._last_result = CmdResult('', 'Bad arguments', '')
+ self._last_result = CmdResult('', 'Bad arguments')
return
# Convert relative paths to absolute paths
@@ -66,7 +66,6 @@ class CmdLineApp(Cmd):
# Make sure the directory exists, is a directory, and we have read access
out = ''
err = ''
- war = ''
if not os.path.isdir(path):
err = '{!r} is not a directory'.format(path)
elif not os.access(path, os.R_OK):
@@ -82,8 +81,9 @@ class CmdLineApp(Cmd):
if err:
self.perror(err, traceback_war=False)
- self._last_result = CmdResult(out, err, war)
+ self._last_result = CmdResult(out, err)
+ # noinspection PyUnusedLocal
def complete_cd(self, text, line, begidx, endidx):
"""Handles completion of arguments for the cd command.
@@ -103,7 +103,7 @@ class CmdLineApp(Cmd):
if arg:
self.perror("dir does not take any arguments:", traceback_war=False)
self.do_help('dir')
- self._last_result = CmdResult('', 'Bad arguments', '')
+ self._last_result = CmdResult('', 'Bad arguments')
return
# Get the contents as a list
@@ -116,7 +116,7 @@ class CmdLineApp(Cmd):
self.stdout.write(fmt.format(f))
self.stdout.write('\n')
- self._last_result = CmdResult(contents, '', '')
+ self._last_result = CmdResult(contents)
if __name__ == '__main__':
diff --git a/examples/script_conditional.py b/examples/script_conditional.py
index 3964ab6d..f0ded920 100644
--- a/examples/script_conditional.py
+++ b/examples/script_conditional.py
@@ -5,7 +5,7 @@ This is a Python script intended to be used with the "python_scripting.py" cmd2
To run it you should do the following:
./python_scripting.py
py run('script_conditional.py')
-
+
Note: The "cmd" function is defined within the cmd2 embedded Python environment and in there "self" is your cmd2
application instance.
"""
@@ -17,8 +17,14 @@ cmd('cd foobar')
if self._last_result:
print('Contents of foobar directory:')
cmd('dir')
+
+ # Change back to where we were
+ cmd('cd ..')
else:
# Change to parent directory
cmd('cd ..')
print('Contents of parent directory:')
cmd('dir')
+
+ # Change back to where we were
+ cmd('cd examples')