-
Notifications
You must be signed in to change notification settings - Fork 249
/
option.sh
385 lines (319 loc) · 10.9 KB
/
option.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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
import util/class util/namedParameters
import util/tryCatch
class:Option() {
public string name
public string value
public string letter
public string flag
public string required
Option.__getter__() {
serializedOption="{\"name\":\"$(this name)\",\"value\":\"$(this value)\",\"letter\":\"$(this letter)\",\"flag\":\"$(this flag)\",\"required\":\"$(this required)\"}"
@return:value $serializedOption
}
}
Type::Initialize Option
class:Options() {
public map optionsMap
public string yadSuccess
Options.ToArray() {
map toSetOptionsArray
string indexList=$(this optionsMap)
indexList=$($var:indexList sanitizeJSON)
string serializedOption
for serializedOption in $indexList; do
serializedOption=$($var:serializedOption sanitizeSingleJSON)
optionName=$(Options::GetSerializedAttribute "$serializedOption" 'name')
optionValue=$(Options::GetSerializedAttribute "$serializedOption" 'value')
toSetOptionsArray[$optionName]=$optionValue
done
@return toSetOptionsArray
}
Options.__getter__() {
string indexList=$(this optionsMap)
@return:value "$($var:indexList sanitizeJSON)"
}
Options.Set() {
[reference] toSet
this optionsMap set "$($var:toSet name)" "$($var:toSet)"
}
Options.Delete () {
[reference] toDelete
this optionsMap delete "$($var:toDelete name)"
}
Options::GetSerializedAttribute() {
[string] serializedOption
[string] attributeName
attributeValue=''
regex="$attributeName\":\"([[:alnum:]]+)"
[[ $serializedOption =~ $regex ]] && attributeValue="${BASH_REMATCH[1]}"
echo "$attributeValue"
}
Options::Unserialize() {
[string] serializedOption
[reference] toReturn
serializedOption=$($var:serializedOption sanitizeSingleJSON)
$var:toReturn name = $(Options::GetSerializedAttribute "$serializedOption" 'name')
$var:toReturn value = $(Options::GetSerializedAttribute "$serializedOption" 'value')
$var:toReturn letter = $(Options::GetSerializedAttribute "$serializedOption" 'letter')
$var:toReturn flag = $(Options::GetSerializedAttribute "$serializedOption" 'flag')
$var:toReturn required = $(Options::GetSerializedAttribute "$serializedOption" 'required')
}
Options.Search() {
[string] attributeName
[string] textToSearch
Option optionFound
string serializedOption
string indexList=$(this optionsMap)
indexList=$($var:indexList sanitizeJSON)
itemFound=false
for serializedOption in $indexList; do
serializedOption=$($var:serializedOption sanitizeSingleJSON)
attributeValue=$(Options::GetSerializedAttribute "$serializedOption" "$attributeName")
if [[ "$attributeValue" == "$textToSearch" ]]; then
itemFound=true
break
fi
done
[[ "$itemFound" == false ]] && return 1
Options::Unserialize "$serializedOption" $ref:optionFound
@return optionFound
}
Options.GetOptionsString() {
optionsString=''
optionLetter=''
optionFlag=false
string serializedOption
string indexList=$(this optionsMap)
indexList=$($var:indexList sanitizeJSON)
for serializedOption in $indexList; do
serializedOption=$($var:serializedOption sanitizeSingleJSON)
optionLetter=$(Options::GetSerializedAttribute "$serializedOption" 'letter')
optionFlag=$(Options::GetSerializedAttribute "$serializedOption" 'flag')
optionsString+=$optionLetter
if [[ "$optionFlag" == true ]]; then
optionsString+=','
else
optionsString+=':'
fi
done
@return:value $optionsString
}
}
Type::Initialize Options
class:OptionsWrapper() {
OptionsWrapper.SetDefaults() {
[reference] toSaveDefaultOptions
[reference] defaultOptions
string serializedOption
string indexList="$($var:defaultOptions)"
indexList=$($var:indexList unJsonfy)
Option toDefault
for serializedOption in $indexList; do
# Replace comma with space.
serializedOption=${serializedOption//,/ }
serializedArray=($serializedOption)
$var:toDefault name = "${serializedArray[0]}"
$var:toDefault value = "${serializedArray[1]}"
$var:toDefault letter = "${serializedArray[2]}"
$var:toDefault flag = "${serializedArray[3]}"
$var:toDefault required = "${serializedArray[4]}"
$var:toSaveDefaultOptions Set toDefault
done
@return toSaveDefaultOptions
}
OptionsWrapper.ParseArguments() {
[reference] toSaveParsedOptions
optionsString=$($var:toSaveParsedOptions GetOptionsString)
string optionValue=''
optionName=''
optionFlag=false
shift
while getopts $optionsString opt; do
try {
! [[ "$opt" == "?" ]]
} catch {
echo "Ilegal option '$opt'."
return 1
}
Option parsedOption=$($var:toSaveParsedOptions Search 'letter' "$opt")
optionFlag=$($var:parsedOption flag)
optionValue=true
[[ "$optionFlag" == false ]] && optionValue="${OPTARG}"
optionValue=$($var:optionValue trim)
$var:parsedOption value = "$optionValue"
$var:toSaveParsedOptions Set parsedOption
done
@return toSaveParsedOptions
}
OptionsWrapper.GetOptionsGUI() {
[reference] toSaveOptionsGUI
string serializedOption
Option guiOption
string indexList="$($var:toSaveOptionsGUI optionsMap)"
indexList=$($var:indexList sanitizeJSON)
optionName=''
optionValue=''
optionFlag=false
$var:toSaveOptionsGUI yadSuccess = true
yadInstalled=$(which 'yad')
if [[ -z "$yadInstalled" ]]; then
$var:toSaveOptionsGUI yadSuccess = false
fi
# Generate yad form.
yadString='yad --form --title="Set options" '
yadOptionsString=''
yadFlagsString=''
yadOption=''
yadLabels=''
# Save index names to manipulate options array later.
optionsNamesList=''
flagsNamesList=''
for serializedOption in $indexList; do
serializedOption=$($var:serializedOption sanitizeSingleJSON)
optionName=$(Options::GetSerializedAttribute "$serializedOption" 'name')
optionValue=$(Options::GetSerializedAttribute "$serializedOption" 'value')
optionFlag=$(Options::GetSerializedAttribute "$serializedOption" 'flag')
# When executing yad with eval, the text with the form: --field="${option[value]}"
# does not get well parsed, to prevent that, add to the yad string the keywords 'name:' and 'value'
# and then replace them with the actual values.
yadOption='--field="name": value '
yadOption=${yadOption/name/$optionName}
# Set the type to checkbox.
if [[ "$optionFlag" == true ]]; then
yadOption=${yadOption/ value/CHK $optionValue}
yadFlagsString+=$yadOption
flagsNamesList+="$optionName "
else
yadOption=${yadOption/value/$optionValue}
yadOptionsString+=$yadOption
optionsNamesList+="$optionName "
fi
done
# Show menu.
yadString+=${yadOptionsString}${yadFlagsString}
yadInput=$(eval $yadString)
string optionsLabelsList="${optionsNamesList}${flagsNamesList}"
optionsLabelsList=$($var:optionsLabelsList trim)
yadLabels=($optionsLabelsList)
# Read the values and store them on the options map.
index=0
IFS='|' read -ra input <<< "$yadInput"
for value in "${input[@]}"; do
optionName="${yadLabels[$index]}"
optionValue="$value"
# Search option on defaults.
Option guiOption=$($var:toSaveOptionsGUI Search 'name' "$optionName")
# If required value must be provided.
optionRequired=$($var:guiOption required)
if [[ "$optionRequired" == true ]]; then
try {
! [[ -z "$optionValue" ]]
} catch {
echo "The option $optionName is required, process aborted." | yad --text-info --width=400 --height=200
$var:toSaveOptionsGUI yadSuccess = false
}
fi
# Set new value.
$var:guiOption value = "$optionValue"
$var:toSaveOptionsGUI Set guiOption
((index++))
done
@return toSaveOptionsGUI
}
}
Type::Initialize OptionsWrapper
# Sanitize to a maximun of two levels only.
string.sanitizeJSON() {
@resolve:this
local toSanitize="$this"
local sanitizedJSONString=''
# Remove \".
toSanitize="${toSanitize//\\\"/}"
# Remove ".
toSanitize="${toSanitize//\"/}"
# Remove [.
toSanitize="${toSanitize//\[/}"
# Replace ]= with :.
toSanitize="${toSanitize//\]=/:}"
# Replace ( with {.
toSanitize="${toSanitize//\(/{}"
# Replace ) with }.
toSanitize="${toSanitize//\)/}}"
# Replace space} with }.
toSanitize="${toSanitize//\ \}/}}"
# Replace space with space,.
toSanitize="${toSanitize// / ,}"
# Trim.
toSanitize=$(var: toSanitize trim)
# Sanitize object by object.
for jsonItem in $toSanitize; do
jsonItem=$(var: jsonItem sanitizeSingleJSON)
sanitizedJSONString+=$jsonItem
sanitizedJSONString+=' ,'
done
sanitizedJSONString="${sanitizedJSONString::-2}"
# Verify if this is a single json option.
# If not, put {}.
openingBracketsCount=$(echo $sanitizedJSONString | grep -o '{' | wc -l)
[[ $openingBracketsCount -gt 1 ]] && sanitizedJSONString="{${sanitizedJSONString}}"
@return sanitizedJSONString
}
string.sanitizeSingleJSON() {
@resolve:this
local toSanitizeSingle="$this"
regex='.*(\{.*\})'
# Verify this is a single JSON option.
openingBracketsCount=$(echo $toSanitizeSingle | grep -o '{' | wc -l)
[[ $openingBracketsCount -gt 2 ]] && @return toSanitizeSingle
# Remove \".
toSanitizeSingle="${toSanitizeSingle//\\\"/}"
# Remove ".
toSanitizeSingle="${toSanitizeSingle//\"/}"
# Replace double {{ }} with single { }.
toSanitizeSingle="${toSanitizeSingle//\{\{/\{}"
toSanitizeSingle="${toSanitizeSingle//\}\}/\}}"
[[ $toSanitizeSingle =~ $regex ]] && toSanitizeSingle="${BASH_REMATCH[1]}"
# Remove all { }.
toSanitizeSingle="${toSanitizeSingle//\{/}"
toSanitizeSingle="${toSanitizeSingle//\}/}"
# Put " back.
toSanitizeSingle="${toSanitizeSingle//:/\":\"}"
toSanitizeSingle="${toSanitizeSingle//,/\",\"}"
toSanitizeSingle="\"${toSanitizeSingle}\""
# Put { } back.
toSanitizeSingle="{${toSanitizeSingle}}"
@return toSanitizeSingle
}
string.unJsonfy() {
@resolve:this
local toUnJsonfy="$this"
# Remove (.
toUnJsonfy=${toUnJsonfy//\(/}
# Remove ).
toUnJsonfy=${toUnJsonfy//\)/}
# Remove ].
toUnJsonfy=${toUnJsonfy//\]/}
# Remove {.
toUnJsonfy=${toUnJsonfy//\}/}
# Remove }.
toUnJsonfy=${toUnJsonfy//\{/}
# Remove =.
toUnJsonfy=${toUnJsonfy//\=/}
# Remove ".
toUnJsonfy="${toUnJsonfy//\"/}"
# Replace [ with space,.
toUnJsonfy=${toUnJsonfy//\[/ ,}
toUnJsonfy=$(var: toUnJsonfy trim)
[[ "${toUnJsonfy:0:1}" == ',' ]] && toUnJsonfy="${toUnJsonfy:1}"
@return toUnJsonfy
}
string.trim() {
@resolve:this
local toTrim="$this"
# Remove leading whitespace.
toTrim="${toTrim#"${toTrim%%[![:space:]]*}"}"
# Remove trailing whitespace.
toTrim="${toTrim%"${var##*[![:space:]]}"}"
toTrim=$(echo -n "$toTrim")
@return toTrim
}