]> git.corax.cc Git - toolbox/commitdiff
include/opt: Initialize value of options that don't have a value
authorMatthias Kruk <m@m10k.eu>
Thu, 11 Aug 2022 18:14:19 +0000 (03:14 +0900)
committerMatthias Kruk <m@m10k.eu>
Thu, 11 Aug 2022 18:14:19 +0000 (03:14 +0900)
The opt module uses the value of options that aren't followed by a
parameter to count how often the option was on the command line.
However, in this case it never initializes the value.

This commit modifies the opt module so that it initializes the
value of options that are not followed by a parameter to 0.

include/opt.sh

index 6bb67382b628933d3b8f0e86ccea66a5a53a619d..80abb52cff110b47b3271ff2181bce7d75a78f4d 100644 (file)
@@ -39,19 +39,16 @@ __init() {
        declare -Axg __opt_map
        declare -Axg __opt_required
 
-       opt_add_arg "h" "help" "" 0 \
-                   "Print this text" \
-                   '' \
-                   opt_print_help
-
-       opt_add_arg "v" "verbose" "" 0 \
-                   "Be more verbose" \
-                   '' \
-                   log_increase_verbosity
-       opt_add_arg "q" "quiet" "" 0 \
-                   "Be less verbose" \
-                   '' \
-                   log_decrease_verbosity
+       opt_add_arg "h" "help" "" 0           \
+                   "Print this text"         \
+                   '' opt_print_help
+
+       opt_add_arg "v" "verbose" "" 0        \
+                   "Be more verbose"         \
+                   '' log_increase_verbosity
+       opt_add_arg "q" "quiet" "" 0          \
+                   "Be less verbose"         \
+                   '' log_decrease_verbosity
 
        return 0
 }
@@ -128,6 +125,10 @@ opt_add_arg() {
                __opt_required["$long"]="$long"
        fi
 
+       if ! (( parsed_flags & __opt_flag_has_value )); then
+               __opt_value["$long"]=0
+       fi
+
        return 0
 }