Loading bashopts.sh +16 −6 Original line number Diff line number Diff line Loading @@ -238,6 +238,10 @@ bashopts_declare() { *) bashopts_critical "Fatal error";; esac done # Check imcompatible -v and -r options if [ -n "${options[default]}" ] && [ "${options[req_value]}" == "true" ]; then bashopts_critical "bashopts_declare: -r and -v options cannot be activated at the same time" fi # format the type and check/format the default value case "${options[type]}" in ''|bool|boolean) Loading @@ -245,11 +249,9 @@ bashopts_declare() { ;; str|string) options[type]="string" options[req_value]="true" ;; num|number) options[type]="number" options[req_value]="true" ;; *) bashopts_critical "Invalid type ${options[type]}" Loading @@ -264,8 +266,11 @@ bashopts_declare() { ''|s|set) # default: simple value - override options[method]="set" if [ "${options[type]}" != "string" ] || [[ -v options[default] ]]; then # Check the default value format options[default]="$(${options[check]} "${options[default]}")" || exit 1 options[default]="$(${options[check]} "${options[default]}")" \ || bashopts_critical "Invalid default value for ${options[name]} option" fi ;; a|add) # array value - add Loading Loading @@ -441,10 +446,10 @@ bashopts_parse_args() { long_opts=() for op in "${bashopts_optlist[@]}"; do if [[ -v bashopts_optprop_short_opt[$op] ]]; then short_opts="${short_opts}${bashopts_optprop_short_opt[$op]}:$(test "${bashopts_optprop_req_value[$op]}" == "true" || echo ":")" short_opts="${short_opts}${bashopts_optprop_short_opt[$op]}:$(test "${bashopts_optprop_type[$op]}" != "boolean" || echo ":")" fi if [[ -v bashopts_optprop_long_opt[$op] ]]; then long_opts+=("${bashopts_optprop_long_opt[$op]}:$(test "${bashopts_optprop_req_value[$op]}" == "true" || echo ":")") long_opts+=("${bashopts_optprop_long_opt[$op]}:$(test "${bashopts_optprop_type[$op]}" != "boolean" || echo ":")") fi done long_opts=$(bashopts_join_by , ${long_opts[@]}) Loading Loading @@ -549,6 +554,9 @@ bashopts_process_option() { if [ -z "$check" ]; then check="${bashopts_optprop_check[$op]}" fi if [ "${bashopts_optprop_req_value[$op]}" == "true" ]; then val_req="true" fi # eval or get default value if [[ -v bashopts_optprop_expression[$op] ]]; then Loading Loading @@ -618,6 +626,8 @@ bashopts_process_option() { # check format if [ "${#tval[@]}" -eq 0 ] && [ "$val_req" == "true" ]; then bashopts_error "At least one value required" unset tval continue fi if [ "${bashopts_optprop_method[$op]}" == "add" ]; then # array value Loading test.sh +31 −0 Original line number Diff line number Diff line Loading @@ -360,6 +360,37 @@ _test_case_24() { || true) | grep -E '^* A str value$' | wc -l )" "2" } _test_case_25() { bashopts_setup -n "$0" -d "Test case $0" bashopts_declare -n value -l value -d "A str value" -t string -r req_test_eq "$( (bashopts_process_opts 2>&1 <<< "" \ || true) | grep -E '^* A str value$' | wc -l )" "2" } _test_case_26() { bashopts_setup -n "$0" -d "Test case $0" bashopts_declare -n value -l value -d "A str value" -t string -r -k is_eq_to_null req_test_eq "$( (bashopts_process_opts 2>&1 <<< " NUL NULL ") | sed 's/At least one value required/###CASE1###/g' \ | sed 's/Not equal to NULL/###CASE2###/g' \ | grep -o -E '###CASE(1|2)###' | tr '\n' ' ' )" \ "###CASE1### ###CASE1### ###CASE1### ###CASE2### " } _test_case_27() { bashopts_setup -n "$0" -d "Test case $0" req_test_eq "$(bashopts_declare -n value -l value -d "A str value" -t string -r -v "test" 2>&1)" \ "[ERROR] bashopts_declare: -r and -v options cannot be activated at the same time" } _test() { bashopts_setup -n "$0" -d "Test case $0" Loading Loading
bashopts.sh +16 −6 Original line number Diff line number Diff line Loading @@ -238,6 +238,10 @@ bashopts_declare() { *) bashopts_critical "Fatal error";; esac done # Check imcompatible -v and -r options if [ -n "${options[default]}" ] && [ "${options[req_value]}" == "true" ]; then bashopts_critical "bashopts_declare: -r and -v options cannot be activated at the same time" fi # format the type and check/format the default value case "${options[type]}" in ''|bool|boolean) Loading @@ -245,11 +249,9 @@ bashopts_declare() { ;; str|string) options[type]="string" options[req_value]="true" ;; num|number) options[type]="number" options[req_value]="true" ;; *) bashopts_critical "Invalid type ${options[type]}" Loading @@ -264,8 +266,11 @@ bashopts_declare() { ''|s|set) # default: simple value - override options[method]="set" if [ "${options[type]}" != "string" ] || [[ -v options[default] ]]; then # Check the default value format options[default]="$(${options[check]} "${options[default]}")" || exit 1 options[default]="$(${options[check]} "${options[default]}")" \ || bashopts_critical "Invalid default value for ${options[name]} option" fi ;; a|add) # array value - add Loading Loading @@ -441,10 +446,10 @@ bashopts_parse_args() { long_opts=() for op in "${bashopts_optlist[@]}"; do if [[ -v bashopts_optprop_short_opt[$op] ]]; then short_opts="${short_opts}${bashopts_optprop_short_opt[$op]}:$(test "${bashopts_optprop_req_value[$op]}" == "true" || echo ":")" short_opts="${short_opts}${bashopts_optprop_short_opt[$op]}:$(test "${bashopts_optprop_type[$op]}" != "boolean" || echo ":")" fi if [[ -v bashopts_optprop_long_opt[$op] ]]; then long_opts+=("${bashopts_optprop_long_opt[$op]}:$(test "${bashopts_optprop_req_value[$op]}" == "true" || echo ":")") long_opts+=("${bashopts_optprop_long_opt[$op]}:$(test "${bashopts_optprop_type[$op]}" != "boolean" || echo ":")") fi done long_opts=$(bashopts_join_by , ${long_opts[@]}) Loading Loading @@ -549,6 +554,9 @@ bashopts_process_option() { if [ -z "$check" ]; then check="${bashopts_optprop_check[$op]}" fi if [ "${bashopts_optprop_req_value[$op]}" == "true" ]; then val_req="true" fi # eval or get default value if [[ -v bashopts_optprop_expression[$op] ]]; then Loading Loading @@ -618,6 +626,8 @@ bashopts_process_option() { # check format if [ "${#tval[@]}" -eq 0 ] && [ "$val_req" == "true" ]; then bashopts_error "At least one value required" unset tval continue fi if [ "${bashopts_optprop_method[$op]}" == "add" ]; then # array value Loading
test.sh +31 −0 Original line number Diff line number Diff line Loading @@ -360,6 +360,37 @@ _test_case_24() { || true) | grep -E '^* A str value$' | wc -l )" "2" } _test_case_25() { bashopts_setup -n "$0" -d "Test case $0" bashopts_declare -n value -l value -d "A str value" -t string -r req_test_eq "$( (bashopts_process_opts 2>&1 <<< "" \ || true) | grep -E '^* A str value$' | wc -l )" "2" } _test_case_26() { bashopts_setup -n "$0" -d "Test case $0" bashopts_declare -n value -l value -d "A str value" -t string -r -k is_eq_to_null req_test_eq "$( (bashopts_process_opts 2>&1 <<< " NUL NULL ") | sed 's/At least one value required/###CASE1###/g' \ | sed 's/Not equal to NULL/###CASE2###/g' \ | grep -o -E '###CASE(1|2)###' | tr '\n' ' ' )" \ "###CASE1### ###CASE1### ###CASE1### ###CASE2### " } _test_case_27() { bashopts_setup -n "$0" -d "Test case $0" req_test_eq "$(bashopts_declare -n value -l value -d "A str value" -t string -r -v "test" 2>&1)" \ "[ERROR] bashopts_declare: -r and -v options cannot be activated at the same time" } _test() { bashopts_setup -n "$0" -d "Test case $0" Loading