Cmd / Cmds: Add config file support

An application based on Cmds will now try to read StringTree
formatted config data from a file $HOME/.<name>rc, and make the data
available via the newly added conf_value() method.

Signed-off-by: Jan Lindemann <jan@janware.com>
This commit is contained in:
Jan Lindemann 2022-12-08 16:44:49 +01:00
commit a4fb791649
2 changed files with 31 additions and 0 deletions

View file

@ -22,6 +22,7 @@ class Cmd(abc.ABC): # export
self.parent = None
self.children: List[Cmd] = []
self.child_classes: List[Type[Cmd]] = []
self.app: Optional[Cmds] = None
async def _run(self, args):
pass
@ -52,3 +53,9 @@ 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