Mercurial > python-cmd2
comparison cmd2.py @ 235:78ad20c2eed0
py working better now; still needs a iscomplete=True on onecmd
author | catherine@dellzilla |
---|---|
date | Tue, 24 Mar 2009 13:48:25 -0400 |
parents | ff99b39259d5 |
children | 035330ccfcf0 |
comparison
equal
deleted
inserted
replaced
234:ff99b39259d5 | 235:78ad20c2eed0 |
---|---|
24 CHANGES: | 24 CHANGES: |
25 As of 0.3.0, options should be specified as `optparse` options. See README.txt. | 25 As of 0.3.0, options should be specified as `optparse` options. See README.txt. |
26 flagReader.py options are still supported for backward compatibility | 26 flagReader.py options are still supported for backward compatibility |
27 """ | 27 """ |
28 import cmd, re, os, sys, optparse, subprocess, tempfile, pyparsing, doctest | 28 import cmd, re, os, sys, optparse, subprocess, tempfile, pyparsing, doctest |
29 import unittest, string, datetime, urllib, inspect, code | 29 import unittest, string, datetime, urllib, inspect |
30 from code import InteractiveConsole, InteractiveInterpreter, softspace | |
30 from optparse import make_option | 31 from optparse import make_option |
31 __version__ = '0.4.8' | 32 __version__ = '0.4.8' |
32 | 33 |
33 class OptionParser(optparse.OptionParser): | 34 class OptionParser(optparse.OptionParser): |
34 def exit(self, status=0, msg=None): | 35 def exit(self, status=0, msg=None): |
223 return result | 224 return result |
224 | 225 |
225 class EmbeddedConsoleExit(Exception): | 226 class EmbeddedConsoleExit(Exception): |
226 pass | 227 pass |
227 | 228 |
228 class InteractiveConsole(code.InteractiveConsole): | 229 class MyInteractiveConsole(InteractiveConsole): |
229 def runcode(self, code): | 230 def runcode(self, code): |
230 """Execute a code object. | 231 """Execute a code object. |
231 | 232 |
232 When an exception occurs, self.showtraceback() is called to | 233 When an exception occurs, self.showtraceback() is called to |
233 display a traceback. All exceptions are caught except | 234 display a traceback. All exceptions are caught except |
245 except (SystemExit, EmbeddedConsoleExit): | 246 except (SystemExit, EmbeddedConsoleExit): |
246 raise | 247 raise |
247 except: | 248 except: |
248 self.showtraceback() | 249 self.showtraceback() |
249 else: | 250 else: |
250 if code.softspace(sys.stdout, 0): | 251 if softspace(sys.stdout, 0): |
251 print | 252 print |
252 | 253 |
253 class Cmd(cmd.Cmd): | 254 class Cmd(cmd.Cmd): |
254 echo = False | 255 echo = False |
255 case_insensitive = True | 256 case_insensitive = True |
794 py: Enters interactive Python mode; end with `Ctrl-D`. | 795 py: Enters interactive Python mode; end with `Ctrl-D`. |
795 Do not end with Ctrl-Z, or it will end your entire cmd2 session! | 796 Do not end with Ctrl-Z, or it will end your entire cmd2 session! |
796 Non-python commands can be issued with cmd('your non-python command here'). | 797 Non-python commands can be issued with cmd('your non-python command here'). |
797 ''' | 798 ''' |
798 if arg.strip(): | 799 if arg.strip(): |
799 interp = code.InteractiveInterpreter(locals=self.pystate) | 800 interp = InteractiveInterpreter(locals=self.pystate) |
800 interp.runcode(arg) | 801 interp.runcode(arg) |
801 else: | 802 else: |
802 interp = InteractiveConsole(locals=self.pystate) | 803 interp = MyInteractiveConsole(locals=self.pystate) |
803 def quit(): | 804 def quit(): |
804 raise EmbeddedConsoleExit | 805 raise EmbeddedConsoleExit |
805 self.pystate['quit'] = quit | 806 self.pystate['quit'] = quit |
806 self.pystate['exit'] = quit | 807 self.pystate['exit'] = quit |
808 self.pystate[self.nonpythoncommand] = self.onecmd | |
807 try: | 809 try: |
808 interp.interact() | 810 interp.interact() |
809 except (EmbeddedConsoleExit, SystemExit): | 811 except SystemExit: |
812 quit() | |
813 except EmbeddedConsoleExit: | |
810 return | 814 return |
811 | 815 |
812 def do_history(self, arg): | 816 def do_history(self, arg): |
813 """history [arg]: lists past commands issued | 817 """history [arg]: lists past commands issued |
814 | 818 |