]> git.corax.cc Git - toolbox/commitdiff
include/opt: Emit an error message when an option is redefined
authorMatthias Kruk <m@m10k.eu>
Thu, 11 Aug 2022 12:27:49 +0000 (21:27 +0900)
committerMatthias Kruk <m@m10k.eu>
Thu, 11 Aug 2022 12:27:49 +0000 (21:27 +0900)
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

index 6d4035b51d0c28b21462737d7d3db8c00d358c64..ceebc5eae27142077401c5812f9590ccca48e847 100644 (file)
@@ -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