1 ### vim:ft=zsh:foldmethod=marker
2 ## parse cmdline options
3 ## Copyright: 2009, Frank Terbeck <ft@bewatermyfriend.org>
5 # This is lookup's option parsing slave.
6 # To use it, you'll have to follow the following idiom:
13 # LOOKUP_parseopts "$@" || return 1
15 # ...the result of LOOKUP_parseopts is now stored in $opts[] and $args[].
16 # $args[] is an array containing the remaining args from "$@" after parsing.
17 # $opts[] is an associative array containing key value pairs in the following
21 # For boolean args an entry is only added if the was given and will look
26 # If you define integer arguments in $lu_parseopts_args[], LOOKUP_parseopts()
27 # will check its value and return with an error if the argument is not a
29 # Hence, in the above example the following calls are valid:
36 LOOKUP_guard || return 1
38 local char o dashdash firstarg who
41 if [[ ${lookup_ei} == '-backend-' ]] ; then
47 if (( ${#lu_parseopts_args} == 0 )) ; then
48 printf '%s: $lu_parseopts_args is emtpy.\n' ${who}
49 printf '%s: Read the example on top of the LOOKUP_parseopts file.\n' ${who}
55 for o in ${(k)lu_parseopts_args} ; do
56 case ${lu_parseopts_args[$o]} in
58 (string|int) largs+=($o:) ;;
60 printf '%s: Unknown option type (%s: %s)!\n' ${who} $o ${lu_parseopts_args[$o]}
73 if [[ ${dashdash} == 'found' ]] ; then
78 zparseopts -A opts -D "${largs[@]}" || return 1
79 if [[ $1 == -* ]] && [[ $1 != ${firstarg} ]] ; then
80 printf '%s: Unknown option: %s\n' ${who} "$1"
83 for o in ${(k)opts} ; do
87 case ${lu_parseopts_args[$char]} in
89 [[ -z ${opts[$o]} ]] && opts[$o]='yes'
92 opts[$o]=${${opts[$o]}/(#s)+/}
93 if [[ ${${opts[$o]}/(#s)-/} == (-|)*[^0-9]* ]]; then
94 printf '%s: %s requires an integer argument.\n' ${who} $o