summaryrefslogtreecommitdiff
path: root/cmd2.py
diff options
context:
space:
mode:
Diffstat (limited to 'cmd2.py')
-rwxr-xr-xcmd2.py20
1 files changed, 11 insertions, 9 deletions
diff --git a/cmd2.py b/cmd2.py
index 1740f33a..e678de16 100755
--- a/cmd2.py
+++ b/cmd2.py
@@ -1130,9 +1130,20 @@ class Cmd(cmd.Cmd):
'''
self.pystate['self'] = self
arg = arg.parsed.raw[2:].strip()
+
+ # Support the run command even if called prior to invoking an interactive interpreter
+ def run(arg):
+ try:
+ with open(arg) as f:
+ interp.runcode(f.read())
+ except IOError as e:
+ self.perror(e)
+ self.pystate['run'] = run
+
localvars = (self.locals_in_py and self.pystate) or {}
interp = InteractiveConsole(locals=localvars)
interp.runcode('import sys, os;sys.path.insert(0, os.getcwd())')
+
if arg.strip():
interp.runcode(arg)
else:
@@ -1142,18 +1153,9 @@ class Cmd(cmd.Cmd):
def onecmd_plus_hooks(arg):
return self.onecmd_plus_hooks(arg + '\n')
- def run(arg):
- try:
- file = open(arg)
- interp.runcode(file.read())
- file.close()
- except IOError as e:
- self.perror(e)
-
self.pystate['quit'] = quit
self.pystate['exit'] = quit
self.pystate['cmd'] = onecmd_plus_hooks
- self.pystate['run'] = run
keepstate = None
try:
cprt = 'Type "help", "copyright", "credits" or "license" for more information.'