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 <jan@janware.com>
This commit is contained in:
Jan Lindemann 2026-02-20 12:04:10 +01:00
commit e5e0cf9930

View file

@ -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