mirror of
ssh://git.janware.com/janware/proj/jw-pkg
synced 2026-04-28 11:25:23 +02:00
lib.Cmd: Add argument "parent" to __init__()
During __init__(), commands have no idea of their parent. This is not a problem as of now, but is easy to fix, and it's architecturally desirable to be prepared just in case, so add the parent argument to the ctor before more commands are added. Signed-off-by: Jan Lindemann <jan@janware.com>
This commit is contained in:
parent
132dce8b3f
commit
f175f9d5c9
32 changed files with 103 additions and 88 deletions
|
|
@ -10,15 +10,12 @@ from argparse import ArgumentParser, _SubParsersAction
|
|||
from .log import *
|
||||
from .Types import Types
|
||||
|
||||
# full blown example of one level of nested subcommands
|
||||
# git -C project remote -v show -n myremote
|
||||
|
||||
class Cmd(abc.ABC): # export
|
||||
|
||||
def __init__(self, name: str, help: str) -> None:
|
||||
def __init__(self, parent: App|Cmd, name: str, help: str) -> None:
|
||||
from . import App
|
||||
self.__parent: App|Cmd|None = parent
|
||||
self.__app: App|None = None
|
||||
self.__parent: App|Cmd|None = None
|
||||
self.__name = name
|
||||
self.__help = help
|
||||
self.__children: list[Cmd] = []
|
||||
|
|
@ -37,7 +34,7 @@ class Cmd(abc.ABC): # export
|
|||
return self.__parent
|
||||
|
||||
@property
|
||||
def app(self):
|
||||
def app(self) -> App:
|
||||
from .App import App
|
||||
if self.__app is None:
|
||||
parent = self.__parent
|
||||
|
|
@ -85,8 +82,7 @@ class Cmd(abc.ABC): # export
|
|||
if cmd_class in self.__child_classes:
|
||||
continue
|
||||
self.__child_classes.append(cmd_class)
|
||||
cmd = cmd_class()
|
||||
cmd.set_parent(self)
|
||||
cmd = cmd_class(self)
|
||||
self.__children.append(cmd)
|
||||
assert len(self.__children) == len(self.__child_classes)
|
||||
except Exception as e:
|
||||
|
|
@ -99,9 +95,3 @@ class Cmd(abc.ABC): # export
|
|||
# Will be called from App base class constructor and set up the parser hierarchy
|
||||
def add_arguments(self, parser: ArgumentParser) -> None:
|
||||
pass
|
||||
|
||||
def conf_value(self, path, default=None):
|
||||
ret = None if self.app is None else self.app.conf_value(path, default)
|
||||
if ret is None and default is not None:
|
||||
return default
|
||||
return ret
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue