Add zsh-lookup sub-system
[grml-etc-core.git] / usr_share_grml / zsh / functions / Lookup / LOOKUP_parseopts
1 ### vim:ft=zsh:foldmethod=marker
2 ## parse cmdline options
3 ## Copyright: 2009, Frank Terbeck <ft@bewatermyfriend.org>
4
5 # This is lookup's option parsing slave.
6 # To use it, you'll have to follow the following idiom:
7 #   lu_parseopts_args=(
8 #       l   bool
9 #       h   bool
10 #       x   string
11 #       i   int
12 #   )
13 #   LOOKUP_parseopts "$@" || return 1
14 #
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
18 #   form:
19 #       -x  "a string"
20 #       -i  12345
21 #   For boolean args an entry is only added if the was given and will look
22 #   like this:
23 #       -l  "yes"
24 #       -h  "yes"
25 #
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
28 # signed integer.
29 # Hence, in the above example the following calls are valid:
30 #   foo -i 123
31 #   foo -i -123
32 #   foo -i +123
33 # while this is not:
34 #   foo -i bar
35
36 LOOKUP_guard || return 1
37
38 local char o dashdash firstarg who
39 local -a largs match
40
41 if [[ ${lookup_ei} == '-backend-' ]] ; then
42     who="${backend}"
43 else
44     who="lookup"
45 fi
46
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}
50     return 1
51 fi
52
53 args=()
54 opts=()
55 for o in ${(k)lu_parseopts_args} ; do
56     case ${lu_parseopts_args[$o]} in
57     (bool)       largs+=($o)  ;;
58     (string|int) largs+=($o:) ;;
59     (*)
60         printf '%s: Unknown option type (%s: %s)!\n' ${who} $o ${lu_parseopts_args[$o]}
61         return 1
62         ;;
63     esac
64 done
65
66 for o in "$@" ; do
67     case $o in
68     (-|--)
69         dashdash='found'
70         continue
71         ;;
72     esac
73     if [[ ${dashdash} == 'found' ]] ; then
74         firstarg="$o"
75         break
76     fi
77 done
78 zparseopts -A opts -D "${largs[@]}" || return 1
79 if [[ $1 == -* ]] && [[ $1 != ${firstarg} ]] ; then
80     printf '%s: Unknown option: %s\n' ${who} "$1"
81     return 1
82 fi
83 for o in ${(k)opts} ; do
84     match=()
85     : ${o/(#b)-(*)/}
86     char="${match[1]}"
87     case ${lu_parseopts_args[$char]} in
88     (bool)
89         [[ -z ${opts[$o]} ]] && opts[$o]='yes'
90         ;;
91     (int)
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
95             return 1
96         fi
97         ;;
98     esac
99 done
100
101 args=( "$@" )