Commit 7d53a379 authored by Kurt B. Kaiser's avatar Kurt B. Kaiser

Rework the command line interface, incorporating the shell/edit

configuration selection. Rework the usage message to match.

Also some minor code polishing.
parent 1615713d
...@@ -10,7 +10,6 @@ import socket ...@@ -10,7 +10,6 @@ import socket
import time import time
import traceback import traceback
import types import types
import warnings
import exceptions import exceptions
import linecache import linecache
...@@ -32,9 +31,6 @@ import RemoteDebugger ...@@ -32,9 +31,6 @@ import RemoteDebugger
IDENTCHARS = string.ascii_letters + string.digits + "_" IDENTCHARS = string.ascii_letters + string.digits + "_"
# XX hardwire this for now, remove later KBK 09Jun02
use_subprocess = 1 # Set to 1 to spawn subprocess for command execution
# Change warnings module to write to sys.__stderr__ # Change warnings module to write to sys.__stderr__
try: try:
import warnings import warnings
...@@ -71,10 +67,9 @@ linecache.checkcache = extended_linecache_checkcache ...@@ -71,10 +67,9 @@ linecache.checkcache = extended_linecache_checkcache
class PyShellEditorWindow(EditorWindow): class PyShellEditorWindow(EditorWindow):
"Regular text edit window when a shell is present" "Regular text edit window when a shell is present"
# XXX KBK 19Oct02 Breakpoints are currently removed if module is # XXX KBK 10Dec02 Breakpoints are currently removed if module is modified.
# changed or closed. Future plans include saving breakpoints in a # In the future, it may be possible to preserve breakpoints by changing
# project file and possibly preserving breakpoints by changing their # their line numbers as a module is modified.
# line numbers as a module is modified.
def __init__(self, *args): def __init__(self, *args):
self.breakpoints = [] self.breakpoints = []
...@@ -317,6 +312,7 @@ class ModifiedInterpreter(InteractiveInterpreter): ...@@ -317,6 +312,7 @@ class ModifiedInterpreter(InteractiveInterpreter):
self.rpcclt.register("stdout", self.tkconsole.stdout) self.rpcclt.register("stdout", self.tkconsole.stdout)
self.rpcclt.register("stderr", self.tkconsole.stderr) self.rpcclt.register("stderr", self.tkconsole.stderr)
self.rpcclt.register("flist", self.tkconsole.flist) self.rpcclt.register("flist", self.tkconsole.flist)
self.transfer_path()
self.poll_subprocess() self.poll_subprocess()
def restart_subprocess(self): def restart_subprocess(self):
...@@ -328,12 +324,20 @@ class ModifiedInterpreter(InteractiveInterpreter): ...@@ -328,12 +324,20 @@ class ModifiedInterpreter(InteractiveInterpreter):
self.rpcclt.close() self.rpcclt.close()
self.spawn_subprocess() self.spawn_subprocess()
self.rpcclt.accept() self.rpcclt.accept()
self.transfer_path()
# restart remote debugger # restart remote debugger
if debug: if debug:
gui = RemoteDebugger.restart_subprocess_debugger(self.rpcclt) gui = RemoteDebugger.restart_subprocess_debugger(self.rpcclt)
# reload remote debugger breakpoints for all PyShellEditWindows # reload remote debugger breakpoints for all PyShellEditWindows
debug.load_breakpoints() debug.load_breakpoints()
def transfer_path(self):
self.runcommand("""if 1:
import sys as _sys
_sys.path = %s
del _sys
\n""" % `sys.path`)
active_seq = None active_seq = None
def poll_subprocess(self): def poll_subprocess(self):
...@@ -586,7 +590,7 @@ class PyShell(OutputWindow): ...@@ -586,7 +590,7 @@ class PyShell(OutputWindow):
ColorDelegator = ModifiedColorDelegator ColorDelegator = ModifiedColorDelegator
UndoDelegator = ModifiedUndoDelegator UndoDelegator = ModifiedUndoDelegator
# Override menus: Run and Format not desired in shell; add Debug # Override menus
menu_specs = [ menu_specs = [
("file", "_File"), ("file", "_File"),
("edit", "_Edit"), ("edit", "_Edit"),
...@@ -1008,108 +1012,168 @@ class PseudoFile: ...@@ -1008,108 +1012,168 @@ class PseudoFile:
usage_msg = """\ usage_msg = """\
usage: idle.py [-c command] [-d] [-i] [-r script] [-s] [-t title] [arg] ...
idle file(s) (without options) edit the file(s) USAGE: idle [-deis] [-t title] [file]*
idle [-ds] [-t title] (-c cmd | -r file) [arg]*
idle [-ds] [-t title] - [arg]*
-h print this help message and exit
The following options will override the IDLE 'settings' configuration:
-e open an edit window
-i open a shell window
The following options imply -i and will open a shell:
-c cmd run the command in a shell, or
-r file run script from file
-d enable the debugger
-s run $IDLESTARTUP or $PYTHONSTARTUP before anything else
-t title set title of shell window
A default edit window will be bypassed when -c, -r, or - are used.
[arg]* are passed to the command (-c) or script (-r) in sys.argv[1:].
Examples:
-c cmd run the command in a shell idle
-d enable the debugger Open an edit window or shell depending on IDLE's configuration.
-e edit mode; arguments are files to be edited
-i open an interactive shell
-i file(s) open a shell and also an editor window for each file
-r
-s run $IDLESTARTUP or $PYTHONSTARTUP before anything else
-t title set title of shell window
Remaining arguments are applied to the command (-c) or script (-r). idle foo.py foobar.py
Edit the files, also open a shell if configured to start with shell.
idle -est "Baz" foo.py
Run $IDLESTARTUP or $PYTHONSTARTUP, edit foo.py, and open a shell
window with the title "Baz".
idle -c "import sys; print sys.argv" "foo"
Open a shell window and run the command, passing "-c" in sys.argv[0]
and "foo" in sys.argv[1].
idle -d -s -r foo.py "Hello World"
Open a shell window, run a startup script, enable the debugger, and
run foo.py, passing "foo.py" in sys.argv[0] and "Hello World" in
sys.argv[1].
echo "import sys; print sys.argv" | idle - "foobar"
Open a shell window, run the script piped in, passing '' in sys.argv[0]
and "foobar" in sys.argv[1].
""" """
def main(): def main():
global flist, root, use_subprocess
enable_shell = False
enable_edit = False
debug = False
cmd = None cmd = None
edit = 0
debug = 0
script = None script = None
startup = 0 startup = False
try: try:
opts, args = getopt.getopt(sys.argv[1:], "c:deir:st:") opts, args = getopt.getopt(sys.argv[1:], "c:deihr:st:")
except getopt.error, msg: except getopt.error, msg:
sys.stderr.write("Error: %s\n" % str(msg)) sys.stderr.write("Error: %s\n" % str(msg))
sys.stderr.write(usage_msg) sys.stderr.write(usage_msg)
sys.exit(2) sys.exit(2)
for o, a in opts: for o, a in opts:
if o == '-c': if o == '-c':
cmd = a cmd = a
enable_shell = True
if o == '-d': if o == '-d':
debug = 1 debug = True
enable_shell = True
if o == '-e': if o == '-e':
edit = 1 enable_edit = True
if o == '-h':
sys.stdout.write(usage_msg)
sys.exit()
if o == '-i':
enable_shell = True
if o == '-r': if o == '-r':
script = a script = a
if os.path.isfile(script):
pass
else:
print "No script file: ", script
sys.exit()
enable_shell = True
if o == '-s': if o == '-s':
startup = 1 startup = True
enable_shell = True
if o == '-t': if o == '-t':
PyShell.shell_title = a PyShell.shell_title = a
enable_shell = True
if args and args[0] == '-':
cmd = sys.stdin.read()
enable_shell = True
use_subprocess = True
if args and args[0] != "-": edit = 1 # process sys.argv and sys.path:
for i in range(len(sys.path)): for i in range(len(sys.path)):
sys.path[i] = os.path.abspath(sys.path[i]) sys.path[i] = os.path.abspath(sys.path[i])
if args and args[0] == '-':
pathx = [] sys.argv = [''] + args[1:]
if edit: elif cmd:
sys.argv = ['-c'] + args
elif script:
sys.argv = [script] + args
elif args:
enable_edit = True
pathx = []
for filename in args: for filename in args:
pathx.append(os.path.dirname(filename)) pathx.append(os.path.dirname(filename))
elif args and args[0] != "-": for dir in pathx:
pathx.append(os.path.dirname(args[0])) dir = os.path.abspath(dir)
else: if not dir in sys.path:
pathx.append(os.curdir) sys.path.insert(0, dir)
for dir in pathx: # check the IDLE settings configuration (but command line overrides)
dir = os.path.abspath(dir) edit_start = idleConf.GetOption('main', 'General',
if not dir in sys.path: 'editor-on-startup', type='bool')
sys.path.insert(0, dir) enable_edit = enable_edit or edit_start
enable_shell = enable_shell or not edit_start
global flist, root # start editor and/or shell windows:
root = Tk(className="Idle") root = Tk(className="Idle")
fixwordbreaks(root) fixwordbreaks(root)
root.withdraw() root.withdraw()
flist = PyShellFileList(root) flist = PyShellFileList(root)
if enable_edit:
if edit: if not (cmd or script):
for filename in args: for filename in args:
flist.open(filename) flist.open(filename)
if not args: if not args:
flist.new() flist.new()
else: if enable_shell:
if cmd: flist.open_shell()
sys.argv = ["-c"] + args elif enable_shell:
else: flist.pyshell = PyShell(flist)
sys.argv = args or [""] flist.pyshell.begin()
shell = flist.pyshell
shell = PyShell(flist) # handle remaining options:
interp = shell.interp if debug:
flist.pyshell = shell shell.open_debugger()
if startup: if startup:
filename = os.environ.get("IDLESTARTUP") or \ filename = os.environ.get("IDLESTARTUP") or \
os.environ.get("PYTHONSTARTUP") os.environ.get("PYTHONSTARTUP")
if filename and os.path.isfile(filename): if filename and os.path.isfile(filename):
interp.execfile(filename) shell.interp.execfile(filename)
if cmd or script:
if debug: shell.interp.runcommand("""if 1:
shell.open_debugger() import sys as _sys
if cmd: _sys.argv = %s
interp.execsource(cmd) del _sys
elif script: \n""" % `sys.argv`)
if os.path.isfile(script): if cmd:
interp.execfile(script) shell.interp.execsource(cmd)
else: elif script:
print "No script file: ", script shell.interp.execfile(script)
shell.begin()
root.mainloop() root.mainloop()
root.destroy() root.destroy()
def display_port_binding_error(): def display_port_binding_error():
print """\ print """\
IDLE cannot run. IDLE cannot run.
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment