Loading bashopts.sh +22 −8 Original line number Diff line number Diff line Loading @@ -53,6 +53,14 @@ bashopts_error() { exit 1 } bashopts_warning() { local l >&2 echo -n "[WARNING] " for l in "$@"; do >&2 echo -e "$l" done } bashopts_regex_escape () { echo $1 | sed 's/[][()\.^$\/?*+]/\\&/g' } Loading Loading @@ -115,7 +123,6 @@ bashopts_optlist=() bashopts_commands=() bashopts_extra_args=() bashopts_tool_name=$0 bashopts_tool_settings_path="/dev/null" # STEP 1: setup bashopts_setup() { Loading Loading @@ -423,12 +430,12 @@ bashopts_parse_args() { ;; esac done bashopts_commands=("$@") } # STEP 4: process arg bashopts_process_args() { local dval ival touch $bashopts_tool_settings_path if [ "$__bashopts_display_help__" == "true" ]; then bashopts_non_interactive="true" fi Loading @@ -442,7 +449,9 @@ bashopts_process_args() { if ! [[ -v $op ]] || [ "$bashopts_tool_force_edit" == "true" ]; then unset tval # read the value from file if ! [[ -v $op ]] && [ "${bashopts_optprop_setting[$op]}" == "true" ] && grep -E -q "^$op=" $bashopts_tool_settings_path; then if ! [[ -v $op ]] && [ "${bashopts_optprop_setting[$op]}" == "true" ] \ && [ -f "$(readlink -f $bashopts_tool_settings_path)" ] \ && grep -E -q "^$op=" $bashopts_tool_settings_path; then eval "tval=$(grep -E "^${op}=" $bashopts_tool_settings_path | sed -E "s/^[^=]+=//g")" fi if [[ ! -v tval ]] && [ -n "$dval" ]; then Loading @@ -462,16 +471,21 @@ bashopts_process_args() { fi fi if [ "${bashopts_optprop_setting[$op]}" == "true" ]; then if [ -n "$bashopts_tool_settings_path" ]; then # vrite the value to the setting file ( sed -i "/^$op=/d" $bashopts_tool_settings_path if [ "${!op}" != "$dval" ]; then echo "$(bashopts_get_def_full $op)" >> $bashopts_tool_settings_path fi ) || bashopts_warning "Please check the settings file" else bashopts_warning "No settings file specified" fi fi done if [ "$__bashopts_display_help__" == "true" ]; then bashopts_diplay_help exit 0 fi bashopts_commands=("$@") } test.sh +29 −0 Original line number Diff line number Diff line Loading @@ -142,6 +142,35 @@ _test_case_8() { req_test_eq "${array_value[1]}" "value 2" } _test_case_9() { bashopts_setup -n "$0" -d "Test case $0" bashopts_parse_args command1 "command 2" bashopts_process_args req_test_eq "${#bashopts_commands[@]}" "2" req_test_eq "${bashopts_commands[0]}" "command1" req_test_eq "${bashopts_commands[1]}" "command 2" } _test_case_10() { bashopts_setup -n "$0" -d "Test case $0" bashopts_declare -n simple_val -s -l value -d "Simple value" -t string -v 'val' bashopts_parse_args --value=test req_test_eq "$(bashopts_process_args 2>&1)" "[WARNING] No settings file specified" } _test_case_11() { bashopts_setup -n "$0" -d "Test case $0" -s /this/is/an/inacessible/file bashopts_declare -n simple_val -s -l value -d "Simple value" -t string -v 'val' bashopts_parse_args --value=test req_test_eq "$(bashopts_process_args 2>&1 | grep 'WARNING')" "[WARNING] Please check the settings file" } if [ ${#} -eq 0 ]; then for t in $(grep -E -o '_test_case_\w+\>' $0); do echo "=> Test case $t" Loading Loading
bashopts.sh +22 −8 Original line number Diff line number Diff line Loading @@ -53,6 +53,14 @@ bashopts_error() { exit 1 } bashopts_warning() { local l >&2 echo -n "[WARNING] " for l in "$@"; do >&2 echo -e "$l" done } bashopts_regex_escape () { echo $1 | sed 's/[][()\.^$\/?*+]/\\&/g' } Loading Loading @@ -115,7 +123,6 @@ bashopts_optlist=() bashopts_commands=() bashopts_extra_args=() bashopts_tool_name=$0 bashopts_tool_settings_path="/dev/null" # STEP 1: setup bashopts_setup() { Loading Loading @@ -423,12 +430,12 @@ bashopts_parse_args() { ;; esac done bashopts_commands=("$@") } # STEP 4: process arg bashopts_process_args() { local dval ival touch $bashopts_tool_settings_path if [ "$__bashopts_display_help__" == "true" ]; then bashopts_non_interactive="true" fi Loading @@ -442,7 +449,9 @@ bashopts_process_args() { if ! [[ -v $op ]] || [ "$bashopts_tool_force_edit" == "true" ]; then unset tval # read the value from file if ! [[ -v $op ]] && [ "${bashopts_optprop_setting[$op]}" == "true" ] && grep -E -q "^$op=" $bashopts_tool_settings_path; then if ! [[ -v $op ]] && [ "${bashopts_optprop_setting[$op]}" == "true" ] \ && [ -f "$(readlink -f $bashopts_tool_settings_path)" ] \ && grep -E -q "^$op=" $bashopts_tool_settings_path; then eval "tval=$(grep -E "^${op}=" $bashopts_tool_settings_path | sed -E "s/^[^=]+=//g")" fi if [[ ! -v tval ]] && [ -n "$dval" ]; then Loading @@ -462,16 +471,21 @@ bashopts_process_args() { fi fi if [ "${bashopts_optprop_setting[$op]}" == "true" ]; then if [ -n "$bashopts_tool_settings_path" ]; then # vrite the value to the setting file ( sed -i "/^$op=/d" $bashopts_tool_settings_path if [ "${!op}" != "$dval" ]; then echo "$(bashopts_get_def_full $op)" >> $bashopts_tool_settings_path fi ) || bashopts_warning "Please check the settings file" else bashopts_warning "No settings file specified" fi fi done if [ "$__bashopts_display_help__" == "true" ]; then bashopts_diplay_help exit 0 fi bashopts_commands=("$@") }
test.sh +29 −0 Original line number Diff line number Diff line Loading @@ -142,6 +142,35 @@ _test_case_8() { req_test_eq "${array_value[1]}" "value 2" } _test_case_9() { bashopts_setup -n "$0" -d "Test case $0" bashopts_parse_args command1 "command 2" bashopts_process_args req_test_eq "${#bashopts_commands[@]}" "2" req_test_eq "${bashopts_commands[0]}" "command1" req_test_eq "${bashopts_commands[1]}" "command 2" } _test_case_10() { bashopts_setup -n "$0" -d "Test case $0" bashopts_declare -n simple_val -s -l value -d "Simple value" -t string -v 'val' bashopts_parse_args --value=test req_test_eq "$(bashopts_process_args 2>&1)" "[WARNING] No settings file specified" } _test_case_11() { bashopts_setup -n "$0" -d "Test case $0" -s /this/is/an/inacessible/file bashopts_declare -n simple_val -s -l value -d "Simple value" -t string -v 'val' bashopts_parse_args --value=test req_test_eq "$(bashopts_process_args 2>&1 | grep 'WARNING')" "[WARNING] Please check the settings file" } if [ ${#} -eq 0 ]; then for t in $(grep -E -o '_test_case_\w+\>' $0); do echo "=> Test case $t" Loading