From 47620583a798a7fa1d60f4e3b63d4c16778ebaa5 Mon Sep 17 00:00:00 2001 From: Gerrit Date: Wed, 8 Jan 2020 18:17:49 -0500 Subject: [PATCH] Add an argument group for required parsed args (#299) This way the --help message will be more clear and won't list the required arguments under optional arguments, which is the default for argument flags (--arg or -a), whether they are required or not. Note: to show the required arguments before the optional arguments we rely on a hacky fixed that accesses and changes a private attributes of the parser object (it reverses the list order). This addresses some of the issues raised in #111. --- gridpath/common_functions.py | 7 ++++--- gridpath/run_scenario.py | 6 ++++++ 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/gridpath/common_functions.py b/gridpath/common_functions.py index 635674f13..94ba314a2 100644 --- a/gridpath/common_functions.py +++ b/gridpath/common_functions.py @@ -76,8 +76,9 @@ def get_scenario_name_parser(): """ parser = ArgumentParser(add_help=False) - parser.add_argument("--scenario", required=True, type=str, - help="Name of the scenario problem to solve.") + required = parser.add_argument_group('required arguments') + required.add_argument("--scenario", required=True, type=str, + help="Name of the scenario problem to solve.") return parser @@ -145,7 +146,7 @@ def get_solve_parser(): "for which you are providing an executable.") parser.add_argument("--mute_solver_output", default=False, action="store_true", - help="Don't print solver output if set to true.") + help="Don't print solver output.") parser.add_argument("--write_solver_files_to_logs_dir", default=False, action="store_true", help="Write the temporary " "solver files to the logs " diff --git a/gridpath/run_scenario.py b/gridpath/run_scenario.py index 853349569..b1018a65e 100644 --- a/gridpath/run_scenario.py +++ b/gridpath/run_scenario.py @@ -798,6 +798,12 @@ def parse_arguments(args): get_solve_parser()] ) + # Flip order of argument groups so "required arguments" show first + # https://stackoverflow.com/questions/39047075/reorder-python-argparse-argument-groups + # Note: hacky fix; preferred answer of creating an explicit optional group + # doesn't work because we combine parsers here with the parents keyword + parser._action_groups.reverse() + # Parse arguments # TODO: should we throw warning for unknown arguments (here and in the # other scripts)? run_start_to_end does pass unknown arguments (e.g.