home · contact · privacy
Improve BaseTui.cmd__prompt__enter error detection/communication.
authorChristian Heller <c.heller@plomlompom.de>
Wed, 6 Aug 2025 11:10:46 +0000 (13:10 +0200)
committerChristian Heller <c.heller@plomlompom.de>
Wed, 6 Aug 2025 11:10:46 +0000 (13:10 +0200)
ircplom/tui_base.py

index 330fe0c6709bb35e1ae01226b004ad67cc84d8ee..f78991406de7c8f2ec0d39e21d7483606f0cb8f3 100644 (file)
@@ -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: