11# shellcheck shell=sh disable=SC2016
22# [getoptions] License: Creative Commons Zero v1.0 Universal
33getoptions () {
4- _error=' ' _on=1 _off =' ' _export=' ' _plus=' ' _mode=' ' _alt=' ' _rest=' '
4+ _error=' ' _on=1 _no =' ' _export=' ' _plus=' ' _mode=' ' _alt=' ' _rest= ' ' _def =' '
55 _flags=' ' _nflags=' ' _opts=' ' _help=' ' _abbr=' ' _cmds=' ' _init=@empty IFS=' '
6+ [ $# -lt 2 ] && set -- " ${1:? No parser definition} " -
7+ [ " $2 " = - ] && _def=getoptions_parse
68
79 _0 () { echo " $@ " ; }
810 for i in 1 2 3 4 5; do eval " _$i () { _$(( ${i-} - 1 )) \" \$ @\" ; }" ; done
@@ -17,6 +19,7 @@ getoptions() {
1719 [ " ${1#: } " = " $1 " ] && c=3 || c=4
1820 eval " [ ! \$ {$c :+x} ] || $2 \"\$ $c \" "
1921 }
22+ sw () { sw=" $sw ${sw: +|} $1 " ; }
2023 kv () { eval " ${2-}${1%%:* } =\$ {1#*:}" ; }
2124 loop () { [ $# -gt 1 ] && [ " $2 " != -- ]; }
2225
@@ -27,7 +30,7 @@ getoptions() {
2730 done
2831
2932 args () {
30- on=$_on off= $_off export=$_export init=$_init _hasarg=$1 && shift
33+ on=$_on no= $_no export=$_export init=$_init _hasarg=$1 && shift
3134 while loop " $@ " && shift ; do
3235 case $1 in
3336 -? ) [ " $_hasarg " ] && _opts=" $_opts ${1# -} " || _flags=" $_flags ${1# -} " ;;
@@ -64,30 +67,31 @@ getoptions() {
6467 cmd () { : ; }
6568 _0 " ${_rest:? } =''"
6669
67- _0 " $2 () {"
70+ _0 " ${_def :- $2 } () {"
6871 _1 ' OPTIND=$(($#+1))'
6972 _1 ' while OPTARG= && [ $# -gt 0 ]; do'
7073 [ " $_abbr " ] && getoptions_abbr " $@ "
7174
7275 args () {
73- sw=' ' validate=' ' pattern=' ' counter=' ' on=$_on off= $_off export=$_export
76+ sw=' ' validate=' ' pattern=' ' counter=' ' on=$_on no= $_no export=$_export
7477 while loop " $@ " && shift ; do
7578 case $1 in
76- --\{ no-\} * ) i=${1# --?no-?} ; sw=" $sw ${sw: +|} '--$i '|'--no-$i '" ;;
77- [-+]? | --* ) sw=" $sw ${sw: +|} '$1 '" ;;
79+ --\{ no-\} * ) i=${1# --?no-?} ; sw " '--$i '|'--no-$i '" ;;
80+ --with\{ out\} -* ) i=${1# --* -} ; sw " '--with-$i '|'--without-$i '" ;;
81+ [-+]? | --* ) sw " '$1 '" ;;
7882 * ) kv " $1 "
7983 esac
8084 done
8185 quote on " $on "
82- quote off " $off "
86+ quote no " $no "
8387 }
8488 setup () { : ; }
8589 _flag () {
8690 args " $@ "
87- [ " $counter " ] && on=1 off =-1 v=" \$ ((\$ {$1 :-0}+\$ OPTARG))" || v=' '
91+ [ " $counter " ] && on=1 no =-1 v=" \$ ((\$ {$1 :-0}+\$ OPTARG))" || v=' '
8892 _3 " $sw )"
8993 _4 ' [ "${OPTARG:-}" ] && OPTARG=${OPTARG#*\=} && set "noarg" "$1" && break'
90- _4 " eval '[ \$ {OPTARG+x} ] &&:' && OPTARG=$on || OPTARG=$off "
94+ _4 " eval '[ \$ {OPTARG+x} ] &&:' && OPTARG=$on || OPTARG=$no "
9195 valid " $1 " " ${v:- \$ OPTARG} "
9296 _4 ' ;;'
9397 }
@@ -104,9 +108,9 @@ getoptions() {
104108 _3 " $sw )"
105109 _4 ' set -- "$1" "$@"'
106110 _4 ' [ ${OPTARG+x} ] && {'
107- _5 ' case $1 in --no-*) set "noarg" "${1%%\=*}"; break; esac'
111+ _5 ' case $1 in --no-*|--without-* ) set "noarg" "${1%%\=*}"; break; esac'
108112 _5 ' [ "${OPTARG:-}" ] && { shift; OPTARG=$2; } ||' " OPTARG=$on "
109- _4 " } || OPTARG=$off "
113+ _4 " } || OPTARG=$no "
110114 valid " $1 " ' $OPTARG'
111115 _4 ' shift ;;'
112116 }
@@ -139,7 +143,7 @@ getoptions() {
139143 _3 ' --?*=*) OPTARG=$1; shift'
140144 _wa ' "${OPTARG%%\=*}" "${OPTARG#*\=}"'
141145 _4 ' ;;'
142- _3 ' --no-*) unset OPTARG ;;'
146+ _3 ' --no-*|--without-* ) unset OPTARG ;;'
143147 [ " $_alt " ] || {
144148 [ " $_opts " ] && _op " -[$_opts ]?*" ' ' ' ;;'
145149 [ ! " $_flags " ] || _op " -[$_flags ]?*" - ' OPTARG= ;;'
@@ -161,16 +165,15 @@ getoptions() {
161165 _3 ' --)'
162166 [ " $_mode " = @ ] || _4 ' shift'
163167 rest
164- _3 " [-${_plus: ++} ]?*)"
165- case $_mode in [=#]) rest ;; * ) _4 ' set "unknown" "$1"; break ;;' ; esac
168+ _3 " [-${_plus: ++} ]?*)" ' set "unknown" "$1"; break ;;'
166169 _3 ' *)'
167170 case $_mode in
168171 @)
169172 _4 " case \$ 1 in ${_cmds:-* } ) ;;"
170173 _5 ' *) set "notcmd" "$1"; break'
171174 _4 ' esac'
172175 rest ;;
173- [+#] ) rest ;;
176+ + ) rest ;;
174177 * ) _4 " $_rest =\"\$ {$_rest }" ' \"\${$(($OPTIND-$#))}\""'
175178 esac
176179 _2 ' esac'
@@ -190,5 +193,7 @@ getoptions() {
190193 _1 ' exit 1'
191194 _0 ' }'
192195
193- [ ! " $_help " ] || eval " shift 2; getoptions_help $1 $_help " ${3+' "$@"' }
196+ [ " $_help " ] && eval " shift 2; getoptions_help $1 $_help " ${3+' "$@"' }
197+ [ " $_def " ] && _0 " eval $_def \$ {1+'\"\$ @\" '}; eval set -- \"\$ {$_rest }\" "
198+ _0 ' # Do not execute' # exit 1
194199}
0 commit comments