-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser.sh
315 lines (276 loc) · 8.52 KB
/
parser.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
# shellcheck shell=bash disable=SC2148
# PA - PArser
# _PA holds data specific to parsing the command line arguments.
declare -A _PA
# Declare externally defined variables ----------------------------------------
# Define globals
OK=0
ERROR=1
STDERR=/dev/stderr
STOP=2
# Getters/Setters -------------------------------------------------------------
# PA_command getter outputs the _PA[command] array member. This contains the
# command the user requested.
PA_command() {
printf '%s' "${_PA[command]}"
}
# PA_subcommand getter outputs the _PA[subcommand] array member. This contains
# the subcommand the user requested.
PA_subcommand() {
printf '%s' "${_PA[subcommand]}"
}
# PA_shift outputs the _PA[shift] array member, returned by the caller
# when an extra shift is required whilst consuming option values.
#
# Example code:
# ```
#XX_process_options_callback() {
#
# case "$1" in
# -h | --help)
# CC_usage
# return "${STOP}"
# ;;
# ... omitted ...
# --k8sver)
# _CC[k8sver]="$2"
# return "$(PA_shift)"
# ;;
# --with-lb)
# ... omitted ..
# ```
PA_shift() {
printf '%s' "${_PA[shift]}"
}
# PA_set_state setter sets the initial state of the parser, which should be one
# of COMMAND, SUBCOMMAND, or ARG1.
# Args: arg1 - the initial state to set.
PA_set_state() {
_PA[state]="$1"
}
# Public Functions ------------------------------------------------------------
# PA_add_option_callback adds a callback to the list of callbacks used for
# processing options.
# Args: arg1 - Null string (for global options), COMMAND or COMMANDSUBCOMMAND.
# arg2 - The function to call.
PA_add_option_callback() {
_PA[optscallbacks]+="$1,$2 "
}
# PA_add_usage_callback adds a callback to the list of callbacks used for
# output of help text.
# Args: arg1 - Null string (for global help), COMMAND or COMMANDSUBCOMMAND.
# arg2 - The function to call.
PA_add_usage_callback() {
_PA[usagecallbacks]+="$1,$2 "
}
# PA_add_state adds a callback to the list of callbacks used for
# programming the state machine.
# Args: arg1 - Current state to match.
# arg2 - The value of the state to match.
# arg3 - The new state if arg1 and arg2 match.
# arg4 - The function to call, optional.
PA_add_state() {
_PA[statecallbacks]+="$1,$2,$3,$4 "
}
# PA_add_run_callback adds a callback to the list of callbacks used for
# running the user defined entrypoint function.
# Args: arg1 - The value of the state to match. Probably like:
# "commandsubcommand" "function"
# arg2 - The function to call.
PA_add_run_callback() {
_PA[runcallbacks]+="$1,$2 "
}
# PA_run implements an interleaved state machine to process the
# user request. It allows for strict checking of arguments and args. All
# command line arguments are processed in order from left to right.
#
# Each COMMAND can have a different set of requirements which are controlled
# by setting the next state at each transition.
#
# --global-options COMMAND --command-options SUBCOMMAND --subcommand-options \
# ARG1 --subcommand-options ARG2 --subcommand-options ...
#
# --global-options are those before COMMAND.
# --command-options can be after the COMMAND but before SUBCOMMAND.
# --subcommand-options can be anywhere after the SUBCOMMAND.
#
# Args: arg1-N - The arguments given to mok by the user on the command line
PA_run() {
set -- "$@"
local arg ARGN=$# retval=${OK}
declare -a expanded_args
# Expand args like -abc to -a -b -c
while [ "$ARGN" -ne 0 ]; do
arg="$1"
if [[ $arg == -[a-zA-Z][a-zA-Z]* ]]; then
# Remove the leading dash
arg="${arg#-}"
# Split the remaining characters and add dashes
for i in ${arg//[a-zA-Z]/ -&}; do
expanded_args+=("$i")
done
else
expanded_args+=("$arg")
fi
ARGN=$((ARGN-1))
shift 1
done
set -- "${expanded_args[@]}"
local ARGN=$# ARGNUM=0
while [ "${ARGN}" -ne 0 ]; do
case "$1" in
--* | -*)
_PA_process_option "$1" "$2" || {
retval=$?
if [[ ${retval} == "${_PA[shift]}" ]]; then
ARGN=$((ARGN - 1))
shift
else
return "${retval}"
fi
}
;;
*)
case "${_PA[state]}" in
COMMAND)
_PA_check_token "${1}" "COMMAND" "command"
[[ $? -eq ${ERROR} ]] && {
PA_usage
printf '\nInvalid COMMAND, "%s".\n' "$1" >"${STDERR}"
return "${ERROR}"
}
;;
SUBCOMMAND)
_PA_check_token "$1" "SUBCOMMAND" "subcommand"
[[ $? -eq ${ERROR} ]] && {
PA_usage
printf '\nInvalid SUBCOMMAND for %s, "%s".\n' "${_PA[command]}" "${1}" \
>"${STDERR}"
return "${ERROR}"
}
;;
ARG*)
((ARGNUM++))
_PA_check_token "${1}" "ARG${ARGNUM}"
[[ $? -eq ${ERROR} ]] && {
PA_usage
printf '\nInvalid ARG1 for %s %s, "%s".\n' "${_PA[command]}" \
"${_PA[subcommand]}" "${1}" >"${STDERR}"
return "${ERROR}"
}
;;
END)
PA_usage
printf '\nERROR No more args expected, "%s" is unexpected for "%s %s"\n' \
"${1}" "${_PA[command]}" "${_PA[subcommand]}" >"${STDERR}"
return "${ERROR}"
;;
*)
printf 'Internal ERROR. Invalid state "%s"\n' "${_PA[state]}" >"${STDERR}"
return "${ERROR}"
;;
esac
;;
esac
shift 1
ARGN=$((ARGN - 1))
done
# Run the user defined entrypoint function:
for item in ${_PA[runcallbacks]}; do
IFS=, read -r stateval func <<<"${item}"
[[ ${stateval} == "${_PA[command]}${_PA[subcommand]}" ]] && {
eval "${func}" || retval=$?
return "${retval}"
}
done
PA_usage
return "${OK}"
}
# PA_usage outputs help text for a single component if help was asked for when
# a command was specified, or for all components otherwise.
# Args: None expected.
PA_usage() {
curcmdsubcmd="${_PA[command]}${_PA[subcommand]}"
for item in ${_PA[usagecallbacks]}; do
IFS=, read -r cmdsubcmd func <<<"${item}"
[[ ${curcmdsubcmd} == "${cmdsubcmd}" ]] && {
eval "${func}"
return
}
done
eval "${_PA[usage]}"
}
# Private Functions -----------------------------------------------------------
# PA_new sets the initial values for the PArser's associative array.
# Args: None expected.
_PA_new() {
_PA[command]=
_PA[subcommand]=
_PA[state]="COMMAND"
_PA[optscallbacks]=
_PA[usagecallbacks]=
_PA[statecallbacks]=
_PA[runcallbacks]=
# The return value if the caller asked for an extra shift:
_PA[shift]=126
}
# _PA_check_token checks for a valid token in arg2 state. The logic is
# most easily understood by reading the full original version at:
# https://github.com/mclarkson/my-own-kind/blob/master/docs/package.md#scripted-cluster-creation-and-deletion
# This function is a reduction of all the check_xxx_token functions.
# Args: arg1 - the token to check.
# arg2 - the current state.
# arg3 - the state value to set, optional. This should only be sent
# for command and subcommand states.
_PA_check_token() {
local item runcmd=0
if [[ -n ${_PA[subcommand]} ]]; then
cmdsubcommand="${_PA[command]}${_PA[subcommand]}"
elif [[ -n ${_PA[command]} && ${_PA[state]} != "ARG"* ]]; then
cmdsubcommand="${_PA[command]}$1"
elif [[ -n ${_PA[command]} && ${_PA[state]} == "ARG"* ]]; then
cmdsubcommand="${_PA[command]}"
elif [[ -z ${_PA[command]} && ${_PA[state]} == "ARG"* ]]; then
cmdsubcommand=
else
cmdsubcommand="$1"
fi
for item in ${_PA[statecallbacks]}; do
IFS=, read -r state component newstate func <<<"${item}"
[[ ${state} == "$2" ]] && {
[[ ${component} == "${cmdsubcommand}" ]] && {
[[ -n $3 ]] && _PA["$3"]="$1"
_PA[state]="${newstate}"
[[ -n ${func} ]] && {
runcmd=1
eval "${func} $1" || return
}
return "${OK}"
}
}
done
[[ ${runcmd} -eq 0 ]] && {
return "${ERROR}"
}
}
# _PA_process_option checks that the user-provided option is valid for the
# command-subcommand or global states.
# Args: arg1 - The option to check.
# arg2 - TODO The value of the option if present, optional.
_PA_process_option() {
local item curcmdsubcmd retval
curcmdsubcmd="${_PA[command]}${_PA[subcommand]}"
for item in ${_PA[optscallbacks]}; do
IFS=, read -r cmdsubcmd func <<<"${item}"
[[ ${curcmdsubcmd} == "${cmdsubcmd}" ]] && {
eval "${func} \"$1\" \"$2\""
retval=$?
[[ ${retval} -eq "${STOP}" ]] && exit 0
return "${retval}"
}
done
return "${ERROR}"
}
# Initialise _PA
_PA_new
# vim:ft=sh:sw=2:et:ts=2: