Commit d0b40e7b authored by Emeric Verschuur's avatar Emeric Verschuur

Adding unset (variables and array) and tests

parent fa6fb3ca
......@@ -90,6 +90,8 @@ bashopts_process_args
- All the options are stored on bash variable (e.g.: option name first_name on the following example give $first_name bash variable)
- All other command (other than argument beginning by '-' or '--') are stored into the ```bashopts_commands``` bash array. You can iterate by ```for command in ${bashopts_commands[@]}; do ...; done```
- All extra arguments (```mytool --arg1 --arg2 cmd1 cmd2 -- --extra-arg1 --extra-arg2 ...```) are stored into the ```bashopts_extra_args``` bash array.
- If the argument value is empty (e.g.: --long-arg "") the value is unset (also usefull to clear an array)
- To add several default value for an array, add: bashopts_declare -e '("value 1" "value 2" "... etc.")'
## Example
......
......@@ -375,7 +375,7 @@ bashopts_parse_args() {
done
long_opts=$(bashopts_join_by , ${long_opts[@]})
if ! args=$(getopt -o $short_opts -l "$long_opts" -n "$bashopts_tool_name " -- "${args[@]}"); then
if ! args=$(getopt -o $short_opts -l "$long_opts" -n "$bashopts_tool_name" -- "${args[@]}"); then
>&2 bashopts_diplay_help
exit 1
fi
......@@ -392,6 +392,10 @@ bashopts_parse_args() {
val="$1"
shift
op=${bashopts_opt2name[$arg]}
if [ -z "$val" ] && [ "${bashopts_optprop_type[$op]}" != "boolean" ]; then
unset $op
continue
fi
case "${bashopts_optprop_type[$op]}" in
boolean)
if [ -z "$val" ]; then
......@@ -431,7 +435,7 @@ bashopts_process_args() {
for op in "${bashopts_optlist[@]}"; do
# eval or get default value
if [[ -v bashopts_optprop_expression[$op] ]]; then
dval="$(eval "echo \"${bashopts_optprop_expression[$op]}\"")"
eval "dval=${bashopts_optprop_expression[$op]}"
else
dval=${bashopts_optprop_default[$op]}
fi
......@@ -443,7 +447,7 @@ bashopts_process_args() {
fi
if [[ ! -v tval ]] && [ -n "$dval" ]; then
# set default value
tval=$dval
eval "tval=$(bashopts_get_def dval)"
fi
if [ "${bashopts_optprop_interactive[$op]}" == "true" ] && [ "$bashopts_non_interactive" != "true" ]; then
echo "* ${bashopts_optprop_description[$op]}"
......
......@@ -47,9 +47,9 @@ _test_case_1() {
}
_test_case_2() {
rm -f /tmp/myapp_case2rc
rm -f /tmp/bashopts_testrc
bashopts_setup -n "myapp_case2" -d "Test case 2" -s /tmp/myapp_case2rc
bashopts_setup -n "$0" -d "Test case $0" -s /tmp/bashopts_testrc
bashopts_declare -n base_path -l base -o b -d "Base path" -t string -v "/opt/test" -i -s
bashopts_declare -n config_path -l config -o c -d "Configuration path" -t string -e "\$base_path/myconf.cfg" -i -s
......@@ -58,27 +58,27 @@ _test_case_2() {
bashopts_process_args
req_test_eq "$config_path" "/tmp/myconf.cfg"
req_test_eq "$(cat /tmp/myapp_case2rc)" "base_path=/tmp"
req_test_eq "$(cat /tmp/bashopts_testrc)" "base_path=/tmp"
}
_test_case_3() {
rm -f /tmp/myapp_case3rc
rm -f /tmp/bashopts_testrc
bashopts_setup -n "myapp_case3" -d "Test case 3" -s /tmp/myapp_case3rc
bashopts_setup -n "$0" -d "Test case $0" -s /tmp/bashopts_testrc
bashopts_declare -n process_file_list -l input-file -o f -d "Input file" -t string -m add -s
bashopts_parse_args --input-file foobar --input-file=/foo/bar
bashopts_process_args
req_test_eq "$(bashopts_join_by , ${process_file_list[@]})" "foobar,/foo/bar"
eval declare -x test="$(grep -E "^process_file_list=" /tmp/myapp_case3rc | sed -E "s/^[^=]+=//g")"
eval declare -x test="$(grep -E "^process_file_list=" /tmp/bashopts_testrc | sed -E "s/^[^=]+=//g")"
req_test_eq "$(bashopts_join_by , ${test[@]})" "foobar,/foo/bar"
}
_test_case_4() {
rm -f /tmp/myapp_case4rc
rm -f /tmp/bashopts_testrc
bashopts_setup -n "myapp_case4" -d "Test case 4" -s /tmp/myapp_case4rc
bashopts_setup -n "$0" -d "Test case $0" -s /tmp/bashopts_testrc
bashopts_declare -n array_value -l value -m add -d "Array value" -t string -s
......@@ -88,14 +88,14 @@ _test_case_4() {
req_test_eq "${array_value[0]}" "value1"
req_test_eq "${array_value[1]}" "value 2"
req_test_eq "${array_value[2]}" "value '\"' \$3"
req_test_eq "$(cat /tmp/myapp_case4rc)" "array_value=([0]=\"value1\" [1]=\"value 2\" [2]=\"value '\\\"' \\\$3\")"
req_test_eq "$(cat /tmp/bashopts_testrc)" "array_value=([0]=\"value1\" [1]=\"value 2\" [2]=\"value '\\\"' \\\$3\")"
req_test_eq "$(bashopts_diplay_help | grep -E -o "^\\s+\-\-value \[\"value1\", \"value 2\", \"value '\\\\\"' \\\$3\"]\s+")" " --value [\"value1\", \"value 2\", \"value '\\\"' \$3\"] "
}
_test_case_5() {
rm -f /tmp/myapp_case5rc
rm -f /tmp/bashopts_testrc
bashopts_setup -n "myapp_case5" -d "Test case 5" -s /tmp/myapp_case5rc
bashopts_setup -n "$0" -d "Test case $0" -s /tmp/bashopts_testrc
bashopts_declare -n array_value -l value -m add -d "Array value" -t string -s
......@@ -106,9 +106,7 @@ _test_case_5() {
}
_test_case_6() {
rm -f /tmp/myapp_case6rc
bashopts_setup -n "myapp_case6" -d "Test case 6"
bashopts_setup -n "$0" -d "Test case $0"
bashopts_declare -n computed_value -e '$(( 2 + 3 * 4 ))' -t string
......@@ -118,6 +116,32 @@ _test_case_6() {
req_test_eq "$computed_value" "14"
}
_test_case_7() {
bashopts_setup -n "$0" -d "Test case $0"
bashopts_declare -n array_value -l value -m add -d "Array value" -t string
bashopts_parse_args --value "value 1" --value "value 1" --value "" --value "value 3" --value "value 4"
bashopts_process_args
req_test_eq "${#array_value[@]}" "2"
req_test_eq "${array_value[0]}" "value 3"
req_test_eq "${array_value[1]}" "value 4"
}
_test_case_8() {
bashopts_setup -n "$0" -d "Test case $0"
bashopts_declare -n array_value -l value -m add -d "Array value" -t string -e '("value 1" "value 2")'
bashopts_parse_args
bashopts_process_args
req_test_eq "${#array_value[@]}" "2"
req_test_eq "${array_value[0]}" "value 1"
req_test_eq "${array_value[1]}" "value 2"
}
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