X-Git-Url: http://git.tuebingen.mpg.de/?a=blobdiff_plain;ds=inline;f=funcs%2Fgsu;h=b409bc50bc75d50969d60a800c0710358c34c2d9;hb=5102a3ed6cf2d4b1addecf52495faafc04107cc5;hp=d72f54f4faa9dfcf30e4baf8f1608e5972cc4cc5;hpb=1d3a993452ac10a1c222c72693746a957179e2b6;p=gsu.git diff --git a/funcs/gsu b/funcs/gsu index d72f54f..b409bc5 100644 --- a/funcs/gsu +++ b/funcs/gsu @@ -14,6 +14,7 @@ E_GSU_BAD_CONFIG_VAR invalid config variable E_GSU_NEED_VALUE value required but not given E_GSU_BAD_BOOL bad value for boolian option E_GSU_BAD_OPTION_TYPE invalid option type +E_GSU_BAD_ARG_COUNT invalid number of arguments E_NO_DEFAULT missing default value $gsu_errors " @@ -44,6 +45,37 @@ gsu_is_a_number() } export -f gsu_is_a_number +# Check number of arguments. +# +# Usage: gsu_check_arg_count [] +# +# Check that is between and inclusively. +# If only ist given, num2 is assumed to be infinity. +# +# Examples: +# 0 0 no argument allowed +# 1 1 exactly one argument required +# 0 2 at most two arguments admissible +# 2 at least two arguments reqired +# +gsu_check_arg_count() +{ + ret=-$E_GSU_BAD_ARG_COUNT + if [[ $# -eq 2 ]]; then # only num1 is given + result="at least $2 args required, $1 given" + [[ $1 -lt $2 ]] && return + ret=$GSU_SUCCESS + return + fi + # num1 and num2 given + result="need at least $2 args, $1 given" + [[ $1 -lt $2 ]] && return + result="need at most $3 args, $1 given" + [[ $1 -gt $3 ]] && return + ret=$GSU_SUCCESS +} +export -f gsu_check_arg_count + gsu_short_msg() { echo "$1" 1>&2 @@ -62,6 +94,8 @@ gsu_date_msg() } export -f gsu_date_msg + + _gsu_banner_msg() { local txt="*** $_gsu_self --" @@ -306,7 +340,7 @@ _gsu_check_options() # only. Moreover it must not start with [a-zA-Z]. ret=-$E_GSU_BAD_CONFIG_VAR - result="$name" + result="name: '$name'" # bash's =~ works only for 3.2 and newer, so use grep echo "$name" | grep '^[a-zA-Z][a-zA-Z_0123456789]*$' &> /dev/null; [[ $? -ne 0 ]] && return @@ -332,7 +366,6 @@ _gsu_check_options() esac eval ${gsu_config_var_prefix}_$name='"'${val:=$default_value}'"' - # Check option type. ATM, only num and string are supported # Other types may be added without breaking compatibility case "$option_type" in @@ -362,7 +395,7 @@ gsu() _gsu_init_errors _gsu_check_options if [[ "$ret" -lt 0 ]]; then - if [[ "$1" != "help" && "$1" != "man" && "$1" != "prefs" ]]; then + if [[ "$1" != "help" && "$1" != "man" ]]; then gsu_err_msg exit 1 fi