changeset 346:49dd1ce6cfd6

quitting after invocation commands
author catherine@Drou
date Tue, 16 Feb 2010 15:47:17 -0500
parents 6fe1e75e3a67
children 432ccab7c6c8
files cmd2.py docs/freefeatures.rst docs/pycon2010/pycon2010.rst
diffstat 3 files changed, 45 insertions(+), 37 deletions(-) [+]
line wrap: on
line diff
--- a/cmd2.py	Tue Feb 16 15:13:11 2010 -0500
+++ b/cmd2.py	Tue Feb 16 15:47:17 2010 -0500
@@ -412,7 +412,7 @@
                 self.poutput(msg)
             else:
                 print (msg)
-    _STOP_AND_EXIT = 2
+    _STOP_AND_EXIT = 2  # distinguish end of script file from actual exit
     editor = os.environ.get('EDITOR')
     if not editor:
         if sys.platform[:3] == 'win':
@@ -742,6 +742,11 @@
                 if len(funcs) == 1:
                     result = 'do_' + funcs[0]
         return result
+    def onecmd_plus_hooks(self, line):
+        line = self.precmd(line)
+        stop = self.onecmd(line)
+        stop = self.postcmd(stop, line)        
+        return stop
     def onecmd(self, line):
         """Interpret the argument as though it had been typed in response
         to the prompt.
@@ -893,9 +898,7 @@
                     line = self.pseudo_raw_input(self.prompt)
                 if (self.echo) and (isinstance(self.stdin, file)):
                     self.stdout.write(line + '\n')
-                line = self.precmd(line)
-                stop = self.onecmd(line)
-                stop = self.postcmd(stop, line)
+                stop = self.onecmd_plus_hooks(line)
             self.postloop()
         finally:
             if self.use_rawinput and self.completekey:
@@ -1024,11 +1027,11 @@
             interp = InteractiveConsole(locals=localvars)
             def quit():
                 raise EmbeddedConsoleExit
-            def onecmd(arg):
-                return self.onecmd(arg + '\n')
+            def onecmd_plus_hooks(arg):
+                return self.onecmd_plus_hooks(arg + '\n')
             self.pystate['quit'] = quit
             self.pystate['exit'] = quit
-            self.pystate['cmd'] = onecmd
+            self.pystate['cmd'] = onecmd_plus_hooks
             try:
                 cprt = 'Type "help", "copyright", "credits" or "license" for more information.'        
                 keepstate = Statekeeper(sys, ('stdin','stdout'))
@@ -1209,9 +1212,7 @@
         runme = self.last_matching(arg)
         self.pfeedback(runme)
         if runme:
-            runme = self.precmd(runme)
-            stop = self.onecmd(runme)
-            stop = self.postcmd(stop, runme)
+            stop = self.onecmd_plus_hooks(runme)
     do_r = do_run        
             
     def fileimport(self, statement, source):
@@ -1236,8 +1237,8 @@
 
     def run_commands_at_invocation(self, callargs):
         for initial_command in callargs:
-            if self.onecmd(initial_command + '\n') == app._STOP_AND_EXIT:
-                return
+            if self.onecmd_plus_hooks(initial_command + '\n'):
+                return self._STOP_AND_EXIT
 
     def cmdloop(self):
         parser = optparse.OptionParser()
@@ -1248,8 +1249,8 @@
         if callopts.test:
             self.runTranscriptTests(callargs)
         else:
-            self.run_commands_at_invocation(callargs)
-            self._cmdloop()   
+            if not self.run_commands_at_invocation(callargs):
+                self._cmdloop()   
             
 class HistoryItem(str):
     listformat = '-------------------------[%d]\n%s\n'
@@ -1474,9 +1475,7 @@
                     command.append(line[len(self.cmdapp.continuation_prompt):])
                     line = transcript.next()
                 command = ''.join(command)               
-                command = self.cmdapp.precmd(command)
-                stop = self.cmdapp.onecmd(command)
-                stop = self.cmdapp.postcmd(stop, command)
+                stop = self.cmdapp.onecmd_plus_hooks(command)
                 #TODO: should act on ``stop``
                 result = self.outputTrap.read()
                 if line.startswith(self.cmdapp.prompt):
--- a/docs/freefeatures.rst	Tue Feb 16 15:13:11 2010 -0500
+++ b/docs/freefeatures.rst	Tue Feb 16 15:47:17 2010 -0500
@@ -42,6 +42,23 @@
   (Cmd) speak it was /* not */ delicious! # Yuck!
   it was  delicious!
 
+Commands at invocation
+======================
+
+You can send commands to your app as you invoke it by
+including them as extra arguments to the program.
+``cmd2`` interprets each argument as a separate 
+command, so you should enclose each command in 
+quotation marks if it is more than a one-word command.
+
+::
+
+  cat@eee:~/proj/cmd2/example$ python example.py "say hello" "say Gracie" quit
+  hello
+  Gracie
+  cat@eee:~/proj/cmd2/example$ 
+
+  
 Output redirection
 ==================
 
@@ -58,22 +75,6 @@
 .. _pywin32:: http://sourceforge.net/projects/pywin32/
 .. _xclip:: http://www.cyberciti.biz/faq/xclip-linux-insert-files-command-output-intoclipboard/
   
-Commands at invocation
-======================
-
-You can send commands to your app as you invoke it by
-including them as extra arguments to the program.
-``cmd2`` interprets each argument as a separate 
-command, so you should enclose each command in 
-quotation marks if it is more than a one-word command.
-
-::
-
-  cat@eee:~/proj/cmd2/example$ python example.py "say hello" "say Gracie" quit
-  hello
-  Gracie
-  cat@eee:~/proj/cmd2/example$ 
-
 Python
 ======
 
@@ -172,4 +173,12 @@
 Transcript-based testing
 ========================
 
-If a ``cmd2``-based application is invoked with --test
\ No newline at end of file
+If the entire transcript (input and output) of a successful session of
+a ``cmd2``-based app is copied from the screen and pasted into a text
+file, ``transcript.txt``, then a transcript test can be run against it::
+
+  python app.py --test transcript.txt
+  
+Any deviations between the output prescribed in ``transcript.txt`` and
+the actual output from a fresh run of the application will be reported
+as a unit test failure.
--- a/docs/pycon2010/pycon2010.rst	Tue Feb 16 15:13:11 2010 -0500
+++ b/docs/pycon2010/pycon2010.rst	Tue Feb 16 15:47:17 2010 -0500
@@ -201,17 +201,17 @@
 What you get
 ============
 
-    * Abbreviated commands
     * Script files
-    * Output redirection
     * Commands at invocation
+    * Output redirection    
     * Python
+    * Transcript-based testing
     
     * Searchable command history
     * Quitting the application
     * Comments
     * Misc. pre-defined commands
-    * Transcript-based testing
+    
 More
 ====