changeset 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
files cmd2.py
diffstat 1 files changed, 16 insertions(+), 7 deletions(-) [+]
line wrap: on
line diff
--- a/cmd2.py	Thu Mar 19 17:34:21 2009 -0400
+++ b/cmd2.py	Mon Mar 23 05:54:43 2009 -0400
@@ -26,7 +26,7 @@
 flagReader.py options are still supported for backward compatibility
 """
 import cmd, re, os, sys, optparse, subprocess, tempfile, pyparsing, doctest
-import unittest, string, datetime, urllib
+import unittest, string, datetime, urllib, inspect
 from optparse import make_option
 __version__ = '0.4.8'
 
@@ -233,8 +233,9 @@
     noSpecialParse = 'set ed edit exit'.split()
     defaultExtension = 'txt'
     default_file_name = 'command.txt'
-    settable = ['prompt', 'continuation_prompt', 'default_file_name', 'editor', 'case_insensitive', 
-                'echo', 'timing']
+    abbrev = True
+    settable = ['prompt', 'continuation_prompt', 'default_file_name', 'editor',
+                'case_insensitive', 'echo', 'timing', 'abbrev']
     settable.sort()
     
     editor = os.environ.get('EDITOR')
@@ -588,10 +589,18 @@
         try:
             # "heart" of the command, replace's cmd's onecmd()
             self.lastcmd = statement.parsed.expanded
-            try:
-                func = getattr(self, 'do_' + statement.parsed.command)
-            except AttributeError:
-                return self.postparsing_postcmd(self.default(statement))
+            if self.abbrev:   # accept shortened versions of commands
+                funcs = [func for (fname, func) in inspect.getmembers(self, inspect.ismethod) 
+                         if fname.startswith('do_' + statement.parsed.command)]
+                if len(funcs) == 1:
+                    func = funcs[0]
+                else:
+                    return self.postparsing_postcmd(self.default(statement))                
+            else:
+                try:
+                    func = getattr(self, 'do_' + statement.parsed.command)
+                except AttributeError:
+                    return self.postparsing_postcmd(self.default(statement))                
             timestart = datetime.datetime.now()
             stop = func(statement) 
             if self.timing: