sfi can use partial non-ambiguous command names like sfaadmin
authorThierry Parmentelat <thierry.parmentelat@sophia.inria.fr>
Thu, 5 Jul 2012 16:58:15 +0000 (18:58 +0200)
committerThierry Parmentelat <thierry.parmentelat@sophia.inria.fr>
Thu, 5 Jul 2012 16:58:15 +0000 (18:58 +0200)
sfa/client/candidates.py [new file with mode: 0644]
sfa/client/sfaadmin.py
sfa/client/sfi.py

diff --git a/sfa/client/candidates.py b/sfa/client/candidates.py
new file mode 100644 (file)
index 0000000..77eef7d
--- /dev/null
@@ -0,0 +1,15 @@
+### 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
+
index c1738cd..d676de6 100755 (executable)
@@ -12,6 +12,8 @@ from sfa.client.sfi import save_records_to_file
 from sfa.trust.hierarchy import Hierarchy
 from sfa.trust.gid import GID
 
+from sfa.client.candidates import Candidates
+
 pprinter = PrettyPrinter(indent=4)
 
 try:
@@ -28,21 +30,6 @@ 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):
         command_names = []
index ff5f6ce..55c457a 100644 (file)
@@ -40,6 +40,7 @@ from sfa.client.sfaclientlib import SfaClientBootstrap
 from sfa.client.sfaserverproxy import SfaServerProxy, ServerException
 from sfa.client.client_helper import pg_users_arg, sfa_users_arg
 from sfa.client.return_value import ReturnValue
+from sfa.client.candidates import Candidates
 
 CM_PORT=12346
 
@@ -502,7 +503,14 @@ class Sfi:
             self.print_command_help(options)
             return -1
     
-        command = args[0]
+        # complete / find unique match with command set
+        command_candidates = Candidates (self.available_names)
+        input = args[0]
+        command = command_candidates.only_match(input)
+        if not command:
+            self.print_command_help(options)
+            sys.exit(1)
+        # second pass options parsing
         self.command_parser = self.create_command_parser(command)
         (command_options, command_args) = self.command_parser.parse_args(args[1:])
         self.command_options = command_options