From: Matthias Kruk Date: Thu, 11 Aug 2022 12:27:49 +0000 (+0900) Subject: include/opt: Emit an error message when an option is redefined X-Git-Url: https://git.corax.cc/?a=commitdiff_plain;h=8faa458a58974b8fb21d16ec96675459bd84fb23;p=toolbox include/opt: Emit an error message when an option is redefined When the caller attempts to redefine an option, `opt_add_arg()` fails without printing an error message, leaving the situation undetected if the caller does not check the return value of the call. This commit modifies `opt_add_arg()` to print an error message when a caller attempts to redefine an option. --- diff --git a/include/opt.sh b/include/opt.sh index 6d4035b..ceebc5e 100644 --- a/include/opt.sh +++ b/include/opt.sh @@ -52,6 +52,21 @@ __init() { return 0 } +_opt_is_defined() { + local options=("$@") + + local option + + for option in "${options[@]}"; do + if [[ -n "${__opt_map[$option]}" ]]; then + log_error "Option \"$option\" was already declared" + return 0 + fi + done + + return 1 +} + opt_add_arg() { local short="$1" local long="$2" @@ -65,8 +80,7 @@ opt_add_arg() { local bflags local i - if array_contains "$short" "${__opt_short[@]}" || - array_contains "$long" "${__opt_long[@]}"; then + if _opt_is_defined "-$short" "--$long"; then return 1 fi