Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improving script and function argument handling #1095

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 24 additions & 12 deletions gridpath/get_scenario_inputs.py
Original file line number Diff line number Diff line change
Expand Up @@ -301,14 +301,10 @@ def delete_prior_inputs(inputs_directory):
for f in prior_input_tab_files:
os.remove(os.path.join(inputs_directory, f))


def parse_arguments(args):
def create_parser():
"""
:param args: the script arguments specified by the user
:return: the parsed known argument values (<class 'argparse.Namespace'>
Python object)

Parse the known arguments.

:return: argument parser object
"""
parser = ArgumentParser(
add_help=True,
Expand All @@ -318,9 +314,27 @@ def parse_arguments(args):
get_get_inputs_parser(),
],
)

return parser

parsed_arguments = parser.parse_known_args(args=args)[0]
def parse_arguments(func_args):
"""
:param args: the function arguments specified by the caller
:return: the parsed known argument values (<class 'argparse.Namespace'>
Python object)

Parse the known arguments.
"""
parser = create_parser()

is_cli_entry_point = func_args is None
if is_cli_entry_point:
args = sys.argv[1:]
parsed_arguments = parser.parse_args(args=args)
else:
args = func_args
parsed_arguments = parser.parse_known_args(args=args)[0]

return parsed_arguments


Expand Down Expand Up @@ -439,16 +453,14 @@ def write_linked_subproblems_map(scenario_directory, conn, subscenarios):
)


def main(args=None):
def main(func_args=None):
"""

:return:
"""
# Retrieve DB location and scenario_id and/or name from args
if args is None:
args = sys.argv[1:]

parsed_arguments = parse_arguments(args=args)
parsed_arguments = parse_arguments(func_args=func_args)

db_path = parsed_arguments.database
scenario_id_arg = parsed_arguments.scenario_id
Expand Down
Loading