forked from oldratlee/useful-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
parseOpts.sh
executable file
·358 lines (313 loc) · 11.1 KB
/
parseOpts.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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
#!/bin/bash
# @Function
# parse options lib, support multiple values for one option.
#
# @Usage
# source this script to your script file, then use func parseOpts.
# parseOpts func useage sample:
# $ parseOpts "a,a-long|b,b-long:|c,c-long+" -a -b bv -c c.sh -p pv -q qv arg1 \; aa bb cc
# then below globle var is set:
# _OPT_VALUE_a = true
# _OPT_VALUE_a_long = true
# _OPT_VALUE_b = bv
# _OPT_VALUE_b_long = bv
# _OPT_VALUE_c = (c.sh -p pv -q qv arg1) # Array type
# _OPT_VALUE_c_long = (c.sh -p pv -q qv arg1) # Array type
# _OPT_ARGS = (aa bb cc) # Array type
#
# @author Jerry Lee
#####################################################################
# Utils Methods
#####################################################################
_opts_colorEcho() {
local color=$1
shift
if [ -c /dev/stdout ] ; then
# if stdout is console, turn on color output.
echo -ne "\033[1;${color}m"
echo -n "$@"
echo -e "\033[0m"
else
echo "$@"
fi
}
_opts_redEcho() {
_opts_colorEcho 31 "$@"
}
_opts_convertToVarName() {
[ $# -ne 1 ] && {
_opts_redEcho "NOT 1 arguemnts when call _opts_convertToVarName: $@"
return 1
}
echo "$1" | sed 's/-/_/g'
}
#####################################################################
# Parse Methods
#
# Use Globle Variable:
# * _OPT_INFO_LIST_INDEX : Option info, data structure.
# _OPT_INFO_LIST_INDEX ->* _a_a_long -> option value.
# * _OPT_VALUE_* : value of option. is Array type for + mode option
# * _OPT_ARGS : option arguments
#####################################################################
_opts_findOptMode() {
[ $# -ne 1 ] && {
_opts_redEcho "NOT 1 arguemnts when call _opts_findOptMode: $@"
return 1
}
local opt="$1" # like a, a-long
local idxName
for idxName in "${_OPT_INFO_LIST_INDEX[@]}" ; do
local idxNameArrayPlaceHolder="$idxName[@]"
local -a idxNameArray=("${!idxNameArrayPlaceHolder}")
local mode="${idxNameArray[0]}"
local optName
for optName in "${idxNameArray[@]:1:${#idxNameArray[@]}}"; do # index from 1, skip mode
[ "$opt" = "${optName}" ] && {
echo "$mode"
return
}
done
done
echo ""
}
_opts_setOptBool() {
[ $# -ne 2 ] && {
_opts_redEcho "NOT 2 arguemnts when call _opts_setOptBool: $@"
return 1
}
_opts_setOptValue "$@"
}
_opts_setOptValue() {
[ $# -ne 2 ] && {
_opts_redEcho "NOT 2 arguemnts when call _opts_setOptValue: $@"
return 1
}
local opt="$1" # like a, a-long
local value="$2"
local idxName
for idxName in "${_OPT_INFO_LIST_INDEX[@]}" ; do
local idxNameArrayPlaceHolder="$idxName[@]"
local -a idxNameArray=("${!idxNameArrayPlaceHolder}")
local optName
for optName in "${idxNameArray[@]:1:${#idxNameArray[@]}}"; do # index from 1, skip mode
[ "$opt" = "$optName" ] && {
local optName2
for optName2 in "${idxNameArray[@]:1:${#idxNameArray[@]}}"; do
local optValueVarName="_OPT_VALUE_`_opts_convertToVarName "${optName2}"`"
local from='"$value"'
eval "$optValueVarName=$from" # set global var!
done
return
}
done
done
_opts_redEcho "NOT Found option $opt!"
return 1
}
_opts_setOptArray() {
local opt="$1" # like a, a-long
shift
local idxName
for idxName in "${_OPT_INFO_LIST_INDEX[@]}" ; do
local idxNameArrayPlaceHolder="$idxName[@]"
local -a idxNameArray=("${!idxNameArrayPlaceHolder}")
local optName
for optName in "${idxNameArray[@]:1:${#idxNameArray[@]}}"; do # index from 1, skip mode
[ "$opt" = "$optName" ] && {
# set _OPT_VALUE
local optName2
for optName2 in "${idxNameArray[@]:1:${#idxNameArray[@]}}"; do
local optValueVarName="_OPT_VALUE_`_opts_convertToVarName "${optName2}"`"
local from='"$@"'
eval "$optValueVarName=($from)" # set global var!
done
return
}
done
done
_opts_redEcho "NOT Found option $opt!"
return 1
}
_opts_cleanOptValueInfoList() {
local idxName
for idxName in "${_OPT_INFO_LIST_INDEX[@]}"; do
local idxNameArrayPlaceHolder="$idxName[@]"
local -a idxNameArray=("${!idxNameArrayPlaceHolder}")
eval "unset $idxName"
local optName
for optName in "${idxNameArray[@]:1:${#idxNameArray[@]}}"; do # index from 1, skip mode
local optValueVarName="_OPT_VALUE_`_opts_convertToVarName "$optName"`"
eval "unset $optValueVarName"
done
done
unset _OPT_INFO_LIST_INDEX
unset _OPT_ARGS
}
parseOpts() {
local optsDescription="$1" # optsDescription LIKE a,a-long|b,b-long:|c,c-long+
shift
_OPT_INFO_LIST_INDEX=() # set global var!
local optDescLines=`echo "$optsDescription" |
# cut head and tail space
sed -r 's/^\s+//;s/\s+$//' |
awk -F '[\t ]*\\\\|[\t ]*' '{for(i=1; i<=NF; i++) print $i}'`
local optDesc
while read optDesc ; do # optDesc LIKE b,b-long:
[ -z "$optDesc" ] && continue
local mode="${optDesc:(-1)}" # LIKE : or +
case "$mode" in
+|:|-)
optDesc="${optDesc:0:(${#optDesc}-1)}" # LIKE b,b-long
;;
*)
mode="-"
;;
esac
local optLines=`echo "$optDesc" | awk -F '[\t ]*,[\t ]*' '{for(i=1; i<=NF; i++) print $i}'` # LIKE "a\na-long"
[ $(echo "$optLines" | wc -l) -gt 2 ] && {
_opts_redEcho "Illegal option description($optDesc), more than 2 opt name!" 1>&2
_opts_cleanOptValueInfoList
return 220
}
local -a optTuple=()
local opt
while read opt ; do # opt LIKE a , a-long
[ -z "$opt" ] && continue
[ ${#opt} -eq 1 ] && {
echo "$opt" | grep -E '^[a-zA-Z0-9]$' -q || {
_opts_redEcho "Illegal short option name($opt in $optDesc) in option description!" 1>&2
_opts_cleanOptValueInfoList
return 221
}
} || {
echo "$opt" | grep -E '^[-a-zA-Z0-9]+$' -q || {
_opts_redEcho "Illegal long option name($opt in $optDesc) in option description!" 1>&2
_opts_cleanOptValueInfoList
return 222
}
}
optTuple=("${optTuple[@]}" "$opt")
done < <(echo "$optLines")
[ ${#optTuple[@]} -gt 2 ] && {
_opts_redEcho "more than 2 opt(${optTuple[@]}) in option description($optDesc)!" 1>&2
_opts_cleanOptValueInfoList
return 223
}
local idxName=
local evalOpts=
local o
for o in "${optTuple[@]}"; do
idxName="${idxName}_opts_index_name_`_opts_convertToVarName "$o"`"
evalOpts="${evalOpts} $o"
done
eval "$idxName=($mode $evalOpts)"
_OPT_INFO_LIST_INDEX=("${_OPT_INFO_LIST_INDEX[@]}" "$idxName")
done < <(echo "$optDescLines")
local -a args=()
while true; do
[ $# -eq 0 ] && break
case "$1" in
---*)
_opts_redEcho "Illegal option($1), more than 2 prefix '-'!" 1>&2
_opts_cleanOptValueInfoList
return 230
;;
--)
shift
args=("${args[@]}" "$@")
break
;;
-*) # short & long option(-a, -a-long), use same read-in logic.
local opt="$1"
local optName=`echo "$1" | sed -r 's/^--?//'`
local mode=`_opts_findOptMode "$optName"`
case "$mode" in
-)
_opts_setOptBool "$optName" "true"
shift
;;
:)
[ $# -lt 2 ] && {
_opts_redEcho "Option $opt has NO value!" 1>&2
_opts_cleanOptValueInfoList
return 231
}
_opts_setOptValue "$optName" "$2"
shift 2
;;
+)
shift
local -a valueArray=()
local foundComma=""
local value
for value in "$@" ; do
[ ";" = "$value" ] && {
foundComma=true
break
} || valueArray=("${valueArray[@]}" "$value")
done
[ "$foundComma" ] || {
_opts_redEcho "value of option $opt no end comma, value = ${valueArray[@]}" 1>&2
_opts_cleanOptValueInfoList
return 231
}
shift "$((${#valueArray[@]} + 1))"
_opts_setOptArray "$optName" "${valueArray[@]}"
;;
*)
_opts_redEcho "Undefined option $opt!" 1>&2
_opts_cleanOptValueInfoList
return 232
;;
esac
;;
*)
args=("${args[@]}" "$1")
shift
;;
esac
done
_OPT_ARGS=("${args[@]}") # set global var!
}
#####################################################################
# Show parsed Option Info Methods
#####################################################################
_opts_showOptDescInfoList() {
echo "==============================================================================="
echo "show option desc info list:"
local idxName
for idxName in "${_OPT_INFO_LIST_INDEX[@]}"; do
local idxNameArrayPlaceHolder="$idxName[@]"
echo "$idxName = (${!idxNameArrayPlaceHolder})"
done
echo "==============================================================================="
}
_opts_showOptValueInfoList() {
echo "==============================================================================="
echo "show option value info list:"
local idxName
for idxName in "${_OPT_INFO_LIST_INDEX[@]}"; do
local idxNameArrayPlaceHolder="$idxName[@]"
local -a idxNameArray=("${!idxNameArrayPlaceHolder}")
local mode=${idxNameArray[0]}
local optName
for optName in "${idxNameArray[@]:1:${#idxNameArray[@]}}"; do # index from 1, skip mode
local optValueVarName="_OPT_VALUE_`_opts_convertToVarName "$optName"`"
case "$mode" in
-)
echo "$optValueVarName=${!optValueVarName}"
;;
:)
echo "$optValueVarName=${!optValueVarName}"
;;
+)
local optArrayValueArrayPlaceHolder="$optValueVarName[@]"
echo "$optValueVarName=(${!optArrayValueArrayPlaceHolder})"
;;
esac
done
done
echo "_OPT_ARGS=(${_OPT_ARGS[@]})"
echo "==============================================================================="
}