f3a99ae3b7d2d7b49599555ccb7f7398792cb68c
[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         if input in self.names: return input
13         matches=[ name for name in self.names if Candidates.fits(input,name) ]
14         if len(matches)==1: return matches[0]
15         else: return None
16
17 #################### minimal test
18 candidates_specs=[
19 ('create delete reset resources slices start status stop version create_gid', 
20   [ ('ver','version'),
21     ('r',None),
22     ('re',None),
23     ('res',None),
24     ('rese','reset'),
25     ('reset','reset'),
26     ('reso','resources'),
27     ('sli','slices'),
28     ('st',None),
29     ('sta',None),
30     ('stop','stop'),
31     ('a',None),
32     ('cre',None),
33     ('create','create'),
34     ('create_','create_gid'),
35     ('create_g','create_gid'),
36     ('create_gi','create_gid'),
37     ('create_gid','create_gid'),
38 ])
39 ]
40
41 def test_candidates ():
42     for (names, tuples) in candidates_specs:
43         names=names.split()
44         for (input,expected) in tuples:
45             got=Candidates(names).only_match(input)
46             if got==expected: print '.',
47             else: print 'X FAIL','names[',names,'] input',input,'expected',expected,'got',got
48
49 if __name__ == '__main__':
50     test_candidates()