over.app.Main now uses a features dict instead of individual flags, closes #5

This commit is contained in:
Martinez 2016-05-17 09:52:26 +02:00
parent 123b04c2b7
commit 720dfa7b09
2 changed files with 18 additions and 7 deletions

View file

@ -162,6 +162,12 @@ def expand_group(token, options):
# -------------------------------------------------- # --------------------------------------------------
class Main: class Main:
default_features = {
"use_cfg_file": False,
"auto_add_help": True,
"handle_exceptions": True
}
""" """
Application backbone. Provides: Application backbone. Provides:
* A configuration system consisting of * A configuration system consisting of
@ -172,7 +178,7 @@ class Main:
* A prettyprinting exception handler. * A prettyprinting exception handler.
""" """
def __init__(self, name, version=None, license=None, use_cfg_file=False, auto_add_help=True, handle_exceptions=True): def __init__(self, name, version=None, license=None, features={}):
self.name = name self.name = name
self.version = version self.version = version
self.license = license self.license = license
@ -181,14 +187,20 @@ class Main:
self.options_by_abbr = OrderedDict() self.options_by_abbr = OrderedDict()
self.cfg = ConfigRouter(self.options) self.cfg = ConfigRouter(self.options)
self.targets = [] self.targets = []
self.auto_add_help = auto_add_help
self.invocation = cmd.format_invocation(sys.argv) self.invocation = cmd.format_invocation(sys.argv)
self.features = types.ndict()
if use_cfg_file: for feature_name in self.default_features:
if feature_name in features:
self.features[feature_name] = features[feature_name]
else:
self.features[feature_name] = self.default_features[feature_name]
if self.features.use_cfg_file:
# TODO ensure it exists, update it with new values, etc. # TODO ensure it exists, update it with new values, etc.
... ...
if handle_exceptions: if self.features.handle_exceptions:
sys.excepthook = self.stack_tracer sys.excepthook = self.stack_tracer
def __repr__(self): def __repr__(self):
@ -278,7 +290,7 @@ class Main:
cmdline = cmdline or sys.argv[1:] cmdline = cmdline or sys.argv[1:]
# placing it here ensures --help is the last option in the list of options # placing it here ensures --help is the last option in the list of options
if self.auto_add_help: if self.features.auto_add_help:
self.enable_help() self.enable_help()
last_read_option = None last_read_option = None

View file

@ -28,7 +28,7 @@ def tri_callback(a, b, c):
return "%s, %s and %s" %(a, b, c) return "%s, %s and %s" %(a, b, c)
if __name__ == "__main__": if __name__ == "__main__":
main = over.app.Main("Over App Test", version.str, "LICENSE", use_cfg_file=False) main = over.app.Main("Over App Test", version.str, "LICENSE")
# name, description, callback, default=Option_sources.none, count=0, overwrite=True, abbr=None, in_cfg_file=True, show_in_help=True # name, description, callback, default=Option_sources.none, count=0, overwrite=True, abbr=None, in_cfg_file=True, show_in_help=True
main.add_option("armed", "Description.", bool, True, abbr="A") main.add_option("armed", "Description.", bool, True, abbr="A")
main.add_option("verbose", "Description.", bool, True, abbr="v") main.add_option("verbose", "Description.", bool, True, abbr="v")
@ -42,5 +42,4 @@ if __name__ == "__main__":
if option.name not in ["help", "-help"]: if option.name not in ["help", "-help"]:
main.print("option <g>%s<.> = %s" %(option.name, option.value)) main.print("option <g>%s<.> = %s" %(option.name, option.value))
main.print(str(main.cfg.verbosee))
main.print("<m>targets<.>: %s" %(main.targets)) main.print("<m>targets<.>: %s" %(main.targets))