comparison cmd2.py @ 230:fea183146819

merged abbrev changes
author catherine@dellzilla
date Mon, 23 Mar 2009 10:16:59 -0400
parents a64555659169 caa475678e24
children 18bf83a77e0e
comparison
equal deleted inserted replaced
226:a64555659169 230:fea183146819
24 CHANGES: 24 CHANGES:
25 As of 0.3.0, options should be specified as `optparse` options. See README.txt. 25 As of 0.3.0, options should be specified as `optparse` options. See README.txt.
26 flagReader.py options are still supported for backward compatibility 26 flagReader.py options are still supported for backward compatibility
27 """ 27 """
28 import cmd, re, os, sys, optparse, subprocess, tempfile, pyparsing, doctest 28 import cmd, re, os, sys, optparse, subprocess, tempfile, pyparsing, doctest
29 import unittest, string, datetime, urllib 29 import unittest, string, datetime, urllib, inspect
30 from optparse import make_option 30 from optparse import make_option
31 __version__ = '0.4.8' 31 __version__ = '0.4.8'
32 32
33 class OptionParser(optparse.OptionParser): 33 class OptionParser(optparse.OptionParser):
34 def exit(self, status=0, msg=None): 34 def exit(self, status=0, msg=None):
231 shortcuts = {'?': 'help', '!': 'shell', '@': 'load' } 231 shortcuts = {'?': 'help', '!': 'shell', '@': 'load' }
232 excludeFromHistory = '''run r list l history hi ed edit li eof'''.split() 232 excludeFromHistory = '''run r list l history hi ed edit li eof'''.split()
233 noSpecialParse = 'set ed edit exit'.split() 233 noSpecialParse = 'set ed edit exit'.split()
234 defaultExtension = 'txt' 234 defaultExtension = 'txt'
235 default_file_name = 'command.txt' 235 default_file_name = 'command.txt'
236 settable = ['prompt', 'continuation_prompt', 'default_file_name', 'editor', 'case_insensitive', 236 abbrev = True
237 'echo', 'timing'] 237 settable = ['prompt', 'continuation_prompt', 'default_file_name', 'editor',
238 'case_insensitive', 'echo', 'timing', 'abbrev']
238 settable.sort() 239 settable.sort()
239 240
240 editor = os.environ.get('EDITOR') 241 editor = os.environ.get('EDITOR')
241 _STOP_AND_EXIT = 2 242 _STOP_AND_EXIT = 2
242 if not editor: 243 if not editor:
585 self.stdout = tempfile.TemporaryFile() 586 self.stdout = tempfile.TemporaryFile()
586 if statement.parsed.output == '>>': 587 if statement.parsed.output == '>>':
587 self.stdout.write(getPasteBuffer()) 588 self.stdout.write(getPasteBuffer())
588 try: 589 try:
589 # "heart" of the command, replace's cmd's onecmd() 590 # "heart" of the command, replace's cmd's onecmd()
590 self.lastcmd = statement.parsed.expanded 591 self.lastcmd = statement.parsed.expanded
591 try: 592 try:
592 func = getattr(self, 'do_' + statement.parsed.command) 593 func = getattr(self, 'do_' + statement.parsed.command)
593 except AttributeError: 594 except AttributeError:
594 return self.postparsing_postcmd(self.default(statement)) 595 func = None
596 if self.abbrev: # accept shortened versions of commands
597 funcs = [func for (fname, func) in inspect.getmembers(
598 self, inspect.ismethod)
599 if fname.startswith('do_' + statement.parsed.command)]
600 if len(funcs) == 1:
601 func = funcs[0]
602 if not func:
603 return self.postparsing_postcmd(self.default(statement))
595 timestart = datetime.datetime.now() 604 timestart = datetime.datetime.now()
596 stop = func(statement) 605 stop = func(statement)
597 if self.timing: 606 if self.timing:
598 print 'Elapsed: %s' % str(datetime.datetime.now() - timestart) 607 print 'Elapsed: %s' % str(datetime.datetime.now() - timestart)
599 except Exception, e: 608 except Exception, e: