From 6f17df841c99801a055a7d13df1fd89c59d8d4c7 Mon Sep 17 00:00:00 2001 From: Matthias Kruk Date: Fri, 12 Aug 2022 03:21:40 +0900 Subject: [PATCH] include/opt: Add a default regex and action The regex and action of an option may be unset, making it necessary to check their value before using them. This commit modifies the opt module to define a default regex and action in case the user didn't provide one, allowing the parser to omit checks if the two are unset. --- include/opt.sh | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/include/opt.sh b/include/opt.sh index 80abb52..9129933 100644 --- a/include/opt.sh +++ b/include/opt.sh @@ -99,8 +99,8 @@ opt_add_arg() { local flags="$3" local default="$4" local desc="$5" - local regex="$6" - local action="$7" + local regex="${6-.*}" + local action="${7-true}" local -i parsed_flags @@ -212,7 +212,7 @@ opt_parse() { value="${argv[$i]}" regex="${__opt_regex[$long]}" - if [[ -n "$regex" ]] && ! [[ "$value" =~ $regex ]]; then + if ! [[ "$value" =~ $regex ]]; then log_error "Value \"$value\" doesn't match \"$regex\"" return 1 fi @@ -222,13 +222,11 @@ opt_parse() { __opt_value["$long"]="$value" - if [[ -n "$action" ]]; then - "$action" "$long" "$value" - err="$?" + "$action" "$long" "$value" + err="$?" - if (( err != 0 )); then - return "$err" - fi + if (( err != 0 )); then + return "$err" fi done -- 2.47.3