Skip to content

Commit

Permalink
Add an argument group for required parsed args (#299)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
gerritdm authored Jan 8, 2020
1 parent 19da2ae commit 4762058
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
7 changes: 4 additions & 3 deletions gridpath/common_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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 "
Expand Down
6 changes: 6 additions & 0 deletions gridpath/run_scenario.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit 4762058

Please sign in to comment.