mirror of
ssh://git.janware.com/janware/proj/jw-pkg
synced 2026-04-25 09:35:54 +02:00
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:
parent
6916d7edc8
commit
e5e0cf9930
1 changed files with 4 additions and 6 deletions
|
|
@ -21,8 +21,10 @@ class Cmd(abc.ABC): # export
|
||||||
self.__children: list[Cmd] = []
|
self.__children: list[Cmd] = []
|
||||||
self.__child_classes: list[type[Cmd]] = []
|
self.__child_classes: list[type[Cmd]] = []
|
||||||
|
|
||||||
async def _run(self, args):
|
@abc.abstractmethod
|
||||||
pass
|
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):
|
def set_parent(self, parent: Any|Cmd):
|
||||||
self.__parent = parent
|
self.__parent = parent
|
||||||
|
|
@ -67,10 +69,6 @@ class Cmd(abc.ABC): # export
|
||||||
async def run(self, args):
|
async def run(self, args):
|
||||||
return await self._run(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:
|
def add_subcommands(self, cmds: Cmd|list[Cmds]|Types|list[Types]) -> None:
|
||||||
if isinstance(cmds, Cmd):
|
if isinstance(cmds, Cmd):
|
||||||
assert False
|
assert False
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue