-
Notifications
You must be signed in to change notification settings - Fork 0
/
html
executable file
·370 lines (329 loc) · 10.8 KB
/
html
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
#!/bin/bash
# @parseArger-begin
# @parseArger-help "option to html form" --option "help" --short-option "h"
# @parseArger-version "0.1" --option "version" --short-option "v"
# @parseArger-verbose --option "verbose" --level "0" --quiet-option "quiet"
# @parseArger-declarations
# @parseArger pos arg-name "nested option namespace"
# @parseArger pos description "positional argument description"
# @parseArger opt one-of "accepted values for keys" --repeat
# @parseArger opt complete "bash built-in completely function" --repeat
# @parseArger opt complete-custom "completely custom dynamic suggestion" --repeat
# @parseArger opt input-container-class "input container class" --default-value "form-group"
# @parseArger opt input-class "input class" --default-value "form-control"
# @parseArger opt label-class "label class" --default-value "form-label"
# @parseArger opt select-class "select class" --default-value "form-select"
# @parseArger opt checkbox-container-class "checkbox and radio class" --default-value "form-check" --alias radio-container-class
# @parseArger opt checkbox-class "checkbox and radio class" --default-value "form-check-input" --alias radio-class
# @parseArger opt checkbox-label-class "checkbox and radio label class" --default-value "form-check-label" --alias radio-label-class
# @parseArger-declarations-end
# @parseArger-utils
_helpHasBeenPrinted=1;
_SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd -P)";
# @parseArger-utils-end
# @parseArger-parsing
die()
{
local _ret="${2:-1}"
test "${_PRINT_HELP:-no}" = yes && print_help >&2
log "$1" -3 >&2
exit "${_ret}"
}
begins_with_short_option()
{
local first_option all_short_options=''
first_option="${1:0:1}"
test "$all_short_options" = "${all_short_options/$first_option/}" && return 1 || return 0
}
# POSITIONALS ARGUMENTS
_positionals=();
_optional_positionals=();
_arg_arg_name="";
_arg_description="";
# OPTIONALS ARGUMENTS
_arg_one_of=()
_arg_complete=()
_arg_complete_custom=()
_arg_input_container_class="form-group"
_arg_input_class="form-control"
_arg_label_class="form-label"
_arg_select_class="form-select"
_arg_checkbox_container_class="form-check"
_arg_checkbox_class="form-check-input"
_arg_checkbox_label_class="form-check-label"
# FLAGS
# NESTED
_verbose_level="0";
print_help()
{
_triggerSCHelp=1;
if [[ "$_helpHasBeenPrinted" == "1" ]]; then
_helpHasBeenPrinted=0;
echo -e "option to html form:"
echo -e " arg-name: nested option namespace"
echo -e " description: positional argument description"
echo -e " --one-of <one-of>: accepted values for keys, repeatable"
echo -e " --complete <complete>: bash built-in completely function, repeatable"
echo -e " --complete-custom <complete-custom>: completely custom dynamic suggestion, repeatable"
echo -e " --input-container-class <input-container-class>: input container class [default: ' form-group ']"
echo -e " --input-class <input-class>: input class [default: ' form-control ']"
echo -e " --label-class <label-class>: label class [default: ' form-label ']"
echo -e " --select-class <select-class>: select class [default: ' form-select ']"
echo -e " --checkbox-container-class|--radio-container-class <checkbox-container-class>: checkbox and radio class [default: ' form-check ']"
echo -e " --checkbox-class|--radio-class <checkbox-class>: checkbox and radio class [default: ' form-check-input ']"
echo -e " --checkbox-label-class|--radio-label-class <checkbox-label-class>: checkbox and radio label class [default: ' form-check-label ']"
echo -e "Usage :
$0 <arg-name> <description> [--one-of <value>] [--complete <value>] [--complete-custom <value>] [--input-container-class <value>] [--input-class <value>] [--label-class <value>] [--select-class <value>] [--checkbox-container-class <value>] [--checkbox-class <value>] [--checkbox-label-class <value>]";
fi
}
log() {
local _arg_msg="${1}";
local _arg_level="${2:-0}";
if [ "${_arg_level}" -le "${_verbose_level}" ]; then
case "$_arg_level" in
-3)
_arg_COLOR="\033[0;31m";
;;
-2)
_arg_COLOR="\033[0;33m";
;;
-1)
_arg_COLOR="\033[1;33m";
;;
1)
_arg_COLOR="\033[0;32m";
;;
2)
_arg_COLOR="\033[1;36m";
;;
3)
_arg_COLOR="\033[0;36m";
;;
*)
_arg_COLOR="\033[0m";
;;
esac
echo -e "${_arg_COLOR}${_arg_msg}\033[0m";
fi
}
parse_commandline()
{
_positionals_count=0
while test $# -gt 0
do
_key="$1"
case "$_key" in
--one-of)
test $# -lt 2 && die "Missing value for the option: '$_key'" 1
_arg_one_of+=("$2")
shift
;;
--one-of=*)
_arg_one_of+=("${_key##--one-of=}")
;;
--complete)
test $# -lt 2 && die "Missing value for the option: '$_key'" 1
_arg_complete+=("$2")
shift
;;
--complete=*)
_arg_complete+=("${_key##--complete=}")
;;
--complete-custom)
test $# -lt 2 && die "Missing value for the option: '$_key'" 1
_arg_complete_custom+=("$2")
shift
;;
--complete-custom=*)
_arg_complete_custom+=("${_key##--complete-custom=}")
;;
--input-container-class)
test $# -lt 2 && die "Missing value for the option: '$_key'" 1
_arg_input_container_class="$2"
shift
;;
--input-container-class=*)
_arg_input_container_class="${_key##--input-container-class=}"
;;
--input-class)
test $# -lt 2 && die "Missing value for the option: '$_key'" 1
_arg_input_class="$2"
shift
;;
--input-class=*)
_arg_input_class="${_key##--input-class=}"
;;
--label-class)
test $# -lt 2 && die "Missing value for the option: '$_key'" 1
_arg_label_class="$2"
shift
;;
--label-class=*)
_arg_label_class="${_key##--label-class=}"
;;
--select-class)
test $# -lt 2 && die "Missing value for the option: '$_key'" 1
_arg_select_class="$2"
shift
;;
--select-class=*)
_arg_select_class="${_key##--select-class=}"
;;
--radio-container-class|--checkbox-container-class)
test $# -lt 2 && die "Missing value for the option: '$_key'" 1
_arg_checkbox_container_class="$2"
shift
;;
--checkbox-container-class=*)
_arg_checkbox_container_class="${_key##--checkbox-container-class=}"
;;
--radio-container-class=*)
_arg_checkbox_container_class="${_key##--radio-container-class=}"
;;
--radio-class|--checkbox-class)
test $# -lt 2 && die "Missing value for the option: '$_key'" 1
_arg_checkbox_class="$2"
shift
;;
--checkbox-class=*)
_arg_checkbox_class="${_key##--checkbox-class=}"
;;
--radio-class=*)
_arg_checkbox_class="${_key##--radio-class=}"
;;
--radio-label-class|--checkbox-label-class)
test $# -lt 2 && die "Missing value for the option: '$_key'" 1
_arg_checkbox_label_class="$2"
shift
;;
--checkbox-label-class=*)
_arg_checkbox_label_class="${_key##--checkbox-label-class=}"
;;
--radio-label-class=*)
_arg_checkbox_label_class="${_key##--radio-label-class=}"
;;
-h|--help)
print_help;
exit 0;
;;
-h*)
print_help;
exit 0;
;;
-v|--version)
print_version;
exit 0;
;;
-v*)
print_version;
exit 0;
;;
--verbose)
if [ $# -lt 2 ];then
_verbose_level="$((_verbose_level + 1))";
else
_verbose_level="$2";
shift;
fi
;;
--quiet)
if [ $# -lt 2 ];then
_verbose_level="$((_verbose_level - 1))";
else
_verbose_level="-$2";
shift;
fi
;;
*)
_last_positional="$1"
_positionals+=("$_last_positional")
_positionals_count=$((_positionals_count + 1))
;;
esac
shift
done
}
handle_passed_args_count()
{
local _required_args_string="arg-name description"
if [ "${_positionals_count}" -gt 2 ] && [ "$_helpHasBeenPrinted" == "1" ];then
_PRINT_HELP=yes die "FATAL ERROR: There were spurious positional arguments --- we expect at most 2 (namely: $_required_args_string), but got ${_positionals_count} (the last one was: '${_last_positional}').\n\t${_positionals[*]}" 1
fi
if [ "${_positionals_count}" -lt 2 ] && [ "$_helpHasBeenPrinted" == "1" ];then
_PRINT_HELP=yes die "FATAL ERROR: Not enough positional arguments - we require at least 2 (namely: $_required_args_string), but got only ${_positionals_count}.
${_positionals[*]}" 1;
fi
}
assign_positional_args()
{
local _positional_name _shift_for=$1;
_positional_names="_arg_arg_name _arg_description ";
shift "$_shift_for"
for _positional_name in ${_positional_names};do
test $# -gt 0 || break;
eval "if [ \"\$_one_of${_positional_name}\" != \"\" ];then [[ \"\${_one_of${_positional_name}[*]}\" =~ \"\${1}\" ]];fi" || die "${_positional_name} must be one of: $(eval "echo \"\${_one_of${_positional_name}[*]}\"")" 1;
eval "$_positional_name=\${1}" || die "Error during argument parsing, possibly an ParseArger bug." 1;
shift;
done
}
print_debug()
{
print_help
# shellcheck disable=SC2145
echo "DEBUG: $0 $@";
echo -e " arg-name: ${_arg_arg_name}";
echo -e " description: ${_arg_description}";
echo -e " one-of: ${_arg_one_of[*]}";
echo -e " complete: ${_arg_complete[*]}";
echo -e " complete-custom: ${_arg_complete_custom[*]}";
echo -e " input-container-class: ${_arg_input_container_class}";
echo -e " input-class: ${_arg_input_class}";
echo -e " label-class: ${_arg_label_class}";
echo -e " select-class: ${_arg_select_class}";
echo -e " checkbox-container-class: ${_arg_checkbox_container_class}";
echo -e " checkbox-class: ${_arg_checkbox_class}";
echo -e " checkbox-label-class: ${_arg_checkbox_label_class}";
}
print_version()
{
echo "0.1";
}
on_interrupt() {
die Process aborted! 130;
}
parse_commandline "$@";
handle_passed_args_count;
assign_positional_args 1 "${_positionals[@]}";
trap on_interrupt INT;
# @parseArger-parsing-end
# print_debug "$@"
# @parseArger-end
inptCmn="pa-type=\"opt\" name=\"${_arg_arg_name}\" id=\"${_arg_arg_name}\"";
if [ "${#_arg_one_of[@]}" -gt 0 ]; then
inpuStr="<select class=\" class=\"${_arg_select_class}\"\" ${inptCmn}>\n";
for oof in "${_arg_one_of[@]}"; do
inpuStr+="\n\t\t<option value=\"${oof}\"";
if [ "${#_arg_default_value[@]}" -gt 0 ] && [ "${_arg_default_value[0]}" == "$oof" ]; then
inpuStr+=" selected";
fi
inpuStr+=">${oof}</option>";
done
inpuStr+="\n\t</select>";
# elif [ "${_arg_repeat}" == "on" ]; then
# inpuStr="<textarea ${inptCmn} type=\"text\" pa-repeat class=\"${_arg_input_class}\">";
# if [ "${#_arg_default_value[@]}" -gt 0 ]; then
# inpuStr+="${_arg_default_value[0]}";
# fi
# inpuStr+="</textarea>";
else
inpuStr="<input class=\"${_arg_input_class}\" ${inptCmn} type=\"text\"";
if [ "${#_arg_default_value[@]}" -gt 0 ]; then
inpuStr+=" value=\"${_arg_default_value[0]}\"";
fi
inpuStr+=" />";
fi
echo -e "<div class=\"${_arg_input_container_class}\">
<label for=\"${_arg_arg_name}\" class=\"${_arg_label_class}\">${_arg_arg_name}</label>
<span class=\"input-help\">${_arg_description}</span>
${inpuStr}
</div>";