Commit 5bedc712 authored by Emeric Verschuur's avatar Emeric Verschuur

Fix bashopts_commands & add settings file error recover

parent 2839ee24
......@@ -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'
}
......@@ -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() {
......@@ -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
......@@ -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
......@@ -462,10 +471,16 @@ bashopts_process_args() {
fi
fi
if [ "${bashopts_optprop_setting[$op]}" == "true" ]; 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
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
......@@ -473,5 +488,4 @@ bashopts_process_args() {
bashopts_diplay_help
exit 0
fi
bashopts_commands=("$@")
}
......@@ -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"
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment