Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 12 additions & 15 deletions cmdarg.sh
Original file line number Diff line number Diff line change
Expand Up @@ -53,18 +53,16 @@ function cmdarg
elif [[ "$argtype" != "" ]]; then
CMDARG_FLAGS[$shortopt]=${argtypemap["$argtype"]}
if [[ "${1:2:4}" == "[]" ]]; then
declare -p ${key} >/dev/null 2>&1
if [[ $? -ne 0 ]]; then
echo 'Array variable '"${key}"' does not exist. Array variables MUST be declared by the user!' >&2
${CMDARG_ERROR_BEHAVIOR} 1
fi
declare -p ${key} >/dev/null 2>&1 || {
echo 'Array variable '"${key}"' does not exist. Array variables MUST be declared by the user!' >&2
${CMDARG_ERROR_BEHAVIOR} 1
}
CMDARG_TYPES[$key]=$CMDARG_TYPE_ARRAY
elif [[ "${1:2:4}" == "{}" ]]; then
declare -p ${key} >/dev/null 2>&1
if [[ $? -ne 0 ]]; then
echo 'Hash variable '"${key}"' does not exist. Hash variables MUST be declared by the user!' >&2
${CMDARG_ERROR_BEHAVIOR} 1
fi
declare -p ${key} >/dev/null 2>&1 || {
echo 'Hash variable '"${key}"' does not exist. Hash variables MUST be declared by the user!' >&2
${CMDARG_ERROR_BEHAVIOR} 1
}
CMDARG_TYPES[$key]=$CMDARG_TYPE_HASH
else
CMDARG_TYPES[$key]=$CMDARG_TYPE_STRING
Expand Down Expand Up @@ -198,11 +196,10 @@ function cmdarg_validate

local shortopt=${CMDARG_REV[$longopt]}
if [ "${CMDARG_VALIDATORS[$shortopt]}" != "" ]; then
( ${CMDARG_VALIDATORS[${shortopt}]} "$value" "$hashkey")
if [ $? -ne 0 ]; then
echo "Invalid value for -$shortopt : ${value}" >&2
${CMDARG_ERROR_BEHAVIOR} 1
fi
${CMDARG_VALIDATORS[${shortopt}]} "$value" "$hashkey" || {
echo "Invalid value for -$shortopt : ${value}" >&2
${CMDARG_ERROR_BEHAVIOR} 1
}
fi
return 0
}
Expand Down
29 changes: 24 additions & 5 deletions tests/test_validators.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ function shunittest_validator_for_hash
echo "my_hash_validator $value" >&2
[[ "$value" == "value" ]]
}

declare -A something

cmdarg_purge
cmdarg 'x:{}' 'something' 'something' '' my_hash_validator || return 1
set -x
Expand All @@ -28,9 +28,9 @@ function shunittest_validator_for_array
echo "my_array_validator $value" >&2
[[ "$value" == "value" ]]
}

declare -a something

cmdarg_purge
cmdarg 'x:[]' 'something' 'something' '' my_array_validator || return 1
cmdarg_parse --something notavalue && return 1
Expand All @@ -46,9 +46,28 @@ function shunittest_validator_failure_recognized
echo "my_validator $value" >&2
[[ "$value" == "value" ]]
}

cmdarg_purge
cmdarg 'x:' 'something' 'something' '' my_validator
cmdarg_parse --something notavalue || return 0
return 1
}

function shunittest_validator_works_with_set_e
{
set -e

function my_validator
{
value=${1:-$OPTARG}
echo "my_validator $value" >&2
[[ "$value" == "value" ]]
}

cmdarg_purge
cmdarg 'x:' 'something' 'something' '' my_validator
cmdarg_parse --something notavalue || return 0
return 1

set +e
}