Skip to content

Commit 16904d9

Browse files
committed
Make regex filter calling be via a call.
1 parent ee98da5 commit 16904d9

File tree

1 file changed

+9
-28
lines changed

1 file changed

+9
-28
lines changed

scripts/lib/bashy-core/arg-processor.sh

Lines changed: 9 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -671,10 +671,15 @@ function _argproc_filter-call {
671671
# evaluation much more straightforward.
672672
local definedFunc=1 filterCall='_argproc_filter-call:inner'
673673
if [[ ${filter} =~ ^/(.*)/$ ]]; then
674-
: #TODO
674+
filter="$(vals -- "${BASH_REMATCH[1]}")"
675+
eval "function ${filterCall} {
676+
local _argproc_regex=${filter}
677+
[[ \$1 =~ \${_argproc_regex} ]] && printf '%s\\n' \"\$1\"
678+
}"
675679
elif [[ ${filter} =~ ^\{(.*)\}$ ]]; then
680+
filter="${BASH_REMATCH[1]}"
676681
eval "function ${filterCall} {
677-
${BASH_REMATCH[1]}
682+
${filter}
678683
}"
679684
else
680685
definedFunc=0
@@ -706,16 +711,8 @@ function _argproc_handler-body {
706711
local varName="$4"
707712
local result=()
708713

709-
if [[ ${filter} =~ ^/(.*)/$ ]]; then
710-
# Add a call to perform the regex check on each argument.
711-
filter="${BASH_REMATCH[1]}"
712-
local desc="$(_argproc_arg-description "${specName}")"
713-
result+=("$(printf \
714-
'_argproc_regex-filter-check %q %q "$@" || return "$?"\n' \
715-
"${desc}" "${filter}"
716-
)")
717-
elif [[ ${filter} != '' ]]; then
718-
# Add a call to perform the filtering.
714+
if [[ ${filter} != '' ]]; then
715+
# Add a call to perform the filtering on all the arguments.
719716
local desc="$(_argproc_arg-description "${specName}")"
720717
result+=("$(printf '
721718
local _argproc_args
@@ -977,22 +974,6 @@ function _argproc_parse-spec {
977974
fi
978975
}
979976

980-
# Helper (called by code produced by `_argproc_handler-body`) which performs
981-
# a regex filter check.
982-
function _argproc_regex-filter-check {
983-
local desc="$1"
984-
local regex="$2"
985-
shift 2
986-
987-
local arg
988-
for arg in "$@"; do
989-
if [[ ! (${arg} =~ ${regex}) ]]; then
990-
error-msg "Invalid value for ${desc}: ${arg}"
991-
return 1
992-
fi
993-
done
994-
}
995-
996977
# Sets the description of the named argument based on its type. This function
997978
# will fail if an argument with the given name was already defined.
998979
function _argproc_set-arg-description {

0 commit comments

Comments
 (0)