diff options
author | Sevinj Aghayeva <sevinj.aghayeva@gmail.com> | 2022-06-09 12:29:10 -0400 |
---|---|---|
committer | Stefano Brivio <sbrivio@redhat.com> | 2022-06-14 15:44:48 +0200 |
commit | a38c40340b3af52b7536acf18bfde25c1a66b43c (patch) | |
tree | ef176882623caa12a0ec823a488621b966f2c106 | |
parent | 7c78f197cc42c38cae2654a02d760e371866b3f4 (diff) | |
download | mbuto-a38c40340b3af52b7536acf18bfde25c1a66b43c.tar mbuto-a38c40340b3af52b7536acf18bfde25c1a66b43c.tar.gz mbuto-a38c40340b3af52b7536acf18bfde25c1a66b43c.tar.bz2 mbuto-a38c40340b3af52b7536acf18bfde25c1a66b43c.tar.lz mbuto-a38c40340b3af52b7536acf18bfde25c1a66b43c.tar.xz mbuto-a38c40340b3af52b7536acf18bfde25c1a66b43c.tar.zst mbuto-a38c40340b3af52b7536acf18bfde25c1a66b43c.zip |
mbuto: Add support for suboptions with optional args
Signed-off-by: Sevinj Aghayeva <sevinj.aghayeva@gmail.com>
[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 <sbrivio@redhat.com>
-rwxr-xr-x | mbuto | 24 |
1 files changed, 14 insertions, 10 deletions
@@ -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 |