]> git.corax.cc Git - toolbox/commitdiff
include/opt: Show default values in help text
authorMatthias Kruk <m@m10k.eu>
Thu, 11 Aug 2022 17:46:52 +0000 (02:46 +0900)
committerMatthias Kruk <m@m10k.eu>
Thu, 11 Aug 2022 17:46:52 +0000 (02:46 +0900)
The opt module does not show the default values of options in the
help text, leaving the user guessing what the values of options are
if they are not specified on the command line.

This commit modifies `opt_print_help()` so that it displays the
default values of options that have a value and a default set.

include/opt.sh

index 2fce9c5fb05f66f63ff85b2f0a5dc49c341d0fdb..d9c596a1192ddef74637fc4da4ef6a686dbab83b 100644 (file)
@@ -156,6 +156,10 @@ opt_print_help() {
 
                printf "\t-%s\t--%s\t%s\n" \
                       "$short" "$long" "$desc"
+               if (( ${__opt_flags["$long"]} & __opt_flag_has_value )) &&
+                  array_contains "$long" "${!__opt_default[@]}"; then
+                       printf '\t\t\t(Default: %s)\n' "${__opt_default[$long]}"
+               fi
        done | column -s $'\t' -t
 
        return 2