-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse-options.sh
341 lines (292 loc) · 12 KB
/
parse-options.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
#!/bin/bash -e
# shellcheck disable=SC1091
source ./util.sh
# OPTION ARR STRUCTURE [
# (short, long, argument, default)
# ...
# ]
function print-options-table {
# structure (short, long, argument, default)
if ! declare -p options 2> /dev/null | grep -q 'declare \-a'; then
printf 'The "options" variable must be declared as an array before invoking print-options-table\n' >&2
return 1
fi
local header_idx='IDX' \
header_short='SHORT' \
header_long='LONG' \
header_argument='ARG' \
header_default='DEFAULT' \
header_occurance='OCCUR' \
local width_idx=${#header_idx} \
width_short=${#header_short} \
width_long=${#header_long} \
width_argument=${#header_argument} \
width_default=${#header_default} \
width_occurance=${#header_occurance}
local e i=0
for (( i=0; i<${#options[@]}; i+=_DOCOPT_OPTION_STRUCT_LENGTH )); do
e=$(( i / _DOCOPT_OPTION_STRUCT_LENGTH )); if (( ${#e} > width_idx )); then width_idx=${#e}; fi
e="${options[i+_DOCOPT_OPTION_POSITION_SHORT]}"; if (( ${#e} > width_short )); then width_short=${#e}; fi
e="${options[i+_DOCOPT_OPTION_POSITION_LONG]}"; if (( ${#e} > width_long )); then width_long=${#e}; fi;
e="${options[i+_DOCOPT_OPTION_POSITION_ARGUMENT]}"; if (( ${#e} > width_argument )); then width_argument=${#e}; fi;
e="${options[i+_DOCOPT_OPTION_POSITION_DEFAULT]}"; if (( ${#e} > width_default )); then width_default=${#e}; fi;
e="${options[i+_DOCOPT_OPTION_POSITION_OCCURANCE]}"; if (( ${#e} > width_occurance )); then width_occurance=${#e}; fi;
done
local -a column_spec=( \
"$header_idx" "$width_idx" \
"$header_short" "$width_short" \
"$header_long" "$width_long" \
"$header_argument" "$width_argument" \
"$header_default" "$width_default" \
"$header_occurance" "$width_occurance"
)
print-table-header
for (( i=0; i<${#options[@]}; i+=_DOCOPT_OPTION_STRUCT_LENGTH )); do
column_spec[0]=$(( i / _DOCOPT_OPTION_STRUCT_LENGTH ))
column_spec[2]="${options[i+_DOCOPT_OPTION_POSITION_SHORT]}"
column_spec[4]="${options[i+_DOCOPT_OPTION_POSITION_LONG]}"
column_spec[6]="${options[i+_DOCOPT_OPTION_POSITION_ARGUMENT]}"
column_spec[8]="${options[i+_DOCOPT_OPTION_POSITION_DEFAULT]}"
e="${options[i+_DOCOPT_OPTION_POSITION_OCCURANCE]}"
case "$e" in
6) e=once;;
7) e=cont;;
*) printf 'Unknown occurance: %s\n' "$e" >&2; return 1;;
esac
# shellcheck disable=SC2034
column_spec[10]="$e"
print-table-row
done
print-table-hr
}
function parse-options {
if ! declare -p options 2> /dev/null | grep -q 'declare \-a'; then
printf 'The "options" variable must be declared as an array before invoking print-options-table\n' >&2
return 1
fi
local -n err
# $option the current option e.g. [-o, --option] name that is beign built.
# $argument the current argument
local USAGE line char \
short long argument default arg def option \
is_option=false is_short=false \
is_single_space=false is_argument=false \
is_description=false is_default=false \
i=-1
local -i exit_code=0
USAGE=$1
err=$2
while IFS= read -r line; do
if test -z "$line"; then continue; fi
if grep -qE '^\s+-' <<< "$line"; then
# debug-line "$line"
# shellcheck disable=SC1007
short= long= argument= default= option= arg= def=
is_option=false is_short=false
is_single_space=false is_argument=false
is_description=false is_default=false
i=-1
while IFS= read -r -n1 char; do
if ! test $exit_code -eq 0; then break 2; fi
i=$((i + 1))
if test -z "$char"; then continue; fi
# debug-line-printf "$line" 'is_argument = %s\n' "$is_argument"
# debug-line-printf "$line" 'is_default = %s\n' "$is_default"
# debug-line-printf "$line" 'is_description = %s\n' "$is_description"
# debug-line-printf "$line" 'is_option = %s\n' "$is_option"
# debug-line-printf "$line" 'is_short = %s\n' "$is_short"
# debug-line-printf "$line" 'is_single_space = %s\n' "$is_single_space"
# debug-line-printf "$line" 'option = %s\n' "$option"
# debug-line-printf "$line" 'def = %s\n' "$def"
# debug-line-printf "$line" 'arg = %s\n' "$arg"
# debug-line-printf "$line" 'short = %s\n' "$short"
# debug-line-printf "$line" 'long = %s\n' "$long"
# debug-line-printf "$line" 'argument = %s\n' "$argument"
# debug-line-printf "$line" 'default = %s\n' "$default"
# debug-line-printf "$line" '\n'
# debug-line-single "$line" "$i"
if [[ "$char" == "," ]]; then
# [-o,]
# [-o ARG,]
# [-o=ARG,]
# [--option,]
# [--option ARG,]
# [--option=ARG,]
continue;
fi
if [[ "$char" == "[" ]]; then
if $is_description; then
is_default=true
fi
continue
fi
if [[ "$char" == "]" ]]; then
if $is_default && ! test -z "$def"; then
default="$def"
def=
fi
is_default=false
continue
fi
if [[ "$char" == " " ]]; then
# [-o ]
# [-o ]
# [-o ARG ]
# [-o ARG ]
# [--option ]
# [--option ]
# [--option ARG ]
# [--option ARG ]
# [--option=ARG ]
# [--option=ARG ]
# [ [default: ]
if $is_default; then
# [ [default: ]
if ! test -z "$def"; then
def+="$char"
fi
continue
fi
if $is_single_space || $is_description; then
# [-o ]
# [-o ARG ]
# [-o=ARG ]
# [--option ]
# [--option ARG ]
# [--option=ARG ]
is_description=true
is_single_space=false
continue
fi
if ! $is_single_space && ($is_option || $is_argument); then
# [-o ]
# [-o ARG ]
# [--option ]
# [--option ARG ]
is_single_space=true
fi
is_argument=false
if ! test -z "$arg"; then
# [-o ARG ]
# [-o=ARG ]
# [--option ARG ]
# [--option=ARG ]
if ! test -z "$argument" && [[ "$argument" != "$arg" ]]; then
# shellcheck disable=SC2034
printf -v err 'Argument: %s should match %s in option %s %s' "$arg" "$argument" "$long" "$short"
exit_code=1
continue
fi
argument="$arg"
arg=
fi
if ! test -z "$option"; then
# [-o ]
# [--option ]
if $is_short; then
# [-o ]
short="$option"
else
# [--option ]
long="$option"
fi
# shellcheck disable=SC1007
option= # reset option variable
fi
is_argument=$is_option
is_option=false
is_short=false
continue
fi
if [[ "$char" == "-" ]]; then
# [-]
# [--]...
if $is_default; then
def+="$char"
continue
fi
if $is_argument && ! test -z "$arg"; then
# [-o=ARGU-MENT]
# [-o ARGU-MENT]
# [--option=ARGU-MENT]
# [--option ARGU-MENT]
arg+="$char"
continue
# arg= #reset arg variable
fi
if $is_option && $is_single_space && ! test -z "$option"; then
# [--option -]
# [-o -]
if ! test -z "$option"; then
if $is_short; then
short="$option"
else
long="$option"
fi
# shellcheck disable=SC1007
option= # reset option variable
fi
fi
if $is_option && ! $is_short && ! $is_single_space; then
# [--*-]
option+="$char"
continue
fi
if $is_option && $is_short; then
# [--]
is_short=false
else
# [-]
is_short=true
fi
# [-]
# [--]
is_option=true
continue
fi
is_single_space=false
if [[ "$char" == "=" ]]; then
if $is_option && ! test -z "$option"; then
# [-o=]
# [--option=]
if $is_short; then
short="$option"
else
long="$option"
fi
# shellcheck disable=SC1007
option= # reset option variable
fi
if ! $is_description && test -z "$arg"; then
# if no agument has been found for current option
is_argument=true
fi
is_option=false
is_short=false
continue
fi
if $is_default; then
def+="$char"
if [[ "default:" == "$def" ]]; then
def=
fi
fi
if ! $is_option && ! $is_description && $is_argument; then
# [-o ARG]
# [-o=ARG]
# [--option ARG]
# [--option=ARG]
is_argument=true
arg+="$char"
fi
if $is_option; then
# [-o]
# [--option]
option+="$char"
fi
done <<< "$line"
options+=( "$short" "$long" "$argument" "$default" "$_DOCOPT_PROGRAM_OCCURANCE_ONCE" )
fi
done <<< "$USAGE"
return $exit_code
}