From b43266fabcc6105679ab95e0d02442ab0a477966 Mon Sep 17 00:00:00 2001 From: jakub-nt <175944085+jakub-nt@users.noreply.github.com> Date: Thu, 25 Jul 2024 14:56:55 +0200 Subject: [PATCH] Refactor out the arg count validation Signed-off-by: jakub-nt <175944085+jakub-nt@users.noreply.github.com> --- cfbs/utils.py | 17 +++++++++++++++++ cfbs/validate.py | 13 +++++-------- 2 files changed, 22 insertions(+), 8 deletions(-) diff --git a/cfbs/utils.py b/cfbs/utils.py index 72e3b5f..dc2087f 100644 --- a/cfbs/utils.py +++ b/cfbs/utils.py @@ -92,6 +92,23 @@ def split_command(command) -> tuple[str, list[str]]: return operation, args +def is_valid_arg_count(args, expected): + actual = len(args) + + if type(expected) is int: + if actual != expected: + return False + + else: + # Only other option is a string of 1+, 2+ or similar: + assert type(expected) is str and expected.endswith("+") + expected = int(expected[0:-1]) + if actual < expected: + return False + + return True + + def user_error(msg: str): sys.exit("Error: " + msg) diff --git a/cfbs/validate.py b/cfbs/validate.py index 9730200..5bc7751 100644 --- a/cfbs/validate.py +++ b/cfbs/validate.py @@ -4,7 +4,7 @@ import re from collections import OrderedDict -from cfbs.utils import is_a_commit_hash, split_command, user_error +from cfbs.utils import is_valid_arg_count, is_a_commit_hash, split_command, user_error from cfbs.pretty import TOP_LEVEL_KEYS, MODULE_KEYS from cfbs.cfbs_config import CFBSConfig from cfbs.build import AVAILABLE_BUILD_STEPS @@ -276,18 +276,15 @@ def validate_steps(name, module): ) expected = AVAILABLE_BUILD_STEPS[operation] actual = len(args) - if type(expected) is int: - if expected != actual: + if not is_valid_arg_count(args, expected): + if type(expected) is int: raise CFBSValidationError( name, "The %s build step expects %d arguments, %d were given" % (operation, expected, actual), ) - else: - # Only other option is a string of 1+, 2+ or similar: - assert type(expected) is str and expected.endswith("+") - expected = int(expected[0:-1]) - if actual < expected: + else: + expected = int(expected[0:-1]) raise CFBSValidationError( name, "The %s build step expects %d or more arguments, %d were given"