Debian Codegraph @

misc/helpers.zsh

raw
1 #!/usr/bin/zsh
2
3 zsh_query() {
4 # ===========================================================================
5 #
6 # This function is for querying the command line.
7 #
8 # COMMANDS
9 # setup <OPTIONS> <ARGS...>
10 # This is the first call you have to make, otherwise the other commands
11 # won't (successfully) work.
12 #
13 # It parses <ARGS> according to <OPTIONS> and stores results in the
14 # variables POSITIONALS, HAVING_OPTIONS and OPTION_VALUES.
15 #
16 # The first argument is a comma-separated list of options that the parser
17 # should know about. Short options (-o), long options (--option), and
18 # old-style options (-option) are supported.
19 #
20 # If an option takes an argument, it is suffixed by '='.
21 # If an option takes an optional argument, it is suffixed by '=?'.
22 #
23 # get_positional <NUM>
24 # Prints out the positional argument number NUM (starting from 1)
25 #
26 # has_option [WITH_INCOMPLETE] <OPTIONS...>
27 # Checks if an option given in OPTIONS is passed on commandline.
28 # If an option requires an argument, this command returns true only if the
29 # option includes an argument. If 'WITH_INCOMPLETE' is specified, it also
30 # returns true for options missing their arguments.
31 #
32 # option_is <OPTIONS...> -- <VALUES...>
33 # Checks if one option in OPTIONS has a value of VALUES.
34 #
35 # EXAMPLE
36 # local POSITIONALS HAVING_OPTIONS OPTION_VALUES
37 # zsh_query setup '-f,-a=,-optional=?' program_name -f -optional -a foo bar
38 # zsh_query has_option -f
39 # zsh_query option_is -a -- foo
40 #
41 # Here, -f is a flag, -a takes an argument, and -optional takes an optional
42 # argument.
43 #
44 # Both queries return true.
45 #
46 # ===========================================================================
47
48 __zsh_query_contains() {
49 local arg='' key="$1"; shift
50 for arg; do [[ "$key" == "$arg" ]] && return 0; done
51 return 1
52 }
53
54 if [[ $# == 0 ]]; then
55 echo "%FUNCNAME%: missing command" >&2
56 return 1;
57 fi
58
59 local cmd="$1"
60 shift
61
62 case "$cmd" in
63 get_positional)
64 if test $# -ne 1; then
65 echo "%FUNCNAME%: get_positional: takes exactly one argument" >&2
66 return 1;
67 fi
68
69 if test "$1" -eq 0; then
70 echo "%FUNCNAME%: get_positional: positionals start at 1, not 0!" >&2
71 return 1
72 fi
73
74 printf "%s" "${POSITIONALS[$1]}"
75 return 0
76 ;;
77 has_option)
78 #ifdef with_incomplete
79 local with_incomplete=0
80 [[ "$1" == "WITH_INCOMPLETE" ]] && { with_incomplete=1; shift; }
81
82 #endif
83 if test $# -eq 0; then
84 echo "%FUNCNAME%: has_option: arguments required" >&2
85 return 1;
86 fi
87
88 local option=''
89 for option in "${HAVING_OPTIONS[@]}"; do
90 __zsh_query_contains "$option" "$@" && return 0
91 done
92
93 #ifdef with_incomplete
94 (( with_incomplete )) && \
95 __zsh_query_contains "$INCOMPLETE_OPTION" "$@" && return 0
96
97 #endif
98 return 1
99 ;;
100 option_is)
101 local -a cmd_option_is_options cmd_option_is_values
102 local end_of_options_num=0
103
104 while test $# -ge 1; do
105 if [[ "$1" == "--" ]]; then
106 (( ++end_of_options_num ))
107 elif test $end_of_options_num -eq 0; then
108 cmd_option_is_options+=("$1")
109 elif test $end_of_options_num -eq 1; then
110 cmd_option_is_values+=("$1")
111 fi
112
113 shift
114 done
115
116 if test ${#cmd_option_is_options[@]} -eq 0; then
117 echo "%FUNCNAME%: option_is: missing options" >&2
118 return 1
119 fi
120
121 if test ${#cmd_option_is_values[@]} -eq 0; then
122 echo "%FUNCNAME%: option_is: missing values" >&2
123 return 1
124 fi
125
126 local I=${#HAVING_OPTIONS[@]}
127 while test $I -ge 1; do
128 local option="${HAVING_OPTIONS[$I]}"
129 if __zsh_query_contains "$option" "${cmd_option_is_options[@]}"; then
130 local VALUE="${OPTION_VALUES[$I]}"
131 __zsh_query_contains "$VALUE" "${cmd_option_is_values[@]}" && return 0
132 fi
133
134 (( --I ))
135 done
136
137 return 1
138 ;;
139 setup)
140 local IFS=','
141 local -a options=(${=1})
142 unset IFS
143 shift
144 ;;
145 *)
146 echo "%FUNCNAME%: argv[1]: invalid command" >&2
147 return 1
148 ;;
149 esac
150
151 # continuing setup....
152
153 # ===========================================================================
154 # Parsing of available options
155 # ===========================================================================
156
157 #ifdef old_options
158 local -a old_opts_with_arg=() old_opts_with_optional_arg=() old_opts_without_arg=()
159 #endif
160 #ifdef long_options
161 local -a long_opts_with_arg=() long_opts_with_optional_arg=() long_opts_without_arg=()
162 #endif
163 #ifdef short_options
164 local -a short_opts_with_arg=() short_opts_with_optional_arg=() short_opts_without_arg=()
165 #endif
166
167 local option=''
168 for option in "${options[@]}"; do
169 case "$option" in
170 #ifdef long_options
171 --?*=) long_opts_with_arg+=("${option%=}");;
172 --?*=\?) long_opts_with_optional_arg+=("${option%=?}");;
173 --?*) long_opts_without_arg+=("$option");;
174 #endif
175 #ifdef short_options
176 -?=) short_opts_with_arg+=("${option%=}");;
177 -?=\?) short_opts_with_optional_arg+=("${option%=?}");;
178 -?) short_opts_without_arg+=("$option");;
179 #endif
180 #ifdef old_options
181 -??*=) old_opts_with_arg+=("${option%=}");;
182 -??*=\?) old_opts_with_optional_arg+=("${option%=?}");;
183 -??*) old_opts_without_arg+=("$option");;
184 #endif
185 *) echo "%FUNCNAME%: $option: not a valid short, long or oldstyle option" >&2; return 1;;
186 esac
187 done
188
189 # ===========================================================================
190 # Parsing of command line options
191 # ===========================================================================
192
193 POSITIONALS=()
194 HAVING_OPTIONS=()
195 OPTION_VALUES=()
196 INCOMPLETE_OPTION=''
197
198 local argi=2 # argi[1] is program name
199 while [[ $argi -le $# ]]; do
200 local arg="${@[$argi]}"
201 local have_trailing_arg=$(test $argi -lt $# && echo true || echo false)
202
203 case "$arg" in
204 -)
205 POSITIONALS+=(-);;
206 --)
207 for argi in $(seq $((argi + 1)) $#); do
208 POSITIONALS+=("${@[$argi]}")
209 done
210 break;;
211 --*=*)
212 HAVING_OPTIONS+=("${arg%%=*}")
213 OPTION_VALUES+=("${arg#*=}");;
214 --*)
215 #ifdef long_options
216 if __zsh_query_contains "$arg" "${long_opts_with_arg[@]}"; then
217 if $have_trailing_arg; then
218 HAVING_OPTIONS+=("$arg")
219 OPTION_VALUES+=("${@[$((argi + 1))]}")
220 (( argi++ ))
221 #ifdef with_incomplete
222 else
223 INCOMPLETE_OPTION="$arg"
224 #endif
225 fi
226 else
227 HAVING_OPTIONS+=("$arg")
228 OPTION_VALUES+=("")
229 fi
230 #endif
231 ;;
232 -*)
233 local end_of_parsing=false
234
235 #ifdef old_options
236 if [[ "$arg" == *=* ]]; then
237 local option="${arg%%=*}"
238 local value="${arg#*=}"
239 if __zsh_query_contains "$option" "${old_opts_with_arg[@]}" "${old_opts_with_optional_arg[@]}"; then
240 HAVING_OPTIONS+=("$option")
241 OPTION_VALUES+=("$value")
242 end_of_parsing=true
243 fi
244 elif __zsh_query_contains "$arg" "${old_opts_with_arg[@]}"; then
245 end_of_parsing=true
246 if $have_trailing_arg; then
247 HAVING_OPTIONS+=("$arg")
248 OPTION_VALUES+=("${@[$((argi + 1))]}")
249 (( argi++ ))
250 #ifdef with_incomplete
251 else
252 INCOMPLETE_OPTION="$arg"
253 #endif
254 fi
255 elif __zsh_query_contains "$arg" "${old_opts_without_arg[@]}" "${old_opts_with_optional_arg[@]}"; then
256 HAVING_OPTIONS+=("$arg")
257 OPTION_VALUES+=("")
258 end_of_parsing=true
259 fi
260 #endif
261
262 #ifdef short_options
263 local arg_length=${#arg}
264 local i=1
265 while ! $end_of_parsing && test $i -lt $arg_length; do
266 local option="-${arg:$i:1}"
267 local trailing_chars="${arg:$((i+1))}"
268
269 if __zsh_query_contains "$option" "${short_opts_without_arg[@]}"; then
270 HAVING_OPTIONS+=("$option")
271 OPTION_VALUES+=("")
272 elif __zsh_query_contains "$option" "${short_opts_with_arg[@]}"; then
273 end_of_parsing=true
274
275 if [[ -n "$trailing_chars" ]]; then
276 HAVING_OPTIONS+=("$option")
277 OPTION_VALUES+=("$trailing_chars")
278 elif $have_trailing_arg; then
279 HAVING_OPTIONS+=("$option")
280 OPTION_VALUES+=("${@[$((argi + 1))]}")
281 (( argi++ ))
282 #ifdef with_incomplete
283 else
284 INCOMPLETE_OPTION="$option"
285 #endif
286 fi
287 elif __zsh_query_contains "$option" "${short_opts_with_optional_arg[@]}"; then
288 end_of_parsing=true
289 HAVING_OPTIONS+=("$option")
290 OPTION_VALUES+=("$trailing_chars") # may be empty
291 fi
292
293 (( i++ ))
294 done
295 #endif
296 ;;
297 *)
298 POSITIONALS+=("$arg");;
299 esac
300
301 (( argi++ ))
302 done
303 }
304
305 _exec() {
306 local -a describe=()
307 local item='' desc=''
308
309 while IFS=$'\t' read -r item desc; do
310 item="${item/:/\\:/}"
311 describe+=("$item:$desc")
312 done < <(eval "$1")
313
314 _describe '' describe
315 }
316
317 _has_hidden_option() {
318 local option=''
319
320 for option; do
321 case "$option" in
322 -?) option="${option:1:1}"
323 [[ "${words[-1]}" != --* ]] && [[ "${words[-1]}" == -*$option* ]] && return 0
324 [[ "${words[-2]}" != --* ]] && [[ "${words[-2]}" == -*$option ]] && return 0;;
325 *)
326 [[ "${words[-1]}" == $option* ]] && return 0
327 [[ "${words[-2]}" == $option ]] && return 0;;
328 esac
329 done
330
331 return 1
332 }
333
334 # =============================================================================
335 # TEST CODE
336 # =============================================================================
337
338 if [[ $# == 1 ]] && [[ "$1" == "-q" ]]; then
339 echo() {}
340 fi
341
342 test_case() {
343 local TEST_NUMBER="$1"; shift
344 local EXPECTED="$1"; shift
345 local -a TEST_ARGS=() SETUP_ARGS=()
346
347 while test $# -ge 1; do
348 if [[ "$1" == ":SETUP:" ]]; then
349 shift
350 SETUP_ARGS=("$@")
351 break
352 else
353 TEST_ARGS+=("$1")
354 shift
355 fi
356 done
357
358 zsh_query setup "${SETUP_ARGS[@]}"
359
360 local RESULT
361 RESULT="$(zsh_query "${TEST_ARGS[@]}")"
362 local RESULT_EXIT=$?
363 echo -n "Testing $TEST_NUMBER ... "
364
365 if [[ "$EXPECTED" == "-true" ]]; then;
366 if test $RESULT_EXIT -ne 0; then
367 echo "expected TRUE, got $RESULT_EXIT"
368 exit 1
369 fi
370 elif [[ "$EXPECTED" == "-false" ]]; then;
371 if test $RESULT_EXIT -eq 0; then
372 echo "expected FALSE, got $RESULT_EXIT"
373 exit 1
374 fi
375 elif [[ "$EXPECTED" != "$RESULT" ]]; then
376 echo "expected '$EXPECTED', got '$RESULT'"
377 exit 1
378 fi
379
380 echo "OK"
381 }
382
383 opts='-f,--flag,-flag,-a=,--arg=,-arg=,-o=?,--optional=?,-optional=?'
384 test_case 01 'foo' get_positional 1 :SETUP: "$opts" prog foo
385 test_case 02 'foo' get_positional 1 :SETUP: "$opts" prog foo
386 test_case 03 'foo' get_positional 1 :SETUP: "$opts" prog -f foo
387 test_case 04 'foo' get_positional 1 :SETUP: "$opts" prog -flag foo
388 test_case 05 'foo' get_positional 1 :SETUP: "$opts" prog --flag foo
389 test_case 06 'foo' get_positional 1 :SETUP: "$opts" prog -a arg foo
390 test_case 07 'foo' get_positional 1 :SETUP: "$opts" prog -arg arg foo
391 test_case 08 'foo' get_positional 1 :SETUP: "$opts" prog --arg arg foo
392 test_case 09 'foo' get_positional 1 :SETUP: "$opts" prog -aarg foo
393 test_case 10 'foo' get_positional 1 :SETUP: "$opts" prog -arg=arg foo
394 test_case 11 'foo' get_positional 1 :SETUP: "$opts" prog --arg=arg foo
395 test_case 12 'foo' get_positional 1 :SETUP: "$opts" prog -o foo
396 test_case 13 'foo' get_positional 1 :SETUP: "$opts" prog -optional foo
397 test_case 14 'foo' get_positional 1 :SETUP: "$opts" prog --optional foo
398 test_case 15 'foo' get_positional 1 :SETUP: "$opts" prog -oarg foo
399 test_case 16 'foo' get_positional 1 :SETUP: "$opts" prog -optional=arg foo
400 test_case 17 'foo' get_positional 1 :SETUP: "$opts" prog --optional=arg foo
401 test_case 18 'foo' get_positional 1 :SETUP: "$opts" prog -foarg foo
402 test_case 19 'foo' get_positional 1 :SETUP: "$opts" prog -faarg foo
403 test_case 20 'foo' get_positional 1 :SETUP: "$opts" prog -fa arg foo
404 test_case 21 'foo' get_positional 1 :SETUP: "$opts" prog -- foo
405 test_case 22 '-' get_positional 1 :SETUP: "$opts" prog -
406 test_case 23 '-f' get_positional 1 :SETUP: "$opts" prog -- -f
407 test_case 24 -true has_option -f --flag -flag :SETUP: "$opts" prog -f
408 test_case 25 -true has_option -f --flag -flag :SETUP: "$opts" prog --flag
409 test_case 26 -true has_option -f --flag -flag :SETUP: "$opts" prog -flag
410 test_case 27 -false has_option -f --flag -flag :SETUP: "$opts" prog -- -f
411 test_case 28 -false has_option -f --flag -flag :SETUP: "$opts" prog -- --flag
412 test_case 29 -false has_option -f --flag -flag :SETUP: "$opts" prog -- -flag
413 test_case 30 -false has_option -f --flag -flag :SETUP: "$opts" prog --arg --flag
414 test_case 31 -false has_option -f --flag -flag :SETUP: "$opts" prog -a --flag
415 test_case 32 -true has_option -a --arg -arg :SETUP: "$opts" prog -afoo
416 test_case 33 -true has_option -a --arg -arg :SETUP: "$opts" prog -a foo
417 test_case 34 -true has_option -a --arg -arg :SETUP: "$opts" prog -arg foo
418 test_case 35 -true has_option -a --arg -arg :SETUP: "$opts" prog -arg=foo
419 test_case 36 -true has_option -a --arg -arg :SETUP: "$opts" prog --arg foo
420 test_case 37 -true has_option -a --arg -arg :SETUP: "$opts" prog --arg=foo
421 test_case 38 -false has_option -a --arg -arg :SETUP: "$opts" prog -a
422 test_case 39 -false has_option -a --arg -arg :SETUP: "$opts" prog -arg
423 test_case 40 -false has_option -a --arg -arg :SETUP: "$opts" prog --arg
424 test_case 41 -true has_option WITH_INCOMPLETE -a --arg -arg :SETUP: "$opts" prog -a
425 test_case 42 -true has_option WITH_INCOMPLETE -a --arg -arg :SETUP: "$opts" prog -arg
426 test_case 43 -true has_option WITH_INCOMPLETE -a --arg -arg :SETUP: "$opts" prog --arg
427 test_case 44 -true option_is -a --arg -arg -- foo bar :SETUP: "$opts" prog -a foo
428 test_case 45 -true option_is -a --arg -arg -- foo bar :SETUP: "$opts" prog --arg foo
429 test_case 46 -true option_is -a --arg -arg -- foo bar :SETUP: "$opts" prog -arg foo
430 test_case 47 -false option_is -a --arg -arg -- foo :SETUP: "$opts" prog -flag
431 test_case 48 -true option_is -o --optional -optional -- foo bar :SETUP: "$opts" prog -ofoo
432 test_case 49 -true option_is -o --optional -optional -- foo bar :SETUP: "$opts" prog --optional=foo
433 test_case 50 -true option_is -o --optional -optional -- foo bar :SETUP: "$opts" prog -optional=foo
434 test_case 51 -true option_is -o --optional -optional -- foo bar :SETUP: "$opts" prog -obar
435 test_case 52 -true option_is -o --optional -optional -- foo bar :SETUP: "$opts" prog --optional=bar
436 test_case 53 -true option_is -o --optional -optional -- foo bar :SETUP: "$opts" prog -optional=bar
437
438 words=(-o) _has_hidden_option -o --option -option || echo "01: failed"
439 words=(-o arg) _has_hidden_option -o --option -option || echo "02: failed"
440 words=(-oarg) _has_hidden_option -o --option -option || echo "03: failed"
441 words=(-fo arg) _has_hidden_option -o --option -option || echo "04: failed"
442 words=(-foarg) _has_hidden_option -o --option -option || echo "05: failed"
443 words=(--option) _has_hidden_option -o --option -option || echo "06: failed"
444 words=(--option arg) _has_hidden_option -o --option -option || echo "07: failed"
445 words=(--option=arg) _has_hidden_option -o --option -option || echo "08: failed"
446 words=(-option) _has_hidden_option -o --option -option || echo "09: failed"
447 words=(-option arg) _has_hidden_option -o --option -option || echo "10: failed"
448 words=(-option=arg) _has_hidden_option -o --option -option || echo "11: failed"
449 words=(-o 1 2) _has_hidden_option -o --option -option && echo "12: failed"