Loading bashopts.sh +47 −9 Original line number Diff line number Diff line Loading @@ -65,6 +65,10 @@ bashopts_regex_escape () { echo $1 | sed 's/[][()\.^$\/?*+]/\\&/g' } bashopts_isboolean() { [[ $1 =~ ^(true|false)$ ]] || return 1 } bashopts_isnumber() { [[ $1 =~ ^-?[0-9]+([.][0-9]+)?$ ]] || return 1 } Loading Loading @@ -226,9 +230,6 @@ bashopts_declare() { ;; a|add) options[method]="add" if [ "${options[interactive]}" == "true" ]; then bashopts_error "'-i' and '-m add' options are incompatible" fi ;; *) bashopts_error "Invalid method ${options[method]}" Loading Loading @@ -274,6 +275,7 @@ bashopts_join_by() { bashopts_dump_value() { local op=$1 shift || bashopts_error "Usage: bashopts_dump_value op_name" [[ -v "$op" ]] || return 0 if [ "${bashopts_optprop_method[$op]}" == "set" ]; then if [ "${bashopts_optprop_type[$op]}" == "string" ]; then echo -n "\"${!op//\"/\\\"}\"" Loading Loading @@ -443,6 +445,8 @@ bashopts_process_args() { # eval or get default value if [[ -v bashopts_optprop_expression[$op] ]]; then eval "dval=${bashopts_optprop_expression[$op]}" elif [ "${bashopts_optprop_method[$op]}" == "add" ]; then dval=() else dval=${bashopts_optprop_default[$op]} fi Loading @@ -459,12 +463,46 @@ bashopts_process_args() { eval "tval=$(bashopts_get_def dval)" fi if [ "${bashopts_optprop_interactive[$op]}" == "true" ] && [ "$bashopts_non_interactive" != "true" ]; then while true; do echo "* ${bashopts_optprop_description[$op]}" echo -n " [$tval]: " echo -n " [$(bashopts_get_def tval)]: " read ival if [ -n "$ival" ]; then if [ -z "$ival" ]; then break; fi if [ "${bashopts_optprop_method[$op]}" == "add" ]; then if ! eval "tval=$ival" 2>/dev/null; then echo "'$ival' is not a valid BASH array (format: '( \"val 1\" \"val2\" \"val3...\" )')" unset tval continue fi else tval=$ival fi case "${bashopts_optprop_type[$op]}" in boolean) for v in "${tval[@]}"; do if ! bashopts_isboolean $v; then echo "'$v' is not a valid boolean value" unset tval break fi done ;; number) for v in "${tval[@]}"; do if ! bashopts_isnumber $v; then echo "'$v' is not a valid number value" unset tval break fi done ;; esac if [[ -v tval ]]; then break fi done fi if [[ -v tval ]]; then eval "$op=$(bashopts_get_def tval)" Loading test.sh +39 −0 Original line number Diff line number Diff line Loading @@ -171,6 +171,45 @@ _test_case_11() { req_test_eq "$(bashopts_process_args 2>&1 | grep 'WARNING')" "[WARNING] Please check the settings file" } _test_case_12() { bashopts_setup -n "$0" -d "Test case $0" bashopts_declare -n array_value -l value -m add -d "Array value" -t string -i bashopts_parse_args bashopts_process_args > /dev/null <<< "( \"value 1\" \"value 2\" )" req_test_eq "${#array_value[@]}" "2" req_test_eq "${array_value[0]}" "value 1" req_test_eq "${array_value[1]}" "value 2" } _test_case_13() { bashopts_setup -n "$0" -d "Test case $0" bashopts_declare -n array_value -l value -m add -d "Array value" -t number -i bashopts_parse_args bashopts_process_args > /dev/null <<< "( 3 -8 )" req_test_eq "${#array_value[@]}" "2" req_test_eq "${array_value[0]}" "3" req_test_eq "${array_value[1]}" "-8" } _test_case_14() { bashopts_setup -n "$0" -d "Test case $0" bashopts_declare -n array_value -l value -m add -d "Array value" -t boolean -i bashopts_parse_args bashopts_process_args > /dev/null <<< "( true false )" req_test_eq "${#array_value[@]}" "2" req_test_eq "${array_value[0]}" "true" req_test_eq "${array_value[1]}" "false" } if [ ${#} -eq 0 ]; then for t in $(grep -E -o '_test_case_\w+\>' $0); do echo "=> Test case $t" Loading Loading
bashopts.sh +47 −9 Original line number Diff line number Diff line Loading @@ -65,6 +65,10 @@ bashopts_regex_escape () { echo $1 | sed 's/[][()\.^$\/?*+]/\\&/g' } bashopts_isboolean() { [[ $1 =~ ^(true|false)$ ]] || return 1 } bashopts_isnumber() { [[ $1 =~ ^-?[0-9]+([.][0-9]+)?$ ]] || return 1 } Loading Loading @@ -226,9 +230,6 @@ bashopts_declare() { ;; a|add) options[method]="add" if [ "${options[interactive]}" == "true" ]; then bashopts_error "'-i' and '-m add' options are incompatible" fi ;; *) bashopts_error "Invalid method ${options[method]}" Loading Loading @@ -274,6 +275,7 @@ bashopts_join_by() { bashopts_dump_value() { local op=$1 shift || bashopts_error "Usage: bashopts_dump_value op_name" [[ -v "$op" ]] || return 0 if [ "${bashopts_optprop_method[$op]}" == "set" ]; then if [ "${bashopts_optprop_type[$op]}" == "string" ]; then echo -n "\"${!op//\"/\\\"}\"" Loading Loading @@ -443,6 +445,8 @@ bashopts_process_args() { # eval or get default value if [[ -v bashopts_optprop_expression[$op] ]]; then eval "dval=${bashopts_optprop_expression[$op]}" elif [ "${bashopts_optprop_method[$op]}" == "add" ]; then dval=() else dval=${bashopts_optprop_default[$op]} fi Loading @@ -459,12 +463,46 @@ bashopts_process_args() { eval "tval=$(bashopts_get_def dval)" fi if [ "${bashopts_optprop_interactive[$op]}" == "true" ] && [ "$bashopts_non_interactive" != "true" ]; then while true; do echo "* ${bashopts_optprop_description[$op]}" echo -n " [$tval]: " echo -n " [$(bashopts_get_def tval)]: " read ival if [ -n "$ival" ]; then if [ -z "$ival" ]; then break; fi if [ "${bashopts_optprop_method[$op]}" == "add" ]; then if ! eval "tval=$ival" 2>/dev/null; then echo "'$ival' is not a valid BASH array (format: '( \"val 1\" \"val2\" \"val3...\" )')" unset tval continue fi else tval=$ival fi case "${bashopts_optprop_type[$op]}" in boolean) for v in "${tval[@]}"; do if ! bashopts_isboolean $v; then echo "'$v' is not a valid boolean value" unset tval break fi done ;; number) for v in "${tval[@]}"; do if ! bashopts_isnumber $v; then echo "'$v' is not a valid number value" unset tval break fi done ;; esac if [[ -v tval ]]; then break fi done fi if [[ -v tval ]]; then eval "$op=$(bashopts_get_def tval)" Loading
test.sh +39 −0 Original line number Diff line number Diff line Loading @@ -171,6 +171,45 @@ _test_case_11() { req_test_eq "$(bashopts_process_args 2>&1 | grep 'WARNING')" "[WARNING] Please check the settings file" } _test_case_12() { bashopts_setup -n "$0" -d "Test case $0" bashopts_declare -n array_value -l value -m add -d "Array value" -t string -i bashopts_parse_args bashopts_process_args > /dev/null <<< "( \"value 1\" \"value 2\" )" req_test_eq "${#array_value[@]}" "2" req_test_eq "${array_value[0]}" "value 1" req_test_eq "${array_value[1]}" "value 2" } _test_case_13() { bashopts_setup -n "$0" -d "Test case $0" bashopts_declare -n array_value -l value -m add -d "Array value" -t number -i bashopts_parse_args bashopts_process_args > /dev/null <<< "( 3 -8 )" req_test_eq "${#array_value[@]}" "2" req_test_eq "${array_value[0]}" "3" req_test_eq "${array_value[1]}" "-8" } _test_case_14() { bashopts_setup -n "$0" -d "Test case $0" bashopts_declare -n array_value -l value -m add -d "Array value" -t boolean -i bashopts_parse_args bashopts_process_args > /dev/null <<< "( true false )" req_test_eq "${#array_value[@]}" "2" req_test_eq "${array_value[0]}" "true" req_test_eq "${array_value[1]}" "false" } if [ ${#} -eq 0 ]; then for t in $(grep -E -o '_test_case_\w+\>' $0); do echo "=> Test case $t" Loading