Candidates is a utility class to match command line input against a
authorThierry Parmentelat <thierry.parmentelat@sophia.inria.fr>
Wed, 4 Jul 2012 15:02:05 +0000 (17:02 +0200)
committerThierry Parmentelat <thierry.parmentelat@sophia.inria.fr>
Wed, 4 Jul 2012 15:02:05 +0000 (17:02 +0200)
fixed set of choices

sfa/client/sfaadmin.py

index 6cb3936..4ab9545 100755 (executable)
@@ -28,6 +28,21 @@ def args(*args, **kwargs):
         return func
     return _decorator
 
+### utility to match command-line args to names
+class Candidates:
+    def __init__ (self, names):
+        self.names=names
+    # is an input string acceptable for one of the known names?
+    @staticmethod
+    def fits (input, name):
+        return name.find(input)==0
+    # returns one of the names if the input name has a unique match
+    # or None otherwise
+    def only_match (self, input):
+        matches=[ name for name in self.names if Candidates.fits(input,name) ]
+        if len(matches)==1: return matches[0]
+        else: return None
+
 class Commands(object):
     def _get_commands(self):
         available_methods = []
@@ -448,3 +463,31 @@ class SfaAdmin:
         except Exception:
             print "Command failed, please check log for more info"
             raise
+
+candidates_specs=[
+('create delete reset resources slices start status stop version', 
+  [ ('ver','version'),
+    ('r',None),
+    ('re',None),
+    ('res',None),
+    ('rese','reset'),
+    ('reset','reset'),
+    ('reso','resources'),
+    ('sli','slices'),
+    ('st',None),
+    ('sta',None),
+    ('stop','stop'),
+    ('a',None),
+])
+]
+
+def test_candidates ():
+    for (names, tuples) in candidates_specs:
+        names=names.split()
+        for (input,expected) in tuples:
+            got=Candidates(names).only_match(input)
+            if got==expected: print '.',
+            else: print 'X FAIL','names[',names,'] input',input,'expected',expected,'got',got
+
+if __name__ == '__main__':
+    test_candidates()