summaryrefslogtreecommitdiff
path: root/examples/scripts
diff options
context:
space:
mode:
Diffstat (limited to 'examples/scripts')
-rwxr-xr-xexamples/scripts/arg_printer.py8
-rw-r--r--examples/scripts/conditional.py39
-rw-r--r--examples/scripts/script.py7
-rw-r--r--examples/scripts/script.txt2
4 files changed, 56 insertions, 0 deletions
diff --git a/examples/scripts/arg_printer.py b/examples/scripts/arg_printer.py
new file mode 100755
index 00000000..848dcf99
--- /dev/null
+++ b/examples/scripts/arg_printer.py
@@ -0,0 +1,8 @@
+#!/usr/bin/env python
+# coding=utf-8
+import os
+import sys
+print("Running Python script {!r} which was called with {} arguments".format(os.path.basename(sys.argv[0]),
+ len(sys.argv) - 1))
+for i, arg in enumerate(sys.argv[1:]):
+ print("arg {}: {!r}".format(i+1, arg))
diff --git a/examples/scripts/conditional.py b/examples/scripts/conditional.py
new file mode 100644
index 00000000..1eeeadba
--- /dev/null
+++ b/examples/scripts/conditional.py
@@ -0,0 +1,39 @@
+# coding=utf-8
+"""
+This is a Python script intended to be used with the "python_scripting.py" cmd2 example application.
+
+To run it you should do the following:
+ ./python_scripting.py
+ pyscript scripts/conditional.py directory_path
+
+Note: The "cmd" function is defined within the cmd2 embedded Python environment and in there "self" is your cmd2
+application instance.
+"""
+import os
+import sys
+
+
+if len(sys.argv) > 1:
+ directory = sys.argv[1]
+ print('Using specified directory: {!r}'.format(directory))
+else:
+ directory = 'foobar'
+ print('Using default directory: {!r}'.format(directory))
+
+# Keep track of where we stared
+original_dir = os.getcwd()
+
+# Try to change to the specified directory
+cmd('cd {}'.format(directory))
+
+# Conditionally do something based on the results of the last command
+if self._last_result:
+ print('\nContents of directory {!r}:'.format(directory))
+ cmd('dir -l')
+
+ # Change back to where we were
+ print('Changing back to original directory: {!r}'.format(original_dir))
+ cmd('cd {}'.format(original_dir))
+else:
+ # cd command failed, print a warning
+ print('Failed to change directory to {!r}'.format(directory))
diff --git a/examples/scripts/script.py b/examples/scripts/script.py
new file mode 100644
index 00000000..3f60c8fb
--- /dev/null
+++ b/examples/scripts/script.py
@@ -0,0 +1,7 @@
+#!/usr/bin/env python
+# coding=utf-8
+"""
+Trivial example of a Python script which can be run inside a cmd2 application.
+"""
+print("This is a python script running ...")
+
diff --git a/examples/scripts/script.txt b/examples/scripts/script.txt
new file mode 100644
index 00000000..1e18262a
--- /dev/null
+++ b/examples/scripts/script.txt
@@ -0,0 +1,2 @@
+help
+help history