summaryrefslogtreecommitdiff
path: root/examples/script_conditional.py
diff options
context:
space:
mode:
Diffstat (limited to 'examples/script_conditional.py')
-rw-r--r--examples/script_conditional.py24
1 files changed, 24 insertions, 0 deletions
diff --git a/examples/script_conditional.py b/examples/script_conditional.py
new file mode 100644
index 00000000..cbfb0494
--- /dev/null
+++ b/examples/script_conditional.py
@@ -0,0 +1,24 @@
+# coding=utf-8
+"""
+This is a Python script intended to be used with the "python_scripting.py" cmd2 example applicaiton.
+
+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.
+"""
+
+# Try to change to a non-existent directory
+cmd('cd foobar')
+
+# Conditionally do something based on the results of the last command
+if self._last_result:
+ print('Contents of foobar directory:')
+ cmd('dir')
+else:
+ # Change to parent directory
+ cmd('cd ..')
+ print('Contents of parent directory:')
+ cmd('dir')