From 2a1362bbbb19976af95ec5d07d5fa949085f2e27 Mon Sep 17 00:00:00 2001 From: Thierry Parmentelat Date: Thu, 5 Jul 2012 18:58:15 +0200 Subject: [PATCH] sfi can use partial non-ambiguous command names like sfaadmin --- sfa/client/candidates.py | 15 +++++++++++++++ sfa/client/sfaadmin.py | 17 ++--------------- sfa/client/sfi.py | 10 +++++++++- 3 files changed, 26 insertions(+), 16 deletions(-) create mode 100644 sfa/client/candidates.py diff --git a/sfa/client/candidates.py b/sfa/client/candidates.py new file mode 100644 index 00000000..77eef7d6 --- /dev/null +++ b/sfa/client/candidates.py @@ -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 + diff --git a/sfa/client/sfaadmin.py b/sfa/client/sfaadmin.py index c1738cdc..d676de64 100755 --- a/sfa/client/sfaadmin.py +++ b/sfa/client/sfaadmin.py @@ -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 = [] diff --git a/sfa/client/sfi.py b/sfa/client/sfi.py index ff5f6cef..55c457af 100644 --- a/sfa/client/sfi.py +++ b/sfa/client/sfi.py @@ -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 -- 2.47.0