improve commandline argument behaviour

+ allow hprt2 / zebra2
This commit is contained in:
zolliker 2023-06-05 14:38:14 +02:00
parent fcef9cb142
commit 6130c5b5c5
5 changed files with 48 additions and 38 deletions

View File

@ -39,12 +39,12 @@ class SewebManager(ServiceManager):
USAGE = """ USAGE = """
Usage: Usage:
seaweb list [<instance>] seaweb list [instance]
seaweb start <instance> seaweb start <instance>
seaweb restart <instance> seaweb restart <instance>
seaweb stop <instance> seaweb stop <instance>
<instance> is one of %s %s
""" """
@ -57,37 +57,45 @@ def run(group, arglist):
defaults = parser['DEFAULT'] defaults = parser['DEFAULT']
managers = {cls.group: cls() for cls in all if cls.group + '_command' in defaults} managers = {cls.group: cls() for cls in all if cls.group + '_command' in defaults}
serv = managers[group] serv = managers[group]
args = dict(action='gui', ins=serv.main_ins) # args = dict(action='gui', ins=serv.main_ins)
ACTION = 1 args = {}
INS = 2 guessed_args = []
SERVICE = 2 if serv.main_ins else 3
extra = [] extra = []
pos = 0
unorder = False arg_is = {
for arg in arglist: 'action': lambda arg: hasattr(serv, 'do_' + arg),
if hasattr(serv, 'do_' + arg): 'ins': lambda arg: arg in serv.info or arg == 'all' or serv.wildcard(arg),
args['action'] = arg 'service': lambda arg: arg in serv.services,
if pos >= ACTION: }
unorder = True
pos = ACTION for kind in 'action', 'ins', 'service':
elif arg in serv.services: if arglist and arg_is[kind](arglist[0]): # arg is of expected kind
args['service'] = arg args[kind] = arglist.pop(0)
if pos >= SERVICE: continue
unorder = True if guessed_args and guessed_args[-1][0] == kind: # last arg was skipped
pos = SERVICE args[kind] = guessed_args.pop()[1]
elif arg in serv.info or arg == 'all' or serv.wildcard(arg): continue
args['ins'] = arg if not arglist:
if pos >= INS: break
unorder = True arg = arglist.pop(0)
pos = INS for kind, is_kind in arg_is.items():
if is_kind(arg):
guessed_args.append((kind, arg))
break
else: else:
extra.append(arg) 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' % print('do you mean:\n %s %s %s %s %s' %
(group, args.get('action', ''), args.get('ins', ''), args.get('service', ''), ' '.join(extra))) (group, args.get('action', ''), args.get('ins', ''), args.get('service', ''), ' '.join(extra)))
else: else:
try: try:
serv.action(args['action'], *serv.treat_args(args, extra)) serv.action(args['action'], *serv.treat_args(args, extra + arglist))
except AttributeError: except AttributeError:
raise raise
except UsageError as e: except UsageError as e:

View File

@ -138,7 +138,7 @@ class ServiceManager:
for ins in parser.sections(): for ins in parser.sections():
section = dict(parser[ins]) section = dict(parser[ins])
if ins == 'MAIN': 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: if 'REMOTE_HOST' in section:
self.remote_hosts[ins] = section['REMOTE_HOST'] self.remote_hosts[ins] = section['REMOTE_HOST']
command = section.get('%s_command' % self.group) command = section.get('%s_command' % self.group)
@ -470,7 +470,8 @@ class ServiceManager:
def do_help(self, *args): def do_help(self, *args):
if self.main_ins: if self.main_ins:
usage = self.USAGE.replace(' <instance>', '').replace(' [<instance>]', '') % '' usage = self.USAGE.replace('<instance>', '[instance]') % (
'[instance] is empty or one of %s' % ', '.join(self.info))
else: else:
usage = self.USAGE % ('<instance> is one of %s' % ', '.join(self.info)) usage = self.USAGE % ('<instance> is one of %s' % ', '.join(self.info))
print(usage) print(usage)
@ -479,7 +480,7 @@ class ServiceManager:
if unknown: if unknown:
raise UsageError('unknown argument: %s' % (' '.join(unknown))) raise UsageError('unknown argument: %s' % (' '.join(unknown)))
if extra: 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')] args = [argdict.get('ins'), argdict.get('service')]
while args and not args[-1]: while args and not args[-1]:
args.pop() args.pop()

View File

@ -34,7 +34,7 @@ class FrappyManager(ServiceManager):
USAGE = """ USAGE = """
Usage: Usage:
frappy list [<instance>] * frappy list [instance] *
frappy start <instance> <service> <cfgfiles> frappy start <instance> <service> <cfgfiles>
frappy restart <instance> [<service>] [<cfgfiles>] * frappy restart <instance> [<service>] [<cfgfiles>] *
frappy stop <instance> [<service>] * frappy stop <instance> [<service>] *

View File

@ -63,7 +63,7 @@ class NicosManager(ServiceManager):
nicos create <instance> <nr> * nicos create <instance> <nr> *
nicos link <instance> (create links to nicos data and scripts) nicos link <instance> (create links to nicos data and scripts)
<service> is one of main, stick, addons <service> is one of cache, deamon, poller
%s %s
* wildcards allowed, using '.' to replace 0 or more arbitrary characters in <instance> * wildcards allowed, using '.' to replace 0 or more arbitrary characters in <instance>

View File

@ -58,12 +58,12 @@ class SeaManager(ServiceManager):
sea gui <instance> sea gui <instance>
sea <instance> # the same as sea gui <instance> sea <instance> # the same as sea gui <instance>
sea cli <instance> # the same as old seacmd sea cli <instance> # the same as old seacmd
sea start <instance> [<service>] * # the same as old 'monit start sea' sea start <instance> [service] * # the same as old 'monit start sea'
sea restart <instance> [<service>] * sea restart <instance> [service] *
sea stop <instance> [<service>] * sea stop <instance> [service] *
sea list [<instance>] * sea list [instance] *
<service> is one of sea, graph [service] is empty or one of sea, graph
%s %s
* wildcards allowed, using '.' to replace 0 or more arbitrary characters in <instance> * wildcards allowed, using '.' to replace 0 or more arbitrary characters in <instance>
""" """
@ -132,6 +132,7 @@ class SeaManager(ServiceManager):
if match: if match:
_, key, dev, addon, _ = match.groups() _, key, dev, addon, _ = match.groups()
if addon: if addon:
if addon != result[1]:
result.append(addon) result.append(addon)
elif key == 'name': elif key == 'name':
result[0] = dev result[0] = dev