summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCatherine Devlin <catherine.devlin@gmail.com>2008-09-04 11:30:26 -0400
committerCatherine Devlin <catherine.devlin@gmail.com>2008-09-04 11:30:26 -0400
commit944106523cb751d9f577a4e3aebaaac1290d30cd (patch)
tree536b62ea1d09b157a5ca19c84b75251a38188437
parent545d29463cfba493db8c45c96bbddd66e53fc963 (diff)
downloadcmd2-git-0.3.6.tar.gz
added to save0.3.6
-rwxr-xr-xcmd2.py29
1 files changed, 23 insertions, 6 deletions
diff --git a/cmd2.py b/cmd2.py
index dfa5d57c..dddbf3fd 100755
--- a/cmd2.py
+++ b/cmd2.py
@@ -511,15 +511,32 @@ class Cmd(cmd.Cmd):
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))