diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/cpt-lib | 166 |
1 files changed, 85 insertions, 81 deletions
diff --git a/src/cpt-lib b/src/cpt-lib index bb7cc2d..ccba660 100644 --- a/src/cpt-lib +++ b/src/cpt-lib @@ -46,16 +46,16 @@ trap_set() { # This is the public domain getoptions shell library. It also forms a usage # function. -# URL: https://github.com/ko1nksm/getoptions (v1.1.0) +# URL: https://github.com/ko1nksm/getoptions (v2.0.1) # License: Creative Commons Zero v1.0 Universal # shellcheck disable=2016 getoptions() { - _error='' _on=1 _off='' _export='' _plus='' _mode='' _alt='' restargs='' - _opts='' _no='' _equal=1 indent='' IFS=' ' + _error='' _on=1 _off='' _export='' _plus='' _mode='' _alt='' _rest='' + _opts='' _help='' _indent='' _init=@empty IFS=' ' for i in 0 1 2 3 4 5; do - eval "_$i() { echo \"$indent\$*\"; }" - indent="$indent " + eval "_$i() { echo \"$_indent\$@\"; }" + _indent="$_indent " done quote() { @@ -64,27 +64,31 @@ getoptions() { q="'${r%????}'" && q=${q#\'\'} && q=${q%\'\'} eval "$1=\${q:-\"''\"}" } - code() { [ "${1#:}" = "$1" ] && c=3 || c=4 eval "[ ! \${$c:+x} ] || $2 \"\$$c\"" } + invoke() { eval '"_$@"'; } + prehook() { invoke "$@"; } + for i in setup flag param option disp msg; do + eval "$i() { prehook $i \"\$@\"; }" + done + args() { - on=$_on off=$_off export=$_export init='@empty' _hasarg=$1 + on=$_on off=$_off export=$_export init=$_init _hasarg=$1 while [ $# -gt 2 ] && [ "$3" != '--' ] && shift; do case $2 in - --no-* | --\{no-\}*) _no=1 ;; - -?) [ "${_hasarg#%}" ] || _opts="$_opts${2#-}" ;; + -?) [ "$_hasarg" ] || _opts="$_opts${2#-}" ;; +*) _plus=1 ;; [!-+]*) eval "${2%%:*}=\${2#*:}" esac done } - defvar() { case $init in @none) : ;; + @export) code "$1" _0 "export $1" ;; @empty) code "$1" _0 "${export:+export }$1=''" ;; @unset) code "$1" _0 "unset $1 ||:" "unset OPTARG ||:; ${1#:}" ;; *) @@ -94,80 +98,78 @@ getoptions() { code "$1" _0 "${export:+export }$1=$init" "OPTARG=$init; ${1#:}" esac } - - setup() { - restargs=$1 && shift + _setup() { + [ $# -gt 0 ] && { [ "$1" ] && _rest=$1; shift; } for i; do [ "$i" = '--' ] && break; eval "_${i%%:*}=\${i#*:}"; done } - flag() { args : "$@"; defvar "$@"; } - param() { args % "$@"; defvar "$@"; } - option() { args % "$@"; defvar "$@"; } - disp() { args : "$@"; } - msg() { args : _ "$@"; } + _flag() { args : "$@"; defvar "$@"; } + _param() { args '' "$@"; defvar "$@"; } + _option() { args '' "$@"; defvar "$@"; } + _disp() { args : "$@"; } + _msg() { args : _ "$@"; } "$@" - _0 "${restargs:?}=''" + _0 "${_rest:?}=''" args() { - sw='' on=$_on off=$_off validate='' pattern='' counter='' default='' + sw='' validate='' pattern='' counter='' on=$_on off=$_off export=$_export while [ $# -gt 1 ] && [ "$2" != '--' ] && shift; do case $1 in - --\{no-\}* ) sw="${sw}${sw:+ | }--${1#--?no-?} | --no-${1#--?no-?}" ;; - [-+]? | --*) sw="${sw}${sw:+ | }$1" ;; + --\{no-\}*) sw="$sw${sw:+ | }--${1#--?no-?} | --no-${1#--?no-?}" ;; + [-+]? | --*) sw="$sw${sw:+ | }$1" ;; *) eval "${1%%:*}=\"\${1#*:}\"" esac done } - setup() { :; } - flag() { + _flag() { args "$@" - code='$OPTARG' - [ "$counter" ] && on=1 off=-1 code="\$((\${$1:-0}+\${OPTARG:-0}))" quote on "$on" && quote off "$off" + [ "$counter" ] && on=1 off=-1 v="\$((\${$1:-0}+\${OPTARG:-0}))" || v='' _3 "$sw)" - _4 '[ "${OPTARG:-}" ] && set -- "$1" noarg && break' + _4 '[ "${OPTARG:-}" ] && OPTARG=${OPTARG#*\=} && set -- noarg "$1" && break' _4 "eval '[ \${OPTARG+x} ] &&:' && OPTARG=$on || OPTARG=$off" - valid "$1" "$code" + valid "$1" "${v:-\$OPTARG}" _4 ';;' } - param() { + _param() { args "$@" _3 "$sw)" - _4 '[ $# -le 1 ] && set -- "$1" required && break' + _4 '[ $# -le 1 ] && set -- required "$1" && break' _4 'OPTARG=$2' valid "$1" '$OPTARG' _4 'shift ;;' } - option() { + _option() { args "$@" - quote default "$default" + quote on "$on" && quote off "$off" _3 "$sw)" - _4 'case $OPTARG in' - _5 '?*) OPTARG=$2 ;;' - _5 " *) OPTARG=$default &&" 'set -- "$1" "$@"' - _4 "esac" + _4 'set -- "$1" "$@"' + _4 '[ ${OPTARG+x} ] && {' + _5 'case $1 in --no-*) set -- noarg "${1%%\=*}"; break; esac' + _5 '[ "${OPTARG:-}" ] && { shift; OPTARG=$2; } ||' "OPTARG=$on" + _4 "} || OPTARG=$off" valid "$1" '$OPTARG' _4 'shift ;;' } valid() { - set -- "$validate" "$pattern" "$@" + set -- "$validate" "$pattern" "$1" "$2" + [ "$1" ] && _4 "$1 || { set -- ${1%% *}:\$? \"\$1\" $1; break; }" [ "$2" ] && { quote pattern "$2" _4 "case \$OPTARG in $2) ;;" - _5 "*) set -- \"\$1\" pattern $pattern; break" + _5 "*) set -- pattern:$pattern \"\$1\"; break" _4 "esac" } - [ "$1" ] && _4 "$1 || { set -- \"\$1\" $1; break; }" - code "$3" _4 "$3=$4" "${3#:}" + code "$3" _4 "${export:+export }$3=\"$4\"" "${3#:}" } - disp() { + _disp() { args "$@" _3 "$sw)" code "$1" _4 "echo \"\${$1}\"" "${1#:}" _4 'exit 0 ;;' } - msg() { :; } + _msg() { :; } _0 "$2() {" _1 'OPTIND=$(($#+1))' @@ -175,12 +177,10 @@ getoptions() { [ "$_alt" ] && _2 'case $1 in -[!-]?*) set -- "-$@"; esac' _2 'case $1 in' wa() { _4 "eval '${1% *}' \${1+'\"\$@\"'}"; } - [ "$_equal" ] && { - _3 '--?*=*) OPTARG=$1; shift' - wa 'set -- "${OPTARG%%\=*}" "${OPTARG#*\=}" "$@"' - _4 ';;' - } - [ "$_no" ] && _3 '--no-*) unset OPTARG ;;' + _3 '--?*=*) OPTARG=$1; shift' + wa 'set -- "${OPTARG%%\=*}" "${OPTARG#*\=}" "$@"' + _4 ';;' + _3 '--no-*) unset OPTARG ;;' [ "$_alt" ] || { [ "$_opts" ] && { _3 "-[$_opts]?*) OPTARG=\$1; shift" @@ -200,74 +200,78 @@ getoptions() { _2 'esac' _2 'case $1 in' "$@" - restargs() { + rest() { _3 "$1" _4 'while [ $# -gt 0 ]; do' - _5 "$restargs=\"\${$restargs}" '\"\${$((${OPTIND:-0}-$#))}\""' + _5 "$_rest=\"\${$_rest}" '\"\${$((${OPTIND:-0}-$#))}\""' _5 'shift' _4 'done' _4 'break ;;' } - restargs '--) shift' - _3 "[-${_plus:++}]?*)" 'set -- "$1" unknown && break ;;' + rest '--) shift' + _3 "[-${_plus:++}]?*)" 'set -- unknown "$1" && break ;;' case $_mode in - +) restargs '*)' ;; - *) _3 "*) $restargs=\"\${$restargs}" '\"\${$((${OPTIND:-0}-$#))}\""' + +) rest '*)' ;; + *) _3 "*) $_rest=\"\${$_rest}" '\"\${$((${OPTIND:-0}-$#))}\""' esac _2 'esac' _2 'shift' _1 'done' _1 '[ $# -eq 0 ] && { OPTIND=1; unset OPTARG; return 0; }' - [ "$_error" ] && _1 "$_error" '"$@" >&2 && exit 1' - _1 'case $2 in' - _2 "unknown) echo \"unrecognized option '\$1'\" ;;" - _2 "noarg) echo \"option '\$1' doesn't allow an argument\" ;;" - _2 "required) echo \"option '\$1' requires an argument\" ;;" - _2 "pattern) echo \"option '\$1' does not match the pattern (\$3)\" ;;" - _2 "*) echo \"option '\$1' validation error: \$2\"" - _1 'esac >&2' + _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" "$@"' + _1 'esac' + [ "$_error" ] && _1 "$_error" '"$@" >&2 || exit $?' + _1 'echo "$1" >&2' _1 'exit 1' _0 '}' + + [ ! "$_help" ] || eval "shift 2; getoptions_help $1 $_help" ${3+'"$@"'} } -# URL: https://github.com/ko1nksm/getoptions (v1.1.0) +# URL: https://github.com/ko1nksm/getoptions (v2.0.1) # License: Creative Commons Zero v1.0 Universal getoptions_help() { - width=30 plus='' here='GETOPTIONS-HEREDOC' + width=30 plus='' leading=' ' pad() { p=$2; while [ ${#p} -lt "$3" ]; do p="$p "; done; eval "$1=\$p"; } args() { - _type=$1 var=$2 sw='' hidden='' _width=$width _pre='' && shift 2 + _type=$1 var=${2%% *} sw='' label='' hidden='' _width=$width && 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" ;; - +? ) pad sw "$sw${sw:+, }" 4; sw="$sw$i" ;; - * ) eval "${i%%:*}=\"\${i#*:}\"" ;; + --*) 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#*:}" esac done [ "$hidden" ] && return 0 - case $_type in - setup | msg) _pre='' _width=0 ;; - flag | disp) pad _pre " $sw " "$_width" ;; - param ) pad _pre " $sw $var " "$_width" ;; - option ) pad _pre " $sw [$var] " "$_width" ;; + [ "$label" ] || case $_type in + setup | msg) label='' _width=0 ;; + flag | disp) label="$sw " ;; + param) label="$sw $var " ;; + option) label="${sw}[=$var] " esac - [ ${#_pre} -le "$_width" ] && [ $# -gt 0 ] && _pre="$_pre$1" && shift - echo "$_pre" - pad _pre '' "$_width" - for i; do echo "$_pre$i"; done + pad label "${label:+$leading}$label" "$_width" + [ ${#label} -le "$_width" ] && [ $# -gt 0 ] && label="$label$1" && shift + echo "$label" + pad label '' "$_width" + 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 :'; do eval "${i% *}() { args $i \"\$@\"; }" done echo "$2() {" - echo "cat<<$here" + echo "cat<<'GETOPTIONSHERE'" "$@" - echo "$here" + echo "GETOPTIONSHERE" echo "}" } |