From a3acd0d7913768ac3a39d53741b85ff546d5d5e4 Mon Sep 17 00:00:00 2001 From: dbeltrankyl Date: Wed, 29 Jan 2025 15:27:04 +0100 Subject: [PATCH] Bruno Feedback --- autosubmit/autosubmit.py | 28 ++++++++++++++++------------ bin/autosubmit | 11 +++++------ 2 files changed, 21 insertions(+), 18 deletions(-) diff --git a/autosubmit/autosubmit.py b/autosubmit/autosubmit.py index c0d8f1a9..7fefd0d3 100644 --- a/autosubmit/autosubmit.py +++ b/autosubmit/autosubmit.py @@ -26,7 +26,7 @@ from distutils.util import strtobool from pathlib import Path from ruamel.yaml import YAML -from typing import Dict, Set, Tuple, Union, Any +from typing import Dict, Set, Tuple, Union, Any, List, Optional from autosubmit.database.db_common import update_experiment_descrip_version from autosubmit.helpers.parameters import PARAMETERS @@ -81,7 +81,6 @@ from pyparsing import nestedExpr from .history.experiment_status import ExperimentStatus from .history.experiment_history import ExperimentHistory -from typing import List import autosubmit.history.utils as HUtils import autosubmit.helpers.autosubmit_helper as AutosubmitHelper import autosubmit.statistics.utils as StatisticsUtils @@ -177,9 +176,14 @@ def environ_init(): os.environ['PYTHONUNBUFFERED'] = 'true' @staticmethod - def parse_args(): + def parse_args() -> Tuple[int, Optional[argparse.Namespace]]: """ - Parse arguments given to an executable and start execution of command given + Parse arguments given to an executable and start execution of command given. + + Returns a tuple with the exit code (``status``), and an optional list of + arguments for ``argparse``. The list of arguments is only ever returned + when the arguments are valid for the execution of a subcommand. Otherwise, + they will be ``None``. """ Autosubmit.environ_init() try: @@ -692,16 +696,16 @@ def parse_args(): args, unknown = parser.parse_known_args() if args.version: Log.info(Autosubmit.autosubmit_version) - return 0 - if unknown or args.command is None: + return 0, None + elif unknown or args.command is None: parser.print_help() - return 1 - except SystemExit as e: - return 1 + return 1, None + + return 0, args + except SystemExit: + return 1, None except BaseException as e: - raise AutosubmitCritical( - "Incorrect arguments for this command", 7011) - return args + raise AutosubmitCritical(f"Incorrect arguments for this command: {str(e)}", 7011) @staticmethod def run_command(args): diff --git a/bin/autosubmit b/bin/autosubmit index 9c582c13..892199be 100755 --- a/bin/autosubmit +++ b/bin/autosubmit @@ -18,8 +18,10 @@ # along with Autosubmit. If not, see . """Script for handling experiment monitoring""" +import argparse import os import sys +from typing import Optional script_dir = os.path.abspath(os.path.dirname(sys.argv[0])) sys.path.append(script_dir) @@ -33,14 +35,11 @@ from autosubmitconfigparser.config.configcommon import AutosubmitConfig # noqa: # noinspection PyProtectedMember def main(): - args = None + args = Optional[argparse.Namespace] try: - args = Autosubmit.parse_args() - if type(args) is not int: + return_value, args = Autosubmit.parse_args() + if args: return_value = Autosubmit.run_command(args) - else: - return_value = args - return_value = return_value if type(return_value) is int else 0 delete_lock_file() except BaseException as e: delete_lock_file()