changeset 91:88f2aa240af1 0.3.6

added to save
author catherine@Elli.myhome.westell.com
date Thu, 04 Sep 2008 11:30:26 -0400
parents 1cd189536e90
children 25101e7b078f
files cmd2.py
diffstat 1 files changed, 23 insertions(+), 6 deletions(-) [+]
line wrap: on
line diff
--- a/cmd2.py	Wed Sep 03 17:32:49 2008 -0400
+++ b/cmd2.py	Thu Sep 04 11:30:26 2008 -0400
@@ -511,15 +511,32 @@
         self.do__load(filename)
     do_edit = do_ed
     
-    def do_save(self, fname=None):
-        """Saves most recent command to a file."""
-        
-        if not fname:
-            fname = self.defaultFileName
+    saveparser = (pyparsing.Optional(pyparsing.Word(pyparsing.nums)^'*')("idx") + 
+                  pyparsing.Optional(pyparsing.Word(pyparsing.printables))("fname") +
+                  pyparsing.stringEnd)    
+    def do_save(self, arg):
+        """`save [N] [filename.ext]`
+        Saves command from history to file.
+        N => Number of command (from history), or `*`; 
+             most recent command if omitted"""
+
+        try:
+            args = self.saveparser.parseString(arg)
+        except pyparsing.ParseException:
+            print self.do_save.__doc__
+            return
+        fname = args.fname or self.defaultFileName
+        if args.idx == '*':
+            saveme = '\n\n'.join(self.history[:])
+        elif args.idx:
+            saveme = self.history[int(args.idx)-1]
+        else:
+            saveme = self.history[-1]
         try:
             f = open(fname, 'w')
-            f.write(self.history[-1])
+            f.write(saveme)
             f.close()
+            print 'Saved to %s' % (fname)
         except Exception, e:
             print 'Error saving %s: %s' % (fname, str(e))