Better __CheckArguments function

This commit is contained in:
deajan 2016-08-26 14:46:11 +02:00
parent 134aaec0da
commit 8817c68f7c
1 changed files with 19 additions and 18 deletions

View File

@ -1,6 +1,6 @@
#### MINIMAL-FUNCTION-SET BEGIN #### #### MINIMAL-FUNCTION-SET BEGIN ####
## FUNC_BUILD=2016082603 ## FUNC_BUILD=2016082604
## BEGIN Generic functions for osync & obackup written in 2013-2016 by Orsiris de Jong - http://www.netpower.fr - ozy@netpower.fr ## BEGIN Generic functions for osync & obackup written in 2013-2016 by Orsiris de Jong - http://www.netpower.fr - ozy@netpower.fr
## type -p does not work on platforms other than linux (bash). If if does not work, always assume output is not a zero exitcode ## type -p does not work on platforms other than linux (bash). If if does not work, always assume output is not a zero exitcode
@ -1024,15 +1024,15 @@ function __CheckArguments {
# Checks the number of arguments of a function and raises an error if some are missing # Checks the number of arguments of a function and raises an error if some are missing
if [ "$_DEBUG" == "yes" ]; then if [ "$_DEBUG" == "yes" ]; then
local number_of_arguments="${1}" # Number of arguments the tested function should have, can be a number of a range, eg 0-2 for zero to two arguments local numberOfArguments="${1}" # Number of arguments the tested function should have, can be a number of a range, eg 0-2 for zero to two arguments
local number_of_given_arguments="${2}" # Number of arguments that have been passed local numberOfGivenArguments="${2}" # Number of arguments that have been passed
local function_name="${3}" # Function name that called __CheckArguments local functionName="${3}" # Function name that called __CheckArguments
local minArgs local minArgs
local maxArgs local maxArgs
if [ "$_PARANOIA_DEBUG" == "yes" ]; then if [ "$_PARANOIA_DEBUG" == "yes" ]; then
Logger "Entering function [$function_name]." "DEBUG" Logger "Entering function [$functionName]." "DEBUG"
fi fi
# All arguments of the function to check are passed as array in ${4} (the function call waits for $@) # All arguments of the function to check are passed as array in ${4} (the function call waits for $@)
@ -1040,30 +1040,31 @@ function __CheckArguments {
# In order to avoid this, we need to iterate over ${4} and count # In order to avoid this, we need to iterate over ${4} and count
local iterate=4 local iterate=4
local fetch_arguments=1 local fetchArguments=1
local arg_list="" local argList=""
while [ $fetch_arguments -eq 1 ]; do local countedArguments
while [ $fetchArguments -eq 1 ]; do
cmd='argument=${'$iterate'}' cmd='argument=${'$iterate'}'
eval $cmd eval $cmd
if [ "$argument" = "" ]; then if [ "$argument" = "" ]; then
fetch_arguments=0 fetchArguments=0
else else
arg_list="$arg_list [Argument $(($iterate-3)): $argument]" argList="$arg_list [Argument $(($iterate-3)): $argument]"
iterate=$(($iterate+1)) iterate=$(($iterate+1))
fi fi
done done
local counted_arguments=$((iterate-4)) countedArguments=$((iterate-4))
if [ $(isNumeric $number_of_arguments) == "1" ]; then if [ $(IsNumeric "$numberOfArguments") -eq 1 ]; then
$minArgs = $number_of_arguments minArgs=$numberOfArguments
$maxArgs = $number_of_arguments maxArgs=$numberOfArguments
else else
IFS='-' read minArgs maxArgs <<< "$pids" IFS='-' read minArgs maxArgs <<< "$numberOfArguments"
fi fi
if ! ([ $counted_arguments -ge $minArgs ] && [ $counted_arguments -le $maxArgs ]); then if ! ([ $countedArguments -ge $minArgs ] && [ $countedArguments -le $maxArgs ]); then
Logger "Function $function_name may have inconsistent number of arguments. Expected: $number_of_arguments, count: $counted_arguments, bash seen: $number_of_given_arguments. see log file." "ERROR" Logger "Function $functionName may have inconsistent number of arguments. Expected min: $minArgs, max: $maxArgs, count: $countedArguments, bash seen: $numberOfGivenArguments. see log file." "ERROR"
Logger "Arguments passed: $arg_list" "ERROR" Logger "Arguments passed: $argList" "ERROR"
fi fi
fi fi
} }