diff --git a/scripting/bin/administration/diagnostics.py b/scripting/bin/administration/diagnostics.py index 8b71cf3cfe764d4e2f50e6117bbaa1f1228c49ca..6586bddc8e83860f09c34e742defa5623b84ac3b 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