From 8faa458a58974b8fb21d16ec96675459bd84fb23 Mon Sep 17 00:00:00 2001 From: Matthias Kruk Date: Thu, 11 Aug 2022 21:27:49 +0900 Subject: [PATCH] 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. --- include/opt.sh | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) 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 -- 2.47.3