changeset 42:b3200c6e5763

inserting exception for quit...
author catherine@cordelia
date Sat, 24 May 2008 20:30:33 -0400
parents 1275d59c08a7
children e03a8c03ac37
files cmd2.py
diffstat 1 files changed, 9 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/cmd2.py	Thu May 22 20:07:07 2008 -0400
+++ b/cmd2.py	Sat May 24 20:30:33 2008 -0400
@@ -125,6 +125,9 @@
             raise OSError, pastebufferr % ('xclip', 'On Debian/Ubuntu, install with "sudo apt-get install xclip"')
         setPasteBuffer = getPasteBuffer
                 
+class ExitException(Exception):
+    pass
+
 class Cmd(cmd.Cmd):
     caseInsensitive = True
     multilineCommands = []
@@ -134,6 +137,7 @@
     defaultExtension = 'txt'
     defaultFileName = 'command.txt'
     editor = os.environ.get('EDITOR')
+    _STOP_AND_EXIT = 2
     if not editor:
         if sys.platform[:3] == 'win':
             editor = 'notepad'
@@ -336,6 +340,7 @@
                     readline.set_completer(self.old_completer)
                 except ImportError:
                     pass    
+        return stop
 
     def do_EOF(self, arg):
         return True
@@ -370,7 +375,7 @@
             self.stdout.write('%s: %s\n' % (param, str(getattr(self, param))))
 
     def do_quit(self, arg):
-        return 1
+        raise ExitException
     do_exit = do_quit
     do_q = do_quit
     
@@ -501,10 +506,12 @@
                     return
         self.use_rawinput = False
         self.prompt = self.continuationPrompt = ''
-        self.cmdloop()
+        stop = self.cmdloop()
+        # but how to detect whether to exit totally?  
         self.stdin.close()
         keepstate.restore()
         self.lastcmd = ''
+        return (stop == self._STOP_AND_EXIT)
         
     def do_run(self, arg):
         """run [arg]: re-runs an earlier command