diff --git a/__init__.py b/__init__.py index 983d73e..cd847cc 100644 --- a/__init__.py +++ b/__init__.py @@ -39,12 +39,12 @@ class SewebManager(ServiceManager): USAGE = """ Usage: - seaweb list [] + seaweb list [instance] seaweb start seaweb restart seaweb stop - is one of %s + %s """ @@ -57,37 +57,45 @@ def run(group, arglist): defaults = parser['DEFAULT'] managers = {cls.group: cls() for cls in all if cls.group + '_command' in defaults} serv = managers[group] - args = dict(action='gui', ins=serv.main_ins) - ACTION = 1 - INS = 2 - SERVICE = 2 if serv.main_ins else 3 + # args = dict(action='gui', ins=serv.main_ins) + args = {} + guessed_args = [] extra = [] - pos = 0 - unorder = False - for arg in arglist: - if hasattr(serv, 'do_' + arg): - args['action'] = arg - if pos >= ACTION: - unorder = True - pos = ACTION - elif arg in serv.services: - args['service'] = arg - if pos >= SERVICE: - unorder = True - pos = SERVICE - elif arg in serv.info or arg == 'all' or serv.wildcard(arg): - args['ins'] = arg - if pos >= INS: - unorder = True - pos = INS + + arg_is = { + 'action': lambda arg: hasattr(serv, 'do_' + arg), + 'ins': lambda arg: arg in serv.info or arg == 'all' or serv.wildcard(arg), + 'service': lambda arg: arg in serv.services, + } + + for kind in 'action', 'ins', 'service': + if arglist and arg_is[kind](arglist[0]): # arg is of expected kind + args[kind] = arglist.pop(0) + continue + if guessed_args and guessed_args[-1][0] == kind: # last arg was skipped + args[kind] = guessed_args.pop()[1] + continue + if not arglist: + break + arg = arglist.pop(0) + for kind, is_kind in arg_is.items(): + if is_kind(arg): + guessed_args.append((kind, arg)) + break else: extra.append(arg) - if unorder: + + args.update(guessed_args) + args.setdefault('action', 'gui') + if serv.main_ins: + args.setdefault('ins', serv.main_ins) + if guessed_args: + args.setdefault('action', 'gui') print('do you mean:\n %s %s %s %s %s' % (group, args.get('action', ''), args.get('ins', ''), args.get('service', ''), ' '.join(extra))) - else: + else: try: - serv.action(args['action'], *serv.treat_args(args, extra)) + serv.action(args['action'], *serv.treat_args(args, extra + arglist)) except AttributeError: raise except UsageError as e: diff --git a/base.py b/base.py index 62a182e..ead47e4 100644 --- a/base.py +++ b/base.py @@ -138,7 +138,7 @@ class ServiceManager: for ins in parser.sections(): section = dict(parser[ins]) if ins == 'MAIN': - ins = self.main_ins = basename(expanduser('~')) + ins = self.main_ins = os.environ.get('Instrument') or basename(expanduser('~')) if 'REMOTE_HOST' in section: self.remote_hosts[ins] = section['REMOTE_HOST'] command = section.get('%s_command' % self.group) @@ -470,7 +470,8 @@ class ServiceManager: def do_help(self, *args): if self.main_ins: - usage = self.USAGE.replace(' ', '').replace(' []', '') % '' + usage = self.USAGE.replace('', '[instance]') % ( + '[instance] is empty or one of %s' % ', '.join(self.info)) else: usage = self.USAGE % (' is one of %s' % ', '.join(self.info)) print(usage) @@ -479,7 +480,7 @@ class ServiceManager: if unknown: raise UsageError('unknown argument: %s' % (' '.join(unknown))) if extra: - return [argdict.get('ins'), argdict.get('service')] + extra + return [argdict.get('ins'), argdict.get('service')] + list(extra) args = [argdict.get('ins'), argdict.get('service')] while args and not args[-1]: args.pop() diff --git a/frappyman.py b/frappyman.py index 7fdb949..9d03874 100644 --- a/frappyman.py +++ b/frappyman.py @@ -34,7 +34,7 @@ class FrappyManager(ServiceManager): USAGE = """ Usage: - frappy list [] * + frappy list [instance] * frappy start frappy restart [] [] * frappy stop [] * diff --git a/nicosman.py b/nicosman.py index 00f1ef9..f984a78 100644 --- a/nicosman.py +++ b/nicosman.py @@ -63,7 +63,7 @@ class NicosManager(ServiceManager): nicos create * nicos link (create links to nicos data and scripts) - is one of main, stick, addons + is one of cache, deamon, poller %s * wildcards allowed, using '.' to replace 0 or more arbitrary characters in diff --git a/seaman.py b/seaman.py index 42326f5..612cb14 100644 --- a/seaman.py +++ b/seaman.py @@ -58,12 +58,12 @@ class SeaManager(ServiceManager): sea gui sea # the same as sea gui sea cli # the same as old seacmd - sea start [] * # the same as old 'monit start sea' - sea restart [] * - sea stop [] * - sea list [] * + sea start [service] * # the same as old 'monit start sea' + sea restart [service] * + sea stop [service] * + sea list [instance] * - is one of sea, graph + [service] is empty or one of sea, graph %s * wildcards allowed, using '.' to replace 0 or more arbitrary characters in """ @@ -132,7 +132,8 @@ class SeaManager(ServiceManager): if match: _, key, dev, addon, _ = match.groups() if addon: - result.append(addon) + if addon != result[1]: + result.append(addon) elif key == 'name': result[0] = dev else: