diff options
author | Luke Shumaker <lukeshu@sbcglobal.net> | 2014-07-01 17:57:18 -0400 |
---|---|---|
committer | Luke Shumaker <lukeshu@sbcglobal.net> | 2014-07-01 17:57:18 -0400 |
commit | cd817482092bf63ee13f912d76023073ef48731c (patch) | |
tree | 59033528548ba9f79ae2bc65f75474bcc3d71822 /src | |
parent | 434dbea55773bbdcf903d22c08d6963c061ccdfb (diff) |
librefetch: use linux-util:getopt(1) to support getopt-flag-combining
Diffstat (limited to 'src')
-rwxr-xr-x | src/librefetch/librefetch | 86 |
1 files changed, 44 insertions, 42 deletions
diff --git a/src/librefetch/librefetch b/src/librefetch/librefetch index 8b02205..1d8b152 100755 --- a/src/librefetch/librefetch +++ b/src/librefetch/librefetch @@ -56,9 +56,6 @@ usage() { prose "Other options, if they are valid \`makepkg\` options, are passed straight to makepkg." echo - prose "%s does NOT support getopt-style flag combining. You must use - '-a -b', not '-ab'." "$cmd" - echo print "Example usage:" print ' $ %s https://repo.parabolagnulinux.org/other/mypackage/mypackage-1.0.tar.gz' "$cmd" echo @@ -196,54 +193,57 @@ main() { # sets the variables BUILDFILE, makepkg_opts, extra_opts, mode parse_options() { - # Detect makepkg options + declare -i ret=0 + local {shrt,long}{1,2} + + # makepkg options local makepkg_orig="$(which makepkg)" - # --long flags that take a second argument - local makepkg_opt2long=( $(LC_ALL=C "${makepkg_orig}" -h | sed -rn 's/\s*(--\S*) <.*/\1/p')) - # -s hort flags that take a second argument - local makepkg_opt2short=($(LC_ALL=C "${makepkg_orig}" -h | sed -rn 's/\s*(-.) <.*/\1/p')) - # all flags - local makepkg_argall=( $(LC_ALL=C "${makepkg_orig}" -h | sed -rn \ - -e 's/^ +(-.) .*/\1/p' \ - -e 's/^ +(-.), (--\S*) .*/\1\n\2/p' \ - -e 's/^ +(--\S*) .*/\1/p')) - - local opt - local have_opt + shrt1=($(LC_ALL=C "${makepkg_orig}" -h | sed -rn 's/^ +-(.)(,| [^<]).*/\1/p')) + shrt2=($(LC_ALL=C "${makepkg_orig}" -h | sed -rn 's/^ +-(.) <.*/\1/p')) + long1=($(LC_ALL=C "${makepkg_orig}" -h | sed -rn -e 's/^ +(-., )?--(\S*) [^<].*/\2/p')) + long2=($(LC_ALL=C "${makepkg_orig}" -h | sed -rn 's/^ +--(\S*) <.*/\1/p')) + + # librefetch options + shrt1+=(C D g S M h) + shrt2+=(p) + long1+=(geninteg srcbuild makepkg help) + long2+=() + + # Feed the options through getopt (sanitize them) + local shrt="$({ printf '%s\0' "${shrt1[@]}"; printf '%s:\0' "${shrt2[@]}"; } | sort -zu | xargs -0 printf '%s')" + local long="$({ printf '%s\0' "${long1[@]}"; printf '%s:\0' "${long2[@]}"; } | sort -zu | xargs -0 printf '%s,')" + local args="$(getopt -n "$cmd" -o "$shrt" -l "${long%,}" -- "$@")" + ret=$? + eval set -- "$args" + unset shrt long args + + # Parse the options. + local opt optarg have_optarg while [[ $# -gt 0 ]]; do - arg=$1 - have_opt=false - if in_array "${arg%%=*}" "${makepkg_opt2long[@]}"; then - opt="${arg#*=}" - arg="${arg%%=*}" - have_opt=true - fi - if in_array "${arg}" "${makepkg_opt2short[@]}"; then - shift - opt=$1 - have_opt=true + opt=$1; shift + have_optarg=false + + if { [[ $opt == --?* ]] && in_array "${opt#--}" "${long2[@]}"; } \ + || { [[ $opt == -? ]] && in_array "${opt#-}" "${shrt2[@]}"; } + then + optarg=$1; shift + have_optarg=true fi - case "$arg" in + + case "$opt" in -C) mode=create;; -D) mode=download;; -g|--geninteg) mode=checksums;; -S|--srcbuild) mode=srcbuild;; -M|--makepkg) mode=makepkg;; - -p) BUILDFILE="$(readlink -m -- "$opt")";; + -p) BUILDFILE="$(readlink -m -- "$optarg")";; -h|--help) mode=help;; - --) shift; break;; - -*) - if in_array "${arg}" "${makepkg_argall[@]}"; then - makepkg_opts+=("$arg") - $have_opt && makepkg_opts+=("$opt") - else - print '%s: invalid flag: %s' "$cmd" "$arg" - return 1 - fi + --) break;; + *) + makepkg_opts+=("$opt") + if $have_optarg; then makepkg_opts+=("$optarg"); fi ;; - *) extra_opts+=("$arg");; esac - shift done extra_opts+=("$@") @@ -254,16 +254,18 @@ parse_options() { checksums|srcbuild|makepkg) # don't take any extra arguments if [[ ${#extra_opts[@]} != 0 ]]; then print "%s: found extra non-flag arguments: %s" "$cmd" "${extra_opts[*]}" >&2 - return 1 + ret=1 fi ;; *download*|*create*) # take 1 or 2 extra arguments if [[ ${#extra_opts[@]} != 1 ]] && [[ ${#extra_opts[@]} != 2 ]]; then print "%s: %d non-flag arguments found, expected 1 or 2: %s" "$cmd" ${#extra_opts[@]} >&2 - return 1 + ret=1 fi ;; esac + + return $ret } # Modify makepkg ############################################################### |