summaryrefslogtreecommitdiff
path: root/cmd2.py
diff options
context:
space:
mode:
authorTodd Leonhardt <tleonhardt@gmail.com>2017-06-13 14:49:22 -0400
committerTodd Leonhardt <tleonhardt@gmail.com>2017-06-13 14:49:22 -0400
commit34c4f3ce4b778d95a67a7d66ed98682ba7464f67 (patch)
treed6f20d06f80c7dede07963133a7a48bb9484f2fb /cmd2.py
parent0af8018cdce37e40719e3f6c4023e9525b1e69de (diff)
downloadcmd2-git-34c4f3ce4b778d95a67a7d66ed98682ba7464f67.tar.gz
Added new pyscript command
This command is explicitly for running Python script files within an embedded Python interpreter. The advantages over the py command with "run" are: - Tab-completion of file system paths is supported - Command-line arguments can be passed to the Python script
Diffstat (limited to 'cmd2.py')
-rwxr-xr-xcmd2.py36
1 files changed, 36 insertions, 0 deletions
diff --git a/cmd2.py b/cmd2.py
index 3fe13f54..aabda96e 100755
--- a/cmd2.py
+++ b/cmd2.py
@@ -1583,6 +1583,42 @@ class Cmd(cmd.Cmd):
self._in_py = False
return self._should_quit
+ # noinspection PyUnusedLocal
+ @options([], arg_desc='<script_path> [script_arguments]')
+ def do_pyscript(self, arg, opts=None):
+ """\nRuns a python script file inside the console
+
+Console commands can be executed inside this script with cmd("your command")
+However, you cannot run nested "py" or "pyscript" commands from within this script
+Paths or arguments that contain spaces must be enclosed in quotes
+"""
+ if not arg:
+ self.perror("pyscript command requires at least 1 argument ...", traceback_war=False)
+ self.do_help('pyscript')
+ return
+
+ if not USE_ARG_LIST:
+ arg = shlex.split(arg, posix=POSIX_SHLEX)
+
+ # Get the absolute path of the script
+ script_path = os.path.abspath(os.path.expanduser(arg[0]))
+
+ # Save current command line arguments
+ orig_args = sys.argv
+
+ # Overwrite sys.argv to allow the script to take command line arguments
+ sys.argv = [script_path]
+ sys.argv.extend(arg[1:])
+
+ # Run the script
+ self.do_py("run('{}')".format(arg[0]))
+
+ # Restore command line arguments to original state
+ sys.argv = orig_args
+
+ # Enable tab completion of paths for pyscript command
+ complete_pyscript = path_complete
+
# Only include the do_ipy() method if IPython is available on the system
if ipython_available:
# noinspection PyMethodMayBeStatic,PyUnusedLocal