Improve morituri configuration handling, help

thomasvs/morituri#141
This commit is contained in:
JoeLametta
2015-11-29 16:28:06 +01:00
parent 735596c401
commit ace8d5ac22
3 changed files with 56 additions and 7 deletions

View File

@@ -34,6 +34,33 @@ class LogCommand(command.Command, log.Loggable):
command.Command.__init__(self, parentCommand, **kwargs)
self.logCategory = self.name
def parse(self, argv):
cmd = self.getRootCommand()
if hasattr(cmd, 'config'):
config = cmd.config
# find section name
cmd = self
section = []
while cmd is not None:
section.insert(0, cmd.name)
cmd = cmd.parentCommand
section = '.'.join(section)
# get defaults from config
defaults = {}
for opt in self.parser.option_list:
if opt.dest is None:
continue
if 'string' == opt.type:
val = config.get(section, opt.dest)
elif opt.action in ('store_false', 'store_true'):
val = config.getboolean(section, opt.dest)
else:
val = None
if val is not None:
defaults[opt.dest] = val
self.parser.set_defaults(**defaults)
command.Command.parse(self, argv)
# command.Command has a fake debug method, so choose the right one
def debug(self, format, *args):