summaryrefslogtreecommitdiff
path: root/examples/scripts/conditional.py
diff options
context:
space:
mode:
authorEric Lin <anselor@gmail.com>2020-02-24 16:11:32 -0500
committerEric Lin <anselor@gmail.com>2020-02-24 17:02:00 -0500
commitec7b442eff9f6488509d43b6ae2e902fcdb79ddf (patch)
treeb7705191352f7681f5b1217331107470139fe720 /examples/scripts/conditional.py
parentfea1bc15f4a53aa72d16c2985377fe3987b6b348 (diff)
downloadcmd2-git-ec7b442eff9f6488509d43b6ae2e902fcdb79ddf.tar.gz
Updated python scripting exmaple to illustrate how
stdout/stderr is automatically captured in CommandResult during python scripting. Fixes #898
Diffstat (limited to 'examples/scripts/conditional.py')
-rw-r--r--examples/scripts/conditional.py18
1 files changed, 14 insertions, 4 deletions
diff --git a/examples/scripts/conditional.py b/examples/scripts/conditional.py
index eb4c203e..eb710695 100644
--- a/examples/scripts/conditional.py
+++ b/examples/scripts/conditional.py
@@ -24,13 +24,20 @@ else:
original_dir = os.getcwd()
# Try to change to the specified directory
-app('cd {}'.format(directory))
+result = app('cd {}'.format(directory))
# Conditionally do something based on the results of the last command
-if self.last_result:
+if result:
+ print(f"STDOUT: {result.stdout}\n")
+ print(f"STDERR: {result.stderr}\n")
+
print('\nContents of directory {!r}:'.format(directory))
- app('dir -l')
- print('{}\n'.format(self.last_result.data))
+ result = app('dir -l')
+
+ print(f"STDOUT: {result.stdout}\n")
+ print(f"STDERR: {result.stderr}\n")
+
+ print('{}\n'.format(result.data))
# Change back to where we were
print('Changing back to original directory: {!r}'.format(original_dir))
@@ -38,3 +45,6 @@ if self.last_result:
else:
# cd command failed, print a warning
print('Failed to change directory to {!r}'.format(directory))
+
+ print(f"STDOUT: {result.stdout}\n")
+ print(f"STDERR: {result.stderr}\n")