From d8c8af4fc63a7beeea032ffc7bf2708e56493088 Mon Sep 17 00:00:00 2001 From: Mikhail Karnevskiy <karnem@max-exfl001.desy.de> Date: Wed, 12 Sep 2018 13:58:15 +0200 Subject: [PATCH] Add parser group: required arguments. Show warning if parameter is an empty list. --- xfel_calibrate/calibrate.py | 41 ++++++++++++++++++++++++------------- 1 file changed, 27 insertions(+), 14 deletions(-) diff --git a/xfel_calibrate/calibrate.py b/xfel_calibrate/calibrate.py index a60a59bc1..606669a00 100755 --- a/xfel_calibrate/calibrate.py +++ b/xfel_calibrate/calibrate.py @@ -48,6 +48,8 @@ def make_initial_parser(): parser.add_argument('--report-to', type=str, help='Filename (and optionally path) for output report') + parser.add_argument_group('required arguments') + return parser parser = make_initial_parser() @@ -243,32 +245,43 @@ elif len(sys.argv) >= 3: helpstr = ("Default: %(default)s" if not p.comment else "{}. Default: %(default)s".format(p.comment.replace("#", " ").strip())) - required = (p.comment is not None and "required" in p.comment) and not overwrite_reqs - - if p.type == list or p.name == cvar: - if p.type is list and len(p.value): - ltype = type(p.value[0]) - elif p.type is not list: - ltype = p.type + required = (p.comment is not None + and "required" in p.comment + and not overwrite_reqs + and p.name != cvar) + + pars_group = parser._action_groups[1] + if required: + pars_group = parser._action_groups[2] + + default = p.value if (not required) else None + + if p.type == list: + # Warning if list is empty + if len(p.value)==0: + warnings.warn("List {} is empty. Parameter type can not be defined.".format(p.name), + RuntimeWarning) + + ltype = type(p.value[0]) range_allowed = "RANGE ALLOWED" in p.comment.upper() if p.comment else False - parser.add_argument("--{}".format(consolize_name(p.name)), + pars_group.add_argument("--{}".format(consolize_name(p.name)), nargs='+', type=ltype if not range_allowed else str, - default=p.value if (not required) and p.name != cvar else None, + default=default, help=helpstr, - required=required and p.name != cvar, + required=required, action=make_intelli_list(ltype) if range_allowed else None) elif p.type == bool: - parser.add_argument("--{}".format(consolize_name(p.name)), + pars_group.add_argument("--{}".format(consolize_name(p.name)), action="store_true", - default=p.value if not required else None, + default=default, help=helpstr, required=required) else: - parser.add_argument("--{}".format(consolize_name(p.name)), + pars_group.add_argument("--{}".format(consolize_name(p.name)), type=p.type, - default=p.value if not required else None, + default=default, help=helpstr, required=required) -- GitLab