-
Notifications
You must be signed in to change notification settings - Fork 1
/
wap.sh
297 lines (259 loc) · 8.06 KB
/
wap.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
#!/bin/bash
# shellcheck disable=SC2001
#WAP_SHOULD_EXIT=
#WAP_DEBUG=0
WAP_SPLIT_TERM='##'
error() {
(>&2 echo "$(tput setab 1)$*$(tput sgr0)")
}
debug() {
if (( WAP_DEBUG == 1 )); then
(>&2 echo "$(tput setab 2)[DEBUG] $*$(tput sgr0)")
fi
}
wonderful_argument_parser() {
# from the top, parse the signature
# shellcheck disable=SC2206
signature=($1);
# make it drop, so we can parse the rest
shift;
declare -a parsed_keys
declare -a parsed_vals
declare -a positional_args
declare -a optional_flag_args
declare -a optional_args
declare -a args
positional_index=0
optional_index=0
signature+=('[--help]') # This is always available
for x in "$@"; do
args+=("$x");
shift;
done
debug "Signature: ${signature[*]}"
for arg in "${signature[@]}"; do
if [[ "$arg" == '<'* ]]; then
# This is a required argument
positional_args+=("$(echo "$arg" | sed 's/^<//;s/>$//')")
elif [[ "$arg" == '[--'* ]]; then
# This is an optional argument
optional_flag_args+=("$(echo "$arg" | sed 's/^\[//;s/\]$//')")
elif [[ "$arg" == '['* ]]; then
# This is an optional argument
optional_args+=("$(echo "$arg" | sed 's/^\[//;s/\]$//')")
else
# This is an error
error "ERROR!!! Bad argument specification: $arg"
fi
done
# Size of the arrays
positional_count=${#positional_args[@]}
optional_count=${#optional_args[@]}
if (( WAP_DEBUG == 1 )); then
debug Positional args
for arg in "${positional_args[@]}"; do debug " $arg"; done;
debug Optional args
for arg in "${optional_args[@]}"; do debug " $arg"; done;
debug Optional flag args
for arg in "${optional_flag_args[@]}"; do debug " $arg"; done;
fi
# If the help flag is in there, we can short-circuit
if [[ "$x" == "--help" ]] || [[ "$x" == "-h" ]]; then
WAP_HELP=1
WAP_HELP_POSITIONAL=
for arg in "${positional_args[@]}"; do WAP_HELP_POSITIONAL="$WAP_HELP_POSITIONAL\n\t<$arg>"; done;
WAP_HELP_OPTIONAL_FLAGS=
for arg in "${optional_flag_args[@]}"; do WAP_HELP_OPTIONAL_FLAGS="$WAP_HELP_OPTIONAL_FLAGS\n\t[$arg]"; done;
WAP_HELP_OPTIONAL=
for arg in "${optional_args[@]}"; do WAP_HELP_OPTIONAL="$WAP_HELP_OPTIONAL\n\t[$arg]"; done;
return
fi
offset=0
# For every optional flag argument with a default value, we should set that.
for arg in "${optional_flag_args[@]}"; do
# Two types of args: with, without values
if [[ "$arg" == "--"*'='* ]]; then
key=$(echo "$arg" | sed 's/--//g;s/=.*//g')
val=$(echo "$arg" | sed 's/--//g;s/.*=//g')
# If it has <desc> then it's just a description, not a default.
if [[ "$val" != '<'*'>' ]]; then
parsed_keys+=("${key}")
parsed_vals+=("${val}")
fi
fi
done
while true; do
# Get the first bit of content from the arguments
a_cur=${args[$offset]}
if [[ "$a_cur" == "" ]]; then
break
fi
if [[ "$a_cur" == "--"* ]]; then
# This is a flag. So find the matching flag definition.
for arg in "${optional_flag_args[@]}"; do
# Two types of args: with, without values
if [[ "$arg" == *'='* ]]; then
# This has another argument.
# So we need to pop something else.
# And increment offset so we don't re-process it
valueless_arg=$(echo "$arg" | sed 's/=.*//')
valueless_acr=$(echo "$a_cur" | sed 's/=.*//')
if [[ "$valueless_arg" == "$valueless_acr" ]]; then
#echo "HI: a_cur=${a_cur} arg=${arg} valueless_arg=${valueless_arg} valueless_acr=${valueless_acr}"
if [[ "${a_cur}" == "${valueless_acr}"'='* ]]; then
val="$(echo "${a_cur}" | sed 's/.*=//g')"
else
val="${args[$offset+1]}"
offset=$(( offset + 1 ))
fi
k="$(echo "$valueless_acr" | sed 's/^--//;s/-/_/g')"
parsed_keys+=("${k}")
parsed_vals+=("$val")
debug "Parsed ${k} = ${val}"
fi
else
# This is just a flag
if [[ "$arg" == "$a_cur" ]]; then
k="$(echo "$a_cur" | sed 's/^--//;s/-/_/g')"
parsed_keys+=("${k}")
parsed_vals+=(1)
debug "Parsed ${k} = 1"
fi
fi
done
else
# This is a non-flag, so maybe a positional, maybe an optional argument.
# So we need to find the Nth positional (via positional_index)
if (( (positional_index + optional_index) >= (positional_count + optional_count) )); then
error "Error: more positional arguments than should be possible"
if [[ -z $WAP_SHOULD_EXIT ]]; then
exit 1;
else
return;
fi
fi
if (( positional_index < positional_count )); then
key_fixed=$(echo "${positional_args[$positional_index]}" | sed 's/|.*//g')
parsed_keys+=("$key_fixed")
parsed_vals+=("${a_cur}")
positional_index=$((positional_index + 1))
else
# We're past the positional and into the optional
k="${optional_args[$optional_index]}"
if [[ "$k" == *'='* ]]; then
# Here there is a default supplied.
#k="${k//=.*//}"
k=$(echo "$k" | sed 's/=.*//g')
fi
parsed_keys+=("$(echo "$k" | sed 's/|.*//g')")
parsed_vals+=("${a_cur}")
optional_index=$(( optional_index + 1 ))
fi
debug "Parsed ${parsed_keys[*]} = ${parsed_vals[*]}"
fi
offset=$(( offset + 1 ))
done
# Set all default optional args, if they aren't set
if (( optional_index < optional_count )); then
for i in $(seq $optional_index $((optional_count - 1)) ); do
if [[ "${optional_args[$i]}" == *'='* ]]; then
parsed_keys+=("$(echo "$optional_args" | sed 's/=.*//g')")
parsed_vals+=("$(echo "$optional_args" | sed 's/.*=//g')")
fi
done
fi
if (( positional_index < positional_count )); then
for i in $(seq $positional_index $(( positional_count - 1 )) ); do
error "Required argument <${positional_args[$i]}> is missing"
done
if [[ -z $WAP_SHOULD_EXIT ]]; then
exit 1;
else
return;
fi
fi
size=${#parsed_keys[@]}
for i in $(seq 0 $((size - 1))); do
debug "$(printf "SETTING\t%10s=%-10s\n" "${parsed_keys[$i]}" "${parsed_vals[$i]}")"
# shellcheck disable=SC2086
export arg_${parsed_keys[$i]}="${parsed_vals[$i]}"
done
}
wap_help() {
if (( WAP_HELP == 1 )); then
echo "HELP [$wap_subcommand $wap_fn]"
echo
cat - | fold -w 80 | sed 's/^/\t/'
echo
echo -e "Positional Arguments"
echo -e "$WAP_HELP_POSITIONAL"
echo
echo -e "Optional Arguments"
echo -e "$WAP_HELP_OPTIONAL"
echo -e "$WAP_HELP_OPTIONAL_FLAGS"
exit 1
fi
}
wap_debug_available_args() {
#echo "Here are the available arguments:"
for x in $(compgen -v | grep ^arg_ | sort); do
echo "${x} = ${!x}"
done
}
wap_fn_signature() {
grep "${1}()" "$0" | sed "s/.*${WAP_SPLIT_TERM} //g;s/: .*//"
}
# TODO: update with gxadmin version of function discovery.
wapify() {
# Discover functions
wap_fn=$1; shift;
LC_ALL=C type "$wap_fn" 2> /dev/null | grep -q 'function'
ec=$?
if (( ec != 0 )); then
echo "Available commands:"
echo
grep '()\s*{\s*'"${WAP_SPLIT_TERM}" "$0" | sed -r 's/\(\).*:\s*/\t/g;s/^/\t/' | column -t -s" "| sort
exit 1
fi
wonderful_argument_parser "$(wap_fn_signature "$wap_fn")" "$@"
$wap_fn
}
wapify_subcommands_help() {
subcommand_groups=()
while IFS='' read -r line; do subcommand_groups+=("$line"); done < \
<(grep '()\s*{\s*'"${WAP_SPLIT_TERM}" "$0" | sed 's/().*//g' | sort | cut -d _ -f 1 | uniq)
echo "$0 usage:"
echo
for group in "${subcommand_groups[@]}"; do
help_text="_${group}_help"
echo -e " $group\t${!help_text}"
done
echo
exit 0
}
wapify_subcommands() {
# Discover subcommand groups
subcommand_groups=()
while IFS='' read -r line; do subcommand_groups+=("$line"); done < \
<(grep '()\s*{\s*'"${WAP_SPLIT_TERM}" "$0" | sed 's/().*//g' | sort | cut -d _ -f 1 | uniq)
if [[ "$1" == "--help" ]] || (( $# == 0 )); then
wapify_subcommands_help
fi
# Parse out the subcommand
wap_subcommand=$1;
shift;
# Subcommand help
if (( $# == 0 )) || [[ "$1" == "--help" ]]; then
echo "Available functions in $0 $wap_subcommand:"
echo
grep "$wap_subcommand"'.*()\s*{\s*'"${WAP_SPLIT_TERM}" "$0" | sed -r "s/\(\).*:\s*/\t/g;s/^/\t/;s/${wap_fn}_/${wap_fn} /" | column -t -s" "| sort
echo
exit 0
fi
wap_fn=$1;
shift;
debug "${wap_subcommand}_${wap_fn}"
debug "$(wap_fn_signature "${wap_subcommand}_${wap_fn}")"
wonderful_argument_parser "$(wap_fn_signature "${wap_subcommand}_${wap_fn}")" "$@"
"${wap_subcommand}_${wap_fn}"
}