diff --git a/cmdarg.sh b/cmdarg.sh index 282b05c..f61f5a9 100644 --- a/cmdarg.sh +++ b/cmdarg.sh @@ -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 @@ -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 } diff --git a/tests/test_validators.sh b/tests/test_validators.sh index 7f59328..86752dc 100644 --- a/tests/test_validators.sh +++ b/tests/test_validators.sh @@ -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 @@ -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 @@ -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 +}