From a38c40340b3af52b7536acf18bfde25c1a66b43c Mon Sep 17 00:00:00 2001 From: Sevinj Aghayeva Date: Thu, 9 Jun 2022 12:29:10 -0400 Subject: mbuto: Add support for suboptions with optional args Signed-off-by: Sevinj Aghayeva [sbrivio: OPTIND is reset on function return on dash and zsh: don't change it directly from subopts_get(), rather in the main getopts loop] Signed-off-by: Stefano Brivio --- mbuto | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/mbuto b/mbuto index dbe0d5c..a5733e6 100755 --- a/mbuto +++ b/mbuto @@ -456,8 +456,8 @@ list_join() { ### Suboption Parsing ######################################################### SUBOPTS=' - kselftests C collection Select a collection of kernel tests - kselftests T test Select a test from a collection + kselftests C collection 2 Select a collection of kernel tests + kselftests T test 2 Select a test from a collection ' subopts_profile= @@ -469,7 +469,7 @@ subopts_profile= subopts_usage_one() { __profile="${1}" __short="${2}" - __help="${4}" + __help="${5}" if [ "${usage_subopts_profile}" != ${__profile} ]; then usage_subopts_profile="${__profile}" @@ -511,21 +511,24 @@ subopts_get_one() { eval $(echo SUBOPT_${__name}=\"${__value}\") } -# subopts_get() - Set sub-option values, validating against SUBOPTS +# subopts_get() - Parse sub-options, validating against SUBOPTS # $1: Short option name as passed on command line # $2: Value passed on command line -# Return: 0 on success, 1 if option doesn't match known ones +# Return: argument count including option switch, doesn't return on failure subopts_get() { IFS=' ' for __line in ${SUBOPTS}; do IFS=' ' - subopts_get_one "${1}" "${2}" ${__line} && unset IFS && return 0 + if subopts_get_one "${1}" "${2}" ${__line}; then + unset IFS + return $(echo "${__line}" | "${AWK}" '{print $4}') + fi + IFS=' ' done - unset IFS - return 1 + usage } ### CPIO ####################################################################### @@ -1213,8 +1216,9 @@ while getopts :c:df:k:m:p:s:vh __opt; do v) VERBOSE="y" ;; ?) eval arg=\${$((OPTIND))} - OPTIND=$((OPTIND + 1)) - subopts_get "${OPTARG}" "${arg}" || usage ;; + subopts_get "${OPTARG}" "${arg}" || __subopt_args=$? + OPTIND=$((OPTIND + ${__subopt_args} - 1)) + ;; h|*) usage ;; esac done -- cgit v1.2.3