a49f44298093791ee22a7777368234d21f41306f
[sfa.git] / sfa / client / candidates.py
1 ### utility to match command-line args to names
2 class Candidates:
3     def __init__ (self, names):
4         self.names=names
5     # is an input string acceptable for one of the known names?
6     @staticmethod
7     def fits (input, name):
8         return name.find(input)==0
9     # returns one of the names if the input name has a unique match
10     # or None otherwise
11     def only_match (self, input):
12         matches=[ name for name in self.names if Candidates.fits(input,name) ]
13         if len(matches)==1: return matches[0]
14         else: return None
15
16 #################### minimal test
17 candidates_specs=[
18 ('create delete reset resources slices start status stop version', 
19   [ ('ver','version'),
20     ('r',None),
21     ('re',None),
22     ('res',None),
23     ('rese','reset'),
24     ('reset','reset'),
25     ('reso','resources'),
26     ('sli','slices'),
27     ('st',None),
28     ('sta',None),
29     ('stop','stop'),
30     ('a',None),
31 ])
32 ]
33
34 def test_candidates ():
35     for (names, tuples) in candidates_specs:
36         names=names.split()
37         for (input,expected) in tuples:
38             got=Candidates(names).only_match(input)
39             if got==expected: print '.',
40             else: print 'X FAIL','names[',names,'] input',input,'expected',expected,'got',got
41
42 if __name__ == '__main__':
43     test_candidates()