comparison cmd2.py @ 227:82b6e0881b78

abbrevs - but stumbling when an abbrev is predefined
author catherine@Elli.myhome.westell.com
date Mon, 23 Mar 2009 05:54:43 -0400
parents 061db156c99f
children 68b444aeaf8b
comparison
equal deleted inserted replaced
225:061db156c99f 227:82b6e0881b78
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:
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 if self.abbrev: # accept shortened versions of commands
592 func = getattr(self, 'do_' + statement.parsed.command) 593 funcs = [func for (fname, func) in inspect.getmembers(self, inspect.ismethod)
593 except AttributeError: 594 if fname.startswith('do_' + statement.parsed.command)]
594 return self.postparsing_postcmd(self.default(statement)) 595 if len(funcs) == 1:
596 func = funcs[0]
597 else:
598 return self.postparsing_postcmd(self.default(statement))
599 else:
600 try:
601 func = getattr(self, 'do_' + statement.parsed.command)
602 except AttributeError:
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: