From e5e0cf9930bea62b30ff7c0c1c8f60375079fafe Mon Sep 17 00:00:00 2001 From: Jan Lindemann Date: Fri, 20 Feb 2026 12:04:10 +0100 Subject: [PATCH] jw.pkg.lib.Cmd._run(): Call parent._run() jw.pkg.lib.Cmd._run() is abstract, but it's nice to give it a default implementation which calls self.parent._run() in case parent is also a command class. That allows for some default processing in _run() for each node up the parent chain. The children / derived classes just need to make sure all classes in the hierarchy do: async def _run(self, args): return await super()._run(args) ... add main command logic here .. Signed-off-by: Jan Lindemann --- src/python/jw/pkg/lib/Cmd.py | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/src/python/jw/pkg/lib/Cmd.py b/src/python/jw/pkg/lib/Cmd.py index a5cdf489..ccdbf44b 100644 --- a/src/python/jw/pkg/lib/Cmd.py +++ b/src/python/jw/pkg/lib/Cmd.py @@ -21,8 +21,10 @@ class Cmd(abc.ABC): # export self.__children: list[Cmd] = [] self.__child_classes: list[type[Cmd]] = [] - async def _run(self, args): - pass + @abc.abstractmethod + async def _run(self, args) -> None: + if isinstance(self.__parent, Cmd): # Calling App.run() would loop + return await self.__parent._run(args) def set_parent(self, parent: Any|Cmd): self.__parent = parent @@ -67,10 +69,6 @@ class Cmd(abc.ABC): # export async def run(self, args): return await self._run(args) - @abc.abstractmethod - async def _run(self, args): - pass - def add_subcommands(self, cmds: Cmd|list[Cmds]|Types|list[Types]) -> None: if isinstance(cmds, Cmd): assert False