aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authormerakor <cem@ckyln.com>2021-02-04 09:14:57 +0000
committermerakor <cem@ckyln.com>2021-02-04 09:14:57 +0000
commitd0281932ec6aa9dbc096b45b591c131581c25b06 (patch)
tree930e308e57fa181e1a67a110eb8e7f8f58ce6939 /src
parent2895af49f379cc79441a79d0a77ad07646f38769 (diff)
downloadcpt-d0281932ec6aa9dbc096b45b591c131581c25b06.tar.gz
getoptions: update to 2.5.0 of the library
FossilOrigin-Name: 536a4979e768c7b605a393251f0468a878e46a23b554045049b7404072b5258b
Diffstat (limited to 'src')
-rw-r--r--src/cpt-lib.in158
1 files changed, 84 insertions, 74 deletions
diff --git a/src/cpt-lib.in b/src/cpt-lib.in
index 9aa232c..1bcd8bc 100644
--- a/src/cpt-lib.in
+++ b/src/cpt-lib.in
@@ -113,17 +113,15 @@ _readlinkf() (
# This is the public domain getoptions shell library. It also forms a usage
# function.
-# URL: https://github.com/ko1nksm/getoptions (v2.0.1)
+# URL: https://github.com/ko1nksm/getoptions (v2.5.0)
# License: Creative Commons Zero v1.0 Universal
# shellcheck disable=2016
getoptions() {
_error='' _on=1 _off='' _export='' _plus='' _mode='' _alt='' _rest=''
- _opts='' _help='' _indent='' _init=@empty IFS=' '
+ _flags='' _nflags='' _opts='' _help='' _abbr='' _cmds='' _init=@empty IFS=' '
- for i in 0 1 2 3 4 5; do
- eval "_$i() { echo \"$_indent\$@\"; }"
- _indent="$_indent "
- done
+ _0() { echo "$@"; }
+ for i in 1 2 3 4 5; do eval "_$i() { _$((${i-}-1)) \" \$@\"; }"; done
quote() {
q="$2'" r=''
@@ -135,6 +133,8 @@ getoptions() {
[ "${1#:}" = "$1" ] && c=3 || c=4
eval "[ ! \${$c:+x} ] || $2 \"\$$c\""
}
+ kv() { eval "${2-}${1%%:*}=\${1#*:}"; }
+ loop() { [ $# -gt 1 ] && [ "$2" != -- ]; }
invoke() { eval '"_$@"'; }
prehook() { invoke "$@"; }
@@ -143,12 +143,12 @@ getoptions() {
done
args() {
- on=$_on off=$_off export=$_export init=$_init _hasarg=$1
- while [ $# -gt 2 ] && [ "$3" != '--' ] && shift; do
- case $2 in
- -?) [ "$_hasarg" ] || _opts="$_opts${2#-}" ;;
- +*) _plus=1 ;;
- [!-+]*) eval "${2%%:*}=\${2#*:}"
+ on=$_on off=$_off export=$_export init=$_init _hasarg=$1 && shift
+ while loop "$@" && shift; do
+ case $1 in
+ -?) [ "$_hasarg" ] && _opts="$_opts${1#-}" || _flags="$_flags${1#-}" ;;
+ +?) _plus=1 _nflags="$_nflags${1#+}" ;;
+ [!-+]*) kv "$1"
esac
done
}
@@ -166,35 +166,43 @@ getoptions() {
esac
}
_setup() {
- [ $# -gt 0 ] && { [ "$1" ] && _rest=$1; shift; }
- for i; do [ "$i" = '--' ] && break; eval "_${i%%:*}=\${i#*:}"; done
+ [ "${1#-}" ] && _rest=$1
+ while loop "$@" && shift; do kv "$1" _; done
}
- _flag() { args : "$@"; defvar "$@"; }
- _param() { args '' "$@"; defvar "$@"; }
- _option() { args '' "$@"; defvar "$@"; }
- _disp() { args : "$@"; }
- _msg() { args : _ "$@"; }
+ _flag() { args '' "$@"; defvar "$@"; }
+ _param() { args 1 "$@"; defvar "$@"; }
+ _option() { args 1 "$@"; defvar "$@"; }
+ _disp() { args '' "$@"; }
+ _msg() { args '' _ "$@"; }
+ cmd() { _mode=@ _cmds="$_cmds${_cmds:+|}'$1'"; }
"$@"
+ cmd() { :; }
_0 "${_rest:?}=''"
+ _0 "$2() {"
+ _1 'OPTIND=$(($#+1))'
+ _1 'while OPTARG= && [ $# -gt 0 ]; do'
+ [ "$_abbr" ] && getoptions_abbr "$@"
+
args() {
sw='' validate='' pattern='' counter='' on=$_on off=$_off export=$_export
- while [ $# -gt 1 ] && [ "$2" != '--' ] && shift; do
+ while loop "$@" && shift; do
case $1 in
- --\{no-\}*) sw="$sw${sw:+ | }--${1#--?no-?} | --no-${1#--?no-?}" ;;
- [-+]? | --*) sw="$sw${sw:+ | }$1" ;;
- *) eval "${1%%:*}=\"\${1#*:}\""
+ --\{no-\}*) i=${1#--?no-?}; sw="$sw${sw:+|}'--$i'|'--no-$i'" ;;
+ [-+]? | --*) sw="$sw${sw:+|}'$1'" ;;
+ *) kv "$1"
esac
done
+ quote on "$on"
+ quote off "$off"
}
setup() { :; }
_flag() {
args "$@"
- quote on "$on" && quote off "$off"
- [ "$counter" ] && on=1 off=-1 v="\$((\${$1:-0}+\${OPTARG:-0}))" || v=''
+ [ "$counter" ] && on=1 off=-1 v="\$((\${$1:-0}+\$OPTARG))" || v=''
_3 "$sw)"
- _4 '[ "${OPTARG:-}" ] && OPTARG=${OPTARG#*\=} && set -- noarg "$1" && break'
+ _4 '[ "${OPTARG:-}" ] && OPTARG=${OPTARG#*\=} && set "noarg" "$1" && break'
_4 "eval '[ \${OPTARG+x} ] &&:' && OPTARG=$on || OPTARG=$off"
valid "$1" "${v:-\$OPTARG}"
_4 ';;'
@@ -202,18 +210,17 @@ getoptions() {
_param() {
args "$@"
_3 "$sw)"
- _4 '[ $# -le 1 ] && set -- required "$1" && break'
+ _4 '[ $# -le 1 ] && set "required" "$1" && break'
_4 'OPTARG=$2'
valid "$1" '$OPTARG'
_4 'shift ;;'
}
_option() {
args "$@"
- quote on "$on" && quote off "$off"
_3 "$sw)"
_4 'set -- "$1" "$@"'
_4 '[ ${OPTARG+x} ] && {'
- _5 'case $1 in --no-*) set -- noarg "${1%%\=*}"; break; esac'
+ _5 'case $1 in --no-*) set "noarg" "${1%%\=*}"; break; esac'
_5 '[ "${OPTARG:-}" ] && { shift; OPTARG=$2; } ||' "OPTARG=$on"
_4 "} || OPTARG=$off"
valid "$1" '$OPTARG'
@@ -223,9 +230,8 @@ getoptions() {
set -- "$validate" "$pattern" "$1" "$2"
[ "$1" ] && _4 "$1 || { set -- ${1%% *}:\$? \"\$1\" $1; break; }"
[ "$2" ] && {
- quote pattern "$2"
_4 "case \$OPTARG in $2) ;;"
- _5 "*) set -- pattern:$pattern \"\$1\"; break"
+ _5 '*) set "pattern:'"$2"'" "$1"; break'
_4 "esac"
}
code "$3" _4 "${export:+export }$3=\"$4\"" "${3#:}"
@@ -238,102 +244,106 @@ getoptions() {
}
_msg() { :; }
- _0 "$2() {"
- _1 'OPTIND=$(($#+1))'
- _1 'while OPTARG= && [ $# -gt 0 ]; do'
[ "$_alt" ] && _2 'case $1 in -[!-]?*) set -- "-$@"; esac'
_2 'case $1 in'
- wa() { _4 "eval '${1% *}' \${1+'\"\$@\"'}"; }
+ _wa() { _4 "eval 'set -- $1' \${1+'\"\$@\"'}"; }
+ _op() {
+ _3 "$1) OPTARG=\$1; shift"
+ _wa '"${OPTARG%"${OPTARG#??}"}" '"$2"'"${OPTARG#??}"'
+ _4 "$3"
+ }
_3 '--?*=*) OPTARG=$1; shift'
- wa 'set -- "${OPTARG%%\=*}" "${OPTARG#*\=}" "$@"'
+ _wa '"${OPTARG%%\=*}" "${OPTARG#*\=}"'
_4 ';;'
_3 '--no-*) unset OPTARG ;;'
[ "$_alt" ] || {
- [ "$_opts" ] && {
- _3 "-[$_opts]?*) OPTARG=\$1; shift"
- wa 'set -- "${OPTARG%"${OPTARG#??}"}" "${OPTARG#??}" "$@"'
- _4 ';;'
- }
- _3 '-[!-]?*) OPTARG=$1; shift'
- wa 'set -- "${OPTARG%"${OPTARG#??}"}" "-${OPTARG#??}" "$@"'
- _4 'OPTARG= ;;'
+ [ "$_opts" ] && _op "-[$_opts]?*" '' ';;'
+ [ ! "$_flags" ] || _op "-[$_flags]?*" - 'OPTARG= ;;'
}
[ "$_plus" ] && {
- _3 '+??*) OPTARG=$1; shift'
- wa 'set -- "${OPTARG%"${OPTARG#??}"}" "+${OPTARG#??}" "$@"'
- _4 'unset OPTARG ;;'
+ [ "$_nflags" ] && _op "+[$_nflags]?*" + 'unset OPTARG ;;'
_3 '+*) unset OPTARG ;;'
}
_2 'esac'
_2 'case $1 in'
"$@"
rest() {
- _3 "$1"
_4 'while [ $# -gt 0 ]; do'
- _5 "$_rest=\"\${$_rest}" '\"\${$((${OPTIND:-0}-$#))}\""'
+ _5 "$_rest=\"\${$_rest}" '\"\${$(($OPTIND-$#))}\""'
_5 'shift'
_4 'done'
_4 'break ;;'
}
- rest '--) shift'
- _3 "[-${_plus:++}]?*)" 'set -- unknown "$1" && break ;;'
+ _3 '--)'
+ [ "$_mode" = @ ] || _4 'shift'
+ rest
+ _3 "[-${_plus:++}]?*)"
+ case $_mode in [=#]) rest ;; *) _4 'set "unknown" "$1"; break ;;'; esac
+ _3 '*)'
case $_mode in
- +) rest '*)' ;;
- *) _3 "*) $_rest=\"\${$_rest}" '\"\${$((${OPTIND:-0}-$#))}\""'
+ @)
+ _4 "case \$1 in ${_cmds:-*}) ;;"
+ _5 '*) set "notcmd" "$1"; break'
+ _4 'esac'
+ rest ;;
+ [+#]) rest ;;
+ *) _4 "$_rest=\"\${$_rest}" '\"\${$(($OPTIND-$#))}\""'
esac
_2 'esac'
_2 'shift'
_1 'done'
_1 '[ $# -eq 0 ] && { OPTIND=1; unset OPTARG; return 0; }'
_1 'case $1 in'
- _2 'unknown) set -- "Unrecognized option: $2" "$@" ;;'
- _2 'noarg) set -- "Does not allow an argument: $2" "$@" ;;'
- _2 'required) set -- "Requires an argument: $2" "$@" ;;'
- _2 'pattern:*) set -- "Does not match the pattern (${1#*:}): $2" "$@" ;;'
- _2 '*) set -- "Validation error ($1): $2" "$@"'
+ _2 'unknown) set "Unrecognized option: $2" "$@" ;;'
+ _2 'noarg) set "Does not allow an argument: $2" "$@" ;;'
+ _2 'required) set "Requires an argument: $2" "$@" ;;'
+ _2 'pattern:*) set "Does not match the pattern (${1#*:}): $2" "$@" ;;'
+ _2 'notcmd) set "Not a command: $2" "$@" ;;'
+ _2 '*) set "Validation error ($1): $2" "$@"'
_1 'esac'
[ "$_error" ] && _1 "$_error" '"$@" >&2 || exit $?'
_1 'echo "$1" >&2'
_1 'exit 1'
_0 '}'
- # This throws an error on older versions of shellcheck.
- # shellcheck disable=2086
[ ! "$_help" ] || eval "shift 2; getoptions_help $1 $_help" ${3+'"$@"'}
}
-# URL: https://github.com/ko1nksm/getoptions (v2.0.1)
+# URL: https://github.com/ko1nksm/getoptions (v2.5.0)
# License: Creative Commons Zero v1.0 Universal
getoptions_help() {
- width=30 plus='' leading=' '
+ _width='30,12' _plus='' _leading=' '
pad() { p=$2; while [ ${#p} -lt "$3" ]; do p="$p "; done; eval "$1=\$p"; }
+ kv() { eval "${2-}${1%%:*}=\${1#*:}"; }
+ sw() { pad sw "$sw${sw:+, }" "$1"; sw="$sw$2"; }
args() {
- _type=$1 var=${2%% *} sw='' label='' hidden='' _width=$width && shift 2
- while [ $# -gt 0 ] && i=$1 && shift && [ ! "$i" = '--' ]; do
+ _type=$1 var=${2%% *} sw='' label='' hidden='' && shift 2
+ while [ $# -gt 0 ] && i=$1 && shift && [ "$i" != -- ]; do
case $i in
- --*) pad sw "$sw${sw:+, }" $((${plus:+4}+4)); sw="$sw$i" ;;
- -?) sw="$sw${sw:+, }$i" ;;
- +?) [ ! "$plus" ] || { pad sw "$sw${sw:+, }" 4; sw="$sw$i"; } ;;
- *) eval "${i%%:*}=\${i#*:}"
+ --*) sw $((${_plus:+4}+4)) "$i" ;;
+ -?) sw 0 "$i" ;;
+ +?) [ ! "$_plus" ] || sw 4 "$i" ;;
+ *) [ "$_type" = setup ] && kv "$i" _; kv "$i"
esac
done
- [ "$hidden" ] && return 0
+ [ "$hidden" ] && return 0 || len=${_width%,*}
[ "$label" ] || case $_type in
- setup | msg) label='' _width=0 ;;
+ setup | msg) label='' len=0 ;;
flag | disp) label="$sw " ;;
param) label="$sw $var " ;;
option) label="${sw}[=$var] "
esac
- pad label "${label:+$leading}$label" "$_width"
- [ ${#label} -le "$_width" ] && [ $# -gt 0 ] && label="$label$1" && shift
+ [ "$_type" = cmd ] && label=${label:-$var } len=${_width#*,}
+ pad label "${label:+$_leading}$label" "$len"
+ [ ${#label} -le "$len" ] && [ $# -gt 0 ] && label="$label$1" && shift
echo "$label"
- pad label '' "$_width"
+ pad label '' "$len"
for i; do echo "$label$i"; done
}
- for i in 'setup :' flag param option disp 'msg :'; do
+ for i in setup flag param option disp 'msg -' cmd; do
eval "${i% *}() { args $i \"\$@\"; }"
done