]> git.corax.cc Git - toolbox/commitdiff
include/opt: Implement argument validation using regular expressions
authorMatthias Kruk <m@m10k.eu>
Mon, 21 Jun 2021 11:27:57 +0000 (20:27 +0900)
committerMatthias Kruk <m@m10k.eu>
Mon, 21 Jun 2021 11:27:57 +0000 (20:27 +0900)
The opt_parse() function does not implement validation of commandline
arguments, leaving it to the caller to sanitize values passed by the
user.
This commit adds an argument to opt_add_arg() that allows the caller
to specify a regular expression that will be used by opt_parse() to
validate user inputs.

include/opt.sh

index de86ea592aad3d6d3d513add691e1dc49c9552df..a4ac69e7815505bb8f823c7a24cb3ec2839df043 100644 (file)
@@ -30,6 +30,7 @@ __init() {
        declare -Axg __opt_flags
        declare -Axg __opt_value
        declare -Axg __opt_default
+       declare -Axg __opt_regex
        declare -Axg __opt_action
        declare -Axg __opt_map
        declare -xgi __opt_num=0
@@ -50,25 +51,19 @@ __init() {
 }
 
 opt_add_arg() {
-       local short
-       local long
-       local flags
-       local default
-       local desc
-       local action
+       local short="$1"
+       local long="$2"
+       local flags="$3"
+       local default="$4"
+       local desc="$5"
+       local regex="$6"
+       local action="$7"
 
        local optlen
        local num_flags
        local bflags
        local i
 
-       short="$1"
-       long="$2"
-       flags="$3"
-       default="$4"
-       desc="$5"
-       action="$6"
-
        if array_contains "$short" "${__opt_short[@]}" ||
           array_contains "$long" "${__opt_long[@]}"; then
                return 1
@@ -99,9 +94,9 @@ opt_add_arg() {
        __opt_long["$short"]="$long"
        __opt_flags["$long"]="$bflags"
        __opt_desc["$long"]="$desc"
-       __opt_default["$long"]="$default"
+       __opt_regex["$long"]="$regex"
        __opt_action["$long"]="$action"
-
+       __opt_default["$long"]="$default"
        __opt_map["-$short"]="$long"
        __opt_map["--$long"]="$long"
 
@@ -161,6 +156,7 @@ opt_parse() {
                local flags
                local value
                local action
+               local regex
 
                param="${!i}"
                long="${__opt_map[$param]}"
@@ -172,6 +168,7 @@ opt_parse() {
 
                flags="${__opt_flags[$long]}"
                action="${__opt_action[$long]}"
+               regex="${__opt_regex[$long]}"
 
                if (( flags & __opt_flag_has_value )); then
                        ((i++))
@@ -182,6 +179,11 @@ opt_parse() {
                        fi
 
                        value="${!i}"
+
+                       if [[ -n "$regex" ]] && ! [[ "$value" =~ $regex ]]; then
+                               log_error "Value \"$value\" doesn't match \"$regex\""
+                               return 1
+                       fi
                else
                        value="${__opt_value[$long]}"
                        ((value++))