Skip to content

Commit

Permalink
Refactor out the arg count validation
Browse files Browse the repository at this point in the history
Signed-off-by: jakub-nt <[email protected]>
  • Loading branch information
jakub-nt committed Jul 25, 2024
1 parent ca29b92 commit b43266f
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 8 deletions.
17 changes: 17 additions & 0 deletions cfbs/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
13 changes: 5 additions & 8 deletions cfbs/validate.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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"
Expand Down

0 comments on commit b43266f

Please sign in to comment.