3 local opt
= optarg
= i
= shortopts
=$1
4 local -a longopts
=() unused_argv
=()
7 while [[ $1 && $1 != '--' ]]; do
15 for o
in "${longopts[@]}"; do
16 if [[ ${o%:} = "$1" ]]; then
20 [[ ${o%:} = "$1"* ]] && longmatch
+=("$o")
23 case ${#longmatch[*]} in
25 # success, override with opt and return arg req (0 == none, 1 == required)
27 if [[ $longmatch = *: ]]; then
33 # fail, no match found
36 # fail, ambiguous match
37 printf "@SCRIPTNAME@: $(gettext "option
'%s' is ambiguous
; possibilities
:")" "--$1"
38 printf " '%s'" "${longmatch[@]%:}"
46 --) # explicit end of options
50 -[!-]*) # short option
51 for (( i
= 1; i
< ${#1}; i
++ )); do
54 # option doesn't exist
55 if [[ $shortopts != *$opt* ]]; then
56 printf "@SCRIPTNAME@: $(gettext "invalid option
") -- '%s'\n" "$opt" >&2
62 # option requires optarg
63 if [[ $shortopts = *$opt:* ]]; then
64 # if we're not at the end of the option chunk, the rest is the optarg
65 if (( i
< ${#1} - 1 )); then
68 # if we're at the end, grab the the next positional, if it exists
69 elif (( i
== ${#1} - 1 )) && [[ $2 ]]; then
75 printf "@SCRIPTNAME@: $(gettext "option requires an argument
") -- '%s'\n" "$opt" >&2
82 --?
*=*|
--?
*) # long option
83 IFS
='=' read -r opt optarg
<<< "${1#--}"
88 if [[ $optarg ]]; then
89 printf "@SCRIPTNAME@: $(gettext "option
'%s' does not allow an argument
")\n" "--$opt" >&2
101 if [[ $optarg ]]; then
102 OPTRET
+=("--$opt" "$optarg")
106 OPTRET
+=("--$opt" "$2" )
110 printf "@SCRIPTNAME@: $(gettext "option
'%s' requires an argument
")\n" "--$opt" >&2
117 # ambiguous option -- error was reported for us by longoptmatch()
123 printf "@SCRIPTNAME@: $(gettext "invalid option
") '--%s'\n" "$opt" >&2
129 *) # non-option arg encountered, add it as a parameter
136 # add end-of-opt terminator and any leftover positional parameters
137 OPTRET
+=('--' "${unused_argv[@]}" "$@")