# HG changeset patch # User catherine@Elli.myhome.westell.com # Date 1220542226 14400 # Node ID 88f2aa240af1f699dda30e82ba8b46730fc37d51 # Parent 1cd189536e90c5773358a9ef878920d985c3190c added to save diff -r 1cd189536e90 -r 88f2aa240af1 cmd2.py --- 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))