From: Christian Heller Date: Wed, 6 Aug 2025 11:10:46 +0000 (+0200) Subject: Improve BaseTui.cmd__prompt__enter error detection/communication. X-Git-Url: https://plomlompom.com/repos/booking/static/%7B%7Bprefix%7D%7D/%7B%7Bdb.prefix%7D%7D/conditions?a=commitdiff_plain;h=0bffae604a2d5a4889284db1e39d8e020112bd5d;p=ircplom Improve BaseTui.cmd__prompt__enter error detection/communication. --- diff --git a/ircplom/tui_base.py b/ircplom/tui_base.py index 330fe0c..f789914 100644 --- a/ircplom/tui_base.py +++ b/ircplom/tui_base.py @@ -383,7 +383,7 @@ class BaseTui(QueueMixin): def _cmd_name_to_cmd(self, cmd_name: str) -> Optional[Callable]: 'Map cmd_name to executable TUI element method.' - cmd_name = CMD_SHORTCUTS.get(cmd_name, cmd_name) # window.connect + cmd_name = CMD_SHORTCUTS.get(cmd_name, cmd_name) ancestors = [self] steps = cmd_name.split('.') method_name = f'cmd__{steps[-1]}' @@ -441,27 +441,31 @@ class BaseTui(QueueMixin): if not to_parse: return alert: Optional[str] = None - if to_parse[0:1] == '/': - toks = to_parse[1:].split(maxsplit=1) - alert = f'{toks[0]} unknown' - cmd = self._cmd_name_to_cmd(toks[0]) - if cmd and cmd.__name__ != stack()[0].function: + if to_parse[0] == '/': + toks = to_parse.split(maxsplit=1) + cmd_name = toks.pop(0) + cmd = self._cmd_name_to_cmd(cmd_name[1:]) + if not cmd: + alert = f'{cmd_name} unknown' + elif cmd.__name__ == stack()[0].function: + alert = f'{cmd_name} would loop into ourselves' + else: params = signature(cmd).parameters n_args_max = len(params) - n_args_min = len([p for p in params.values() - if p.default == inspect_empty]) - alert = f'{cmd.__name__} needs between {n_args_min} and '\ - f'{n_args_max} args' - if len(toks) == 1 and not n_args_min: - alert = cmd() - elif len(toks) > 1 and params\ - and n_args_min <= len(toks[1].split()): - args = [] - while len(toks) > 1 and n_args_max: - toks = toks[1].split(maxsplit=1) - args += [toks[0]] - n_args_max -= 1 - alert = cmd(*args) + if toks and not n_args_max: + alert = f'{cmd_name} given argument(s) while none expected' + else: + n_args_min = len([p for p in params.values() + if p.default == inspect_empty]) + args: list[str] = [] + while toks and len(args) < n_args_max: + toks = toks[0].split(maxsplit=1) + args += [toks.pop(0)] + if len(args) < n_args_min: + alert = f'{cmd_name} too few arguments '\ + + f'(given {len(args)}, need {n_args_min})' + else: + alert = cmd(*args) else: alert = 'not prefixed by /' if alert: