comparison cmd2.py @ 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 414a78c16ce9
comparison
equal deleted inserted replaced
90:1cd189536e90 91:88f2aa240af1
509 509
510 os.system('%s %s' % (self.editor, filename)) 510 os.system('%s %s' % (self.editor, filename))
511 self.do__load(filename) 511 self.do__load(filename)
512 do_edit = do_ed 512 do_edit = do_ed
513 513
514 def do_save(self, fname=None): 514 saveparser = (pyparsing.Optional(pyparsing.Word(pyparsing.nums)^'*')("idx") +
515 """Saves most recent command to a file.""" 515 pyparsing.Optional(pyparsing.Word(pyparsing.printables))("fname") +
516 516 pyparsing.stringEnd)
517 if not fname: 517 def do_save(self, arg):
518 fname = self.defaultFileName 518 """`save [N] [filename.ext]`
519 Saves command from history to file.
520 N => Number of command (from history), or `*`;
521 most recent command if omitted"""
522
523 try:
524 args = self.saveparser.parseString(arg)
525 except pyparsing.ParseException:
526 print self.do_save.__doc__
527 return
528 fname = args.fname or self.defaultFileName
529 if args.idx == '*':
530 saveme = '\n\n'.join(self.history[:])
531 elif args.idx:
532 saveme = self.history[int(args.idx)-1]
533 else:
534 saveme = self.history[-1]
519 try: 535 try:
520 f = open(fname, 'w') 536 f = open(fname, 'w')
521 f.write(self.history[-1]) 537 f.write(saveme)
522 f.close() 538 f.close()
539 print 'Saved to %s' % (fname)
523 except Exception, e: 540 except Exception, e:
524 print 'Error saving %s: %s' % (fname, str(e)) 541 print 'Error saving %s: %s' % (fname, str(e))
525 542
526 def do_load(self, fname=None): 543 def do_load(self, fname=None):
527 """Runs command(s) from a file.""" 544 """Runs command(s) from a file."""