From: Matthias Kruk Date: Thu, 11 Aug 2022 18:14:19 +0000 (+0900) Subject: include/opt: Initialize value of options that don't have a value X-Git-Url: https://git.corax.cc/?a=commitdiff_plain;h=8efd23d2548e1aebf0d7a36bf065ddf3c0b9dbba;p=toolbox include/opt: Initialize value of options that don't have a value 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. --- diff --git a/include/opt.sh b/include/opt.sh index 6bb6738..80abb52 100644 --- a/include/opt.sh +++ b/include/opt.sh @@ -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 }