Commit e380b399 authored by Emeric Verschuur's avatar Emeric Verschuur Committed by Emeric Verschuur
Browse files

Replace message functions by bashopts_log function

parent b099d64e
...@@ -45,29 +45,34 @@ trap 'bashopts_exit_handle' ERR ...@@ -45,29 +45,34 @@ trap 'bashopts_exit_handle' ERR
set -o errtrace set -o errtrace
# display a error (fatal) # display a error (fatal)
bashopts_critical() { bashopts_log() {
>&2 printf "[ERROR] %s\n" "$@" if [ -n "$bashopts_log_handler" ]; then
$bashopts_log_handler "$@"
return;
fi
local level=$1;
shift || bashopts_log C "Usage bashopts_log <level> message"
case "${level,,}" in
c|critical)
>&2 printf "[CRIT] %s\n" "$@"
exit 1 exit 1
;;
e|error)
>&2 printf "[ERRO] %s\n" "$@"
;;
w|warning)
>&2 printf "[WARN] %s\n" "$@"
;;
*)
bashopts_log C "Invalid log level: $level"
;;
esac
} }
if [ ! "${BASH_VERSINFO[0]}" -ge 4 ]; then if [ ! "${BASH_VERSINFO[0]}" -ge 4 ]; then
bashopts_critical "bashopts require BASH version 4 or greater" bashopts_log C "bashopts require BASH version 4 or greater"
fi fi
# display a error (non fatal)
bashopts_error() {
>&2 printf "[ERROR] %s\n" "$@"
}
# display a warning (non fatal)
bashopts_warning() {
>&2 printf "[WARNING] %s\n" "$@"
}
bashopts_regex_escape () {
echo $1 | sed 's/[][()\.^$\/?*+]/\\&/g'
}
# extract the value part of a declaration ("the value") # extract the value part of a declaration ("the value")
bashopts_get_def() { bashopts_get_def() {
declare | grep "^$1=" | sed -E 's/^[^=]+=//g' declare | grep "^$1=" | sed -E 's/^[^=]+=//g'
...@@ -88,7 +93,7 @@ bashopts_check_opt_name() { ...@@ -88,7 +93,7 @@ bashopts_check_opt_name() {
echo $1 echo $1
return 0 return 0
fi fi
bashopts_error "'$1' is not a valid variable name" bashopts_log E "'$1' is not a valid variable name"
return 1 return 1
} }
...@@ -101,23 +106,23 @@ bashopts_check_number() { ...@@ -101,23 +106,23 @@ bashopts_check_number() {
echo $1 echo $1
return 0 return 0
fi fi
bashopts_error "'$1' is not a valid number" bashopts_log E "'$1' is not a valid number"
return 1 return 1
} }
# check and format a boolean value # check and format a boolean value
bashopts_check_boolean() { bashopts_check_boolean() {
case "$1" in case "${1,,}" in
''|f|false|F|FALSE|False|0) ''|f|false|0)
echo "false" echo "false"
return 0 return 0
;; ;;
t|true|T|TRUE|True|1) t|true|1)
echo "true" echo "true"
return 0 return 0
;; ;;
*) *)
bashopts_error "'$1' is not a valid boolean value" bashopts_log E "'$1' is not a valid boolean value"
return 1 return 1
;; ;;
esac esac
...@@ -155,7 +160,7 @@ bashopts_tool_name=$0 ...@@ -155,7 +160,7 @@ bashopts_tool_name=$0
bashopts_setup() { bashopts_setup() {
local arg arglist no_default_opts non_interactive disable_interactive local arg arglist no_default_opts non_interactive disable_interactive
if ! arglist=$(getopt -o "n:d:u:s:yxp" -n "$0 " -- "$@"); then if ! arglist=$(getopt -o "n:d:u:s:yxp" -n "$0 " -- "$@"); then
bashopts_critical "Usage bashopts_setup:" \ bashopts_log C "Usage bashopts_setup:" \
" -n <val> Tool name" \ " -n <val> Tool name" \
" -d <val> Tool description" \ " -d <val> Tool description" \
" -u <val> Tool usage description" \ " -u <val> Tool usage description" \
...@@ -178,14 +183,14 @@ bashopts_setup() { ...@@ -178,14 +183,14 @@ bashopts_setup() {
-x) disable_interactive="true";; -x) disable_interactive="true";;
-p) bashopts_tool_settings_force_write="true";; -p) bashopts_tool_settings_force_write="true";;
--) break;; --) break;;
*) bashopts_critical "Fatal error";; *) bashopts_log C "Fatal error";;
esac esac
done done
if [ -z "$bashopts_tool_name" ]; then if [ -z "$bashopts_tool_name" ]; then
bashopts_critical "Undefined tool name" bashopts_log C "Undefined tool name"
fi fi
if [ -z "$bashopts_tool_description" ]; then if [ -z "$bashopts_tool_description" ]; then
bashopts_critical "Undefined tool description" bashopts_log C "Undefined tool description"
fi fi
bashopts_tool_usage=${bashopts_tool_usage:-"$bashopts_tool_name [options and commands] [-- [extra args]]"} bashopts_tool_usage=${bashopts_tool_usage:-"$bashopts_tool_name [options and commands] [-- [extra args]]"}
# add the default options # add the default options
...@@ -205,7 +210,7 @@ bashopts_setup() { ...@@ -205,7 +210,7 @@ bashopts_setup() {
bashopts_declare() { bashopts_declare() {
local arg arglist options local arg arglist options
if ! arglist=$(getopt -o "n:v:e:o:l:d:t:m:k:rsi" -n "$0 " -- "$@"); then if ! arglist=$(getopt -o "n:v:e:o:l:d:t:m:k:rsi" -n "$0 " -- "$@"); then
bashopts_critical "Usage bashopts_declare:" \ bashopts_log C "Usage bashopts_declare:" \
" -n <val> Name" \ " -n <val> Name" \
" -v <val> Default value" \ " -v <val> Default value" \
" -e <val> Bash expression: like default but this expression is computed and can contain variables and other bash expression" \ " -e <val> Bash expression: like default but this expression is computed and can contain variables and other bash expression" \
...@@ -239,12 +244,12 @@ bashopts_declare() { ...@@ -239,12 +244,12 @@ bashopts_declare() {
-i) options[interactive]="true";; -i) options[interactive]="true";;
-r) options[req_value]="true";; -r) options[req_value]="true";;
--) break;; --) break;;
*) bashopts_critical "Fatal error";; *) bashopts_log C "Fatal error";;
esac esac
done done
# Check incompatible -v and -r options # Check incompatible -v and -r options
if [ -n "${options[default]}" ] && [ "${options[req_value]}" == "true" ]; then if [ -n "${options[default]}" ] && [ "${options[req_value]}" == "true" ]; then
bashopts_critical "bashopts_declare: -r and -v options cannot be activated at the same time" bashopts_log C "bashopts_declare: -r and -v options cannot be activated at the same time"
fi fi
# format the type and check/format the default value # format the type and check/format the default value
case "${options[type]}" in case "${options[type]}" in
...@@ -258,7 +263,7 @@ bashopts_declare() { ...@@ -258,7 +263,7 @@ bashopts_declare() {
options[type]="number" options[type]="number"
;; ;;
*) *)
bashopts_critical "Invalid type ${options[type]}" bashopts_log C "Invalid type ${options[type]}"
;; ;;
esac esac
# Setup check value method # Setup check value method
...@@ -273,7 +278,7 @@ bashopts_declare() { ...@@ -273,7 +278,7 @@ bashopts_declare() {
if [ "${options[type]}" != "string" ] || [[ -v options[default] ]]; then if [ "${options[type]}" != "string" ] || [[ -v options[default] ]]; then
# Check the default value format # Check the default value format
options[default]="$(${options[check]} "${options[default]}")" \ options[default]="$(${options[check]} "${options[default]}")" \
|| bashopts_critical "Invalid default value for ${options[name]} option" || bashopts_log C "Invalid default value for ${options[name]} option"
fi fi
;; ;;
a|add) a|add)
...@@ -281,30 +286,30 @@ bashopts_declare() { ...@@ -281,30 +286,30 @@ bashopts_declare() {
options[method]="add" options[method]="add"
;; ;;
*) *)
bashopts_critical "Invalid method ${options[method]}" bashopts_log C "Invalid method ${options[method]}"
;; ;;
esac esac
# Check option name # Check option name
if [[ -v bashopts_optprop_name[${options[name]}] ]]; then if [[ -v bashopts_optprop_name[${options[name]}] ]]; then
bashopts_critical "Dupplicate option name '${options[name]}'" bashopts_log C "Dupplicate option name '${options[name]}'"
fi fi
# check the short option # check the short option
if [[ -v options[short_opt] ]]; then if [[ -v options[short_opt] ]]; then
if ! [[ ${options[short_opt]} =~ ^[a-zA-Z0-9_-]$ ]]; then if ! [[ ${options[short_opt]} =~ ^[a-zA-Z0-9_-]$ ]]; then
bashopts_critical "Invalid short option ${options[short_opt]}" bashopts_log C "Invalid short option ${options[short_opt]}"
fi fi
if [[ -v bashopts_arg2op[-${options[short_opt]}] ]]; then if [[ -v bashopts_arg2op[-${options[short_opt]}] ]]; then
bashopts_critical "Dupplicate short option '${options[short_opt]}'" bashopts_log C "Dupplicate short option '${options[short_opt]}'"
fi fi
bashopts_arg2op[-${options[short_opt]}]=${options[name]} bashopts_arg2op[-${options[short_opt]}]=${options[name]}
fi fi
# check the long option # check the long option
if [[ -v options[long_opt] ]]; then if [[ -v options[long_opt] ]]; then
if ! [[ ${options[long_opt]} =~ ^[a-zA-Z0-9_-]{2,}$ ]]; then if ! [[ ${options[long_opt]} =~ ^[a-zA-Z0-9_-]{2,}$ ]]; then
bashopts_critical "Invalid long option ${options[long_opt]}" bashopts_log C "Invalid long option ${options[long_opt]}"
fi fi
if [[ -v bashopts_arg2op[--${options[long_opt]}] ]]; then if [[ -v bashopts_arg2op[--${options[long_opt]}] ]]; then
bashopts_critical "Dupplicate long option '${options[long_opt]}'" bashopts_log C "Dupplicate long option '${options[long_opt]}'"
fi fi
bashopts_arg2op[--${options[long_opt]}]=${options[name]} bashopts_arg2op[--${options[long_opt]}]=${options[name]}
fi fi
...@@ -328,14 +333,14 @@ bashopts_math_min() { ...@@ -328,14 +333,14 @@ bashopts_math_min() {
# join array element # join array element
bashopts_join_by() { bashopts_join_by() {
local IFS="$1" local IFS="$1"
shift || bashopts_critical "Usage: bashopts_join_by <character> [elt1 [elt2...]]" shift || bashopts_log C "Usage: bashopts_join_by <character> [elt1 [elt2...]]"
echo "$*" echo "$*"
} }
# dump an option value by its name # dump an option value by its name
bashopts_dump_value() { bashopts_dump_value() {
local op=$1 local op=$1
shift || bashopts_critical "Usage: bashopts_dump_value op_name" shift || bashopts_log C "Usage: bashopts_dump_value op_name"
[[ -v "$op" ]] || return 0 [[ -v "$op" ]] || return 0
if [ "${bashopts_optprop_method[$op]}" == "set" ]; then if [ "${bashopts_optprop_method[$op]}" == "set" ]; then
if [ "${bashopts_optprop_type[$op]}" == "string" ]; then if [ "${bashopts_optprop_type[$op]}" == "string" ]; then
...@@ -507,7 +512,7 @@ bashopts_parse_args() { ...@@ -507,7 +512,7 @@ bashopts_parse_args() {
esac esac
;; ;;
*) *)
bashopts_critical "Fatal error: args" bashopts_log C "Fatal error: args"
;; ;;
esac esac
done done
...@@ -519,7 +524,7 @@ bashopts_parse_args() { ...@@ -519,7 +524,7 @@ bashopts_parse_args() {
# display an array: [val1, val2, ...] # display an array: [val1, val2, ...]
bashopts_dump_array() { bashopts_dump_array() {
local type=$1 local type=$1
shift || bashopts_critical "Usage: bashopts_dump_array type elt1 [elt2...]" shift || bashopts_log C "Usage: bashopts_dump_array type elt1 [elt2...]"
echo -n "[" echo -n "["
if [ "$type" == "string" ]; then if [ "$type" == "string" ]; then
echo -n "\"${1//\"/\\\"}\"" echo -n "\"${1//\"/\\\"}\""
...@@ -542,7 +547,7 @@ bashopts_dump_array() { ...@@ -542,7 +547,7 @@ bashopts_dump_array() {
bashopts_process_option() { bashopts_process_option() {
local dval tval ival op arg arglist check val_req edit_req local dval tval ival op arg arglist check val_req edit_req
if ! arglist=$(getopt -o "n:k:r" -n "bashopts_process_option " -- "$@"); then if ! arglist=$(getopt -o "n:k:r" -n "bashopts_process_option " -- "$@"); then
bashopts_critical "Usage bashopts_process_opt" \ bashopts_log C "Usage bashopts_process_opt" \
" -n <val> property name" \ " -n <val> property name" \
" -k <val> override value check function" \ " -k <val> override value check function" \
" -r At least one value required" " -r At least one value required"
...@@ -557,11 +562,11 @@ bashopts_process_option() { ...@@ -557,11 +562,11 @@ bashopts_process_option() {
-k) check=$1; shift;; -k) check=$1; shift;;
-r) val_req="true";; -r) val_req="true";;
--) break;; --) break;;
*) bashopts_critical "Fatal error";; *) bashopts_log C "Fatal error";;
esac esac
done done
test -n "$op" || \ test -n "$op" || \
bashopts_critical "bashopts_process_option: missing -n option" bashopts_log C "bashopts_process_option: missing -n option"
if [ -z "$check" ]; then if [ -z "$check" ]; then
check="${bashopts_optprop_check[$op]}" check="${bashopts_optprop_check[$op]}"
fi fi
...@@ -594,7 +599,7 @@ bashopts_process_option() { ...@@ -594,7 +599,7 @@ bashopts_process_option() {
for (( i=0; i<${#tval[@]}; i++)); do for (( i=0; i<${#tval[@]}; i++)); do
if ! $check "${tval[$i]}" > /dev/null; then if ! $check "${tval[$i]}" > /dev/null; then
if [ "$BASHOPTS_INTERACTIVE" != "true" ]; then if [ "$BASHOPTS_INTERACTIVE" != "true" ]; then
bashopts_critical "Non interactive mode: Exit due to one or more error" bashopts_log C "Non interactive mode: Exit due to one or more error"
fi fi
# (re)enable edition # (re)enable edition
edit_req="true" edit_req="true"
...@@ -602,9 +607,9 @@ bashopts_process_option() { ...@@ -602,9 +607,9 @@ bashopts_process_option() {
fi fi
done done
elif [ "$val_req" == "true" ]; then elif [ "$val_req" == "true" ]; then
bashopts_error "At least one value required" bashopts_log E "At least one value required"
if [ "$BASHOPTS_INTERACTIVE" != "true" ]; then if [ "$BASHOPTS_INTERACTIVE" != "true" ]; then
bashopts_critical "Non interactive mode: Exit due to one or more error" bashopts_log C "Non interactive mode: Exit due to one or more error"
fi fi
# (re)enable edition # (re)enable edition
edit_req="true" edit_req="true"
...@@ -625,7 +630,7 @@ bashopts_process_option() { ...@@ -625,7 +630,7 @@ bashopts_process_option() {
if [ "${bashopts_optprop_method[$op]}" == "add" ]; then if [ "${bashopts_optprop_method[$op]}" == "add" ]; then
# array value # array value
if ! eval "tval=$ival" 2>/dev/null; then if ! eval "tval=$ival" 2>/dev/null; then
bashopts_error "'$ival' must be written in BASH array format: '( \"val 1\" \"val2\" \"val3...\" )'" bashopts_log C "'$ival' must be written in BASH array format: '( \"val 1\" \"val2\" \"val3...\" )'"
unset tval unset tval
continue continue
fi fi
...@@ -636,7 +641,7 @@ bashopts_process_option() { ...@@ -636,7 +641,7 @@ bashopts_process_option() {
fi fi
# check format # check format
if [ "${#tval[@]}" -eq 0 ] && [ "$val_req" == "true" ]; then if [ "${#tval[@]}" -eq 0 ] && [ "$val_req" == "true" ]; then
bashopts_error "At least one value required" bashopts_log E "At least one value required"
unset tval unset tval
continue continue
fi fi
...@@ -682,9 +687,9 @@ bashopts_process_option() { ...@@ -682,9 +687,9 @@ bashopts_process_option() {
# force_write is true # force_write is true
echo "$(bashopts_get_def_full $op)" >> $bashopts_tool_settings_path echo "$(bashopts_get_def_full $op)" >> $bashopts_tool_settings_path
fi fi
) || bashopts_warning "Please check the settings file" ) || bashopts_log W "Please check the settings file"
else else
bashopts_warning "No settings file specified" bashopts_log W "No settings file specified"
fi fi
fi fi
if [ "$op" == "BASHOPTS_NON_INTERACTIVE" ] && ! [[ -v BASHOPTS_INTERACTIVE ]]; then if [ "$op" == "BASHOPTS_NON_INTERACTIVE" ] && ! [[ -v BASHOPTS_INTERACTIVE ]]; then
......
...@@ -159,7 +159,7 @@ _test_case_10() { ...@@ -159,7 +159,7 @@ _test_case_10() {
bashopts_declare -n simple_val -s -l value -d "Simple value" -t string -v 'val' bashopts_declare -n simple_val -s -l value -d "Simple value" -t string -v 'val'
bashopts_parse_args --value=test bashopts_parse_args --value=test
req_test_eq "$(bashopts_process_opts 2>&1)" "[WARNING] No settings file specified" req_test_eq "$(bashopts_process_opts 2>&1)" "[WARN] No settings file specified"
} }
_test_case_11() { _test_case_11() {
...@@ -168,7 +168,7 @@ _test_case_11() { ...@@ -168,7 +168,7 @@ _test_case_11() {
bashopts_declare -n simple_val -s -l value -d "Simple value" -t string -v 'val' bashopts_declare -n simple_val -s -l value -d "Simple value" -t string -v 'val'
bashopts_parse_args --value=test bashopts_parse_args --value=test
req_test_eq "$(bashopts_process_opts 2>&1 | grep 'WARNING')" "[WARNING] Please check the settings file" req_test_eq "$(bashopts_process_opts 2>&1 | grep 'WARN')" "[WARN] Please check the settings file"
} }
_test_case_12() { _test_case_12() {
...@@ -276,9 +276,12 @@ _test_case_18() { ...@@ -276,9 +276,12 @@ _test_case_18() {
bashopts_declare -n a_boolean_value -l boolean -m set -d "A boolean value" -t boolean -i bashopts_declare -n a_boolean_value -l boolean -m set -d "A boolean value" -t boolean -i
bashopts_parse_args bashopts_parse_args
req_test_eq "$(bashopts_process_opts 2>&1 >/dev/null <<< "tru")" "[ERROR] 'tru' is not a valid boolean value" req_test_eq "$(bashopts_process_opts 2>&1 >/dev/null <<< "tru" \
req_test_eq "$(bashopts_process_opts 2>&1 >/dev/null <<< "fa")" "[ERROR] 'fa' is not a valid boolean value" | grep '^\[ERR')" "[ERRO] 'tru' is not a valid boolean value"
req_test_eq "$(bashopts_process_opts 2>&1 >/dev/null <<< "5")" "[ERROR] '5' is not a valid boolean value" req_test_eq "$(bashopts_process_opts 2>&1 >/dev/null <<< "fa" \
| grep '^\[ERR')" "[ERRO] 'fa' is not a valid boolean value"
req_test_eq "$(bashopts_process_opts 2>&1 >/dev/null <<< "5" \
| grep '^\[ERR')" "[ERRO] '5' is not a valid boolean value"
} }
_test_case_19() { _test_case_19() {
...@@ -297,7 +300,7 @@ is_eq_to_null() { ...@@ -297,7 +300,7 @@ is_eq_to_null() {
echo "$1" echo "$1"
return 0 return 0
fi fi
bashopts_error "Not equal to NULL" bashopts_log E "Not equal to NULL"
return 1 return 1
} }
...@@ -309,11 +312,9 @@ _test_case_20() { ...@@ -309,11 +312,9 @@ _test_case_20() {
bashopts_parse_args bashopts_parse_args
bashopts_process_opts bashopts_process_opts
req_test_eq "$(echo "$(bashopts_process_option -n value -k is_eq_to_null -r 2>&1 > /dev/null <<< "na")" \ req_test_eq "$(echo "$(bashopts_process_option -n value -k is_eq_to_null -r 2>&1 > /dev/null <<< "na")" \
| grep '\[ERROR\] Not equal to NULL' \ | grep '^\[ERR' | tail -n 1 || true)" '[ERRO] Not equal to NULL'
| head -n 1 || true)" '[ERROR] Not equal to NULL'
req_test_eq "$(echo "$(bashopts_process_option -n value -k is_eq_to_null -r 2>&1 > /dev/null <<< "()")" \ req_test_eq "$(echo "$(bashopts_process_option -n value -k is_eq_to_null -r 2>&1 > /dev/null <<< "()")" \
| grep '\[ERROR\] At least one value required' \ | grep '^\[ERR' | tail -n 1 || true)" '[ERRO] At least one value required'
| head -n 1 || true)" '[ERROR] At least one value required'
bashopts_process_option -n value -k is_eq_to_null -r > /dev/null 2>&1 <<< "NULL" bashopts_process_option -n value -k is_eq_to_null -r > /dev/null 2>&1 <<< "NULL"
} }
...@@ -325,8 +326,7 @@ _test_case_21() { ...@@ -325,8 +326,7 @@ _test_case_21() {
bashopts_parse_args -n bashopts_parse_args -n
bashopts_process_opts bashopts_process_opts
req_test_eq "$(echo "$(bashopts_process_option -n value -k is_not_empty -r 2>&1 > /dev/null)" \ req_test_eq "$(echo "$(bashopts_process_option -n value -k is_not_empty -r 2>&1 > /dev/null)" \
| grep '\[ERROR\] At least one value required' \ | grep '^\[ERR' | head -n 1)" '[ERRO] At least one value required'
| head -n 1)" '[ERROR] At least one value required'
} }
_test_case_22() { _test_case_22() {
...@@ -388,7 +388,7 @@ _test_case_27() { ...@@ -388,7 +388,7 @@ _test_case_27() {
bashopts_setup -n "$0" -d "Test case $0" 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)" \ 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" "[CRIT] bashopts_declare: -r and -v options cannot be activated at the same time"
} }
_test() { _test() {
......
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