From 51b6cb4f9b275ce51a084a4d3d0cbd21cf8486c8 Mon Sep 17 00:00:00 2001 From: Felix Kloss Date: Mon, 3 Jun 2024 16:33:07 +0200 Subject: [PATCH] Move argument parser creation to separate function --- cluster_utils/settings.py | 51 ++++++++++++++++++++++----------------- 1 file changed, 29 insertions(+), 22 deletions(-) diff --git a/cluster_utils/settings.py b/cluster_utils/settings.py index c0af30d..d76d8d1 100644 --- a/cluster_utils/settings.py +++ b/cluster_utils/settings.py @@ -423,27 +423,8 @@ def add_cmd_params(orig_dict): ) -def read_params_from_cmdline( - cmd_line: Optional[list[str]] = None, - make_immutable: bool = True, - verbose: bool = True, - dynamic: bool = True, - save_params: bool = True, -) -> smart_settings.AttributeDict: - """Read parameters based on command line input. - - Args: - cmd_line: Command line arguments (defaults to sys.argv). - make_immutable: See ``smart_settings.loads()`` - verbose: If true, print the loaded parameters. - dynamic: See ``smart_settings.loads()`` - save_params: If true, save the settings as JSON file in the working_dir. - - Returns: - Parameters as loaded by smart_settings. - """ - if not cmd_line: - cmd_line = sys.argv +def init_job_script_argument_parser() -> argparse.ArgumentParser: + """Initialise ArgumentParser for job scripts.""" def server_info(ip_and_port: str) -> dict[str, str | int]: """Split and validate string in "ip:port" format. For use with argparse.""" @@ -495,9 +476,35 @@ def server_info(ip_and_port: str) -> dict[str, str | int]: help="IP and port used to connect to the cluster_utils main process.", ) + return parser + + +def read_params_from_cmdline( + cmd_line: Optional[list[str]] = None, + make_immutable: bool = True, + verbose: bool = True, + dynamic: bool = True, + save_params: bool = True, +) -> smart_settings.AttributeDict: + """Read parameters based on command line input. + + Args: + cmd_line: Command line arguments (defaults to sys.argv). + make_immutable: See ``smart_settings.loads()`` + verbose: If true, print the loaded parameters. + dynamic: See ``smart_settings.loads()`` + save_params: If true, save the settings as JSON file in the working_dir. + + Returns: + Parameters as loaded by smart_settings. + """ + if not cmd_line: + cmd_line = sys.argv + + parser = init_job_script_argument_parser() args = parser.parse_args(cmd_line[1:]) - # some argument validation + # some argument validation which cannot be done by argparse directly if args.cluster_utils_server and args.job_id is None: parser.error("--job-id is required when --cluster-utils-server is set.")