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