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.
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
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
__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