From a9271b9caaee0d9bd2afa3a635394932473e7b67 Mon Sep 17 00:00:00 2001 From: Timm Fitschen <t.fitschen@indiscale.com> Date: Tue, 25 Aug 2020 16:44:16 +0200 Subject: [PATCH] Add doctests to diagnostics --- scripting/bin/administration/diagnostics.py | 41 ++++++++++++++++++++- 1 file changed, 39 insertions(+), 2 deletions(-) diff --git a/scripting/bin/administration/diagnostics.py b/scripting/bin/administration/diagnostics.py index 8b71cf3c..6586bddc 100755 --- a/scripting/bin/administration/diagnostics.py +++ b/scripting/bin/administration/diagnostics.py @@ -54,9 +54,44 @@ def get_files(): return result -def get_option(name, default=None): +def get_option(name, default=None, args=None): + """get_option + + Return the value of a command line options by name or a default value if + the command line parameters do not contain such an option. + + Examples + -------- + >>> get_option("opt1", args=["--opt1=bla", "--opt2=blub"]) + 'bla' + >>> get_option("opt1", args=["--opt1", "bla", "--opt2", "blub"]) + 'bla' + >>> get_option("opt1", default="def", args=["--other=blub"]) + 'def' + >>> get_option("opt1", args=["--opt1", "--opt2=blub"]) + True + >>> get_option("opt1", args=["--opt1"]) + True + >>> get_option("opt1", args=["--other=blub"]) is None + True + + Parameters + ---------- + name : str + The name + default : str (optional) + Return this value if the arguments do not contain the option. Defaults + to `None`. + args : list of str (optional) + List of command line parameters which are to be parsed. Defaults to + `sys.argv`. + + Returns + ------- + """ _next = False - for arg in sys.argv: + _args = args if args is not None else sys.argv + for arg in _args: if _next is True: if arg.startswith("--"): return True @@ -66,6 +101,8 @@ def get_option(name, default=None): return arg[index:] elif arg == "--{}".format(name): _next = True + if _next is True: + return True return default -- GitLab