-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVirtCmmdInterface.sh
441 lines (440 loc) · 17.6 KB
/
VirtCmmdInterface.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
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
#!/bin/bash
###############################################################################
##
## Section: Abstract Interface:
## Defines an abstract command processing interface and
## for now, couple it do a default implementation.
##
###############################################################################
##
###############################################################################
##
## Purpose:
## Define a generic command processing framework. Framework outline:
## 1. Obtain configuration settings.
## 2. Parse command line arguments.
## 3. Determine if call for help (give precedence to --help option above
## all otheres).
## 4. When help requested, display the command's help text.
## 5. Determine if call for version info (give precedence to --version
## option after --help but before all others).
## 6. When version requested, display the command's version text.
## 7. Verify the arguments passed to the command.
## 8. Execute the command.
##
## Input:
## $1 - Variable name containing the submitted arguments/options for a
## given command. To create this variable see: "ArgumentsMainInclude.sh"
##
## Output:
## When Failure:
## A return code of 1.
## SYSERR - One or more messages prefixed with either "Error:" or "Abort:"
## explaining the reason for the failure.
##
###############################################################################
function main () {
local -r mainArgListNm="$1"
if ! VirtCmmdConfigSet; then
ScriptUnwind "$LINENO" "Command context could not be properly established"
fi
local -a mainArgList
local -A mainArgMap
if ! VirtCmmdArgumentsParse "$mainArgListNm" 'mainArgList' 'mainArgMap'; then
ScriptUnwind "$LINENO" "Parsing command line options failed."
fi
if ! VirtCmmdOptionHelpVerify 'mainArgList' 'mainArgMap'; then
ScriptUnwind "$LINENO" "Command options/arguments invalid. Try 'help'."
fi
if VirtCmmdHelpIsDisplay 'mainArgList' 'mainArgMap'; then
VirtCmmdHelpDisplay
return 0
fi
if ! VirtCmmdOptionVersionVerify 'mainArgList' 'mainArgMap'; then
ScriptUnwind "$LINENO" "Command options/arguments invalid. Try 'help'."
fi
if VirtCmmdVersionIsDisplay 'mainArgList' 'mainArgMap'; then
VirtCmmdVersionDisplay
return 0
fi
if ! VirtCmmdOptionsArgsVerify 'mainArgList' 'mainArgMap'; then
ScriptUnwind "$LINENO" "Command options/arguments invalid. Try 'help'."
fi
if ! VirtCmmdExecute 'mainArgList' 'mainArgMap'; then
ScriptUnwind "$LINENO" "Problem occurred while executing command"
fi
}
################################################################################
##
## Purpose:
## To establish a configuration interface used to load a script's
## statically, from the perspective of the running script, defined
## execution enviroment. For example, loading a series of bash environment
## variables. The default implementation can be overridden by redefining
## the bash function in the script file that includes it.
##
## This implementation will first attempt to load a script's execution
## environment by running yet another script, assigned the same name but
## located in directory/symbolic link named "config" that's subordinate
## to the directory containing the script that's running which included
## this one. If this file isn't found, then a function that loads
## a "default" environment is executed.
##
## Input:
## $0 - Name of running script that included this configuration interface.
##
## Output:
## When Failure:
## SYSERR - Reflects reason for failure.
##
#################################################################################
function VirtCmmdConfigSet (){
local -r script_dir=$(dirname "${BASH_SOURCE[0]}")
local -r script_name=$(basename "$0")
local -r script_config="$script_dir/config/$script_name"
if [ -e "$script_config" ]; then
"$script_config"
else
VirtCmmdConfigSetDefault
fi
return
}
###############################################################################
##
## Purpose:
## Establish a "default" configuration environment for the running script.
##
## Input:
## $0 - Name of running script that included this configuration interface.
##
## Output:
## When Failure:
## SYSERR - Reflect message indicating reason for error
##
#################################################################################
function VirtCmmdConfigSetDefault (){
ScriptUnwind "$LINENO" "Please override '$FUNCNAME'"
}
###############################################################################
##
## Purpose:
## Parse command line options and arguments into an array and an associative
## array. The standard array contains the option labels and a generated
## argument label name. The ordering of theses labels in the array
## reflects its position specified on the command line. The associative
## is keyed by these label names and its values reflect the values assigned
## to the option/arguments.
##
## Assumption:
## Since bash variable names are passed to this routine, these names
## cannot overlap the variable names locally declared within the
## scope of this routine or its decendents.
##
## Input:
## $1 - Variable name whose value contains the command's options and
## specified on the command line arguments.
## $2 - Variable name to an array whose values will contain label names
## of the options and agruments appearing on the command line in the
## order specified by it. An option label is simply the option while
## arguments other than options are assigned labels 'Arg<N>' where
## <N> represents the order in which the argument was encountered
## starting with 1.
## $3 - Variable name to an associative array whose key is either the
## option or argument label and whose value represents the value
## associated to that label.
##
## Output:
## When Successful:
## $2 - This array variable contains entries of all the option/argument
## labels submitted on the command line.
## $3 - This associative array contains the values associated to
## each label.
## When Failure:
## SYSERR - Identifies the reason for failure.
##
###############################################################################
function VirtCmmdArgumentsParse () {
ArgumentsParse "$1" "$2" "$3"
}
###############################################################################
##
## Purpose:
## Validate the help option on the command line, if entered, without
## considering the other command options.
##
## Assumption:
## Since bash variable names are passed to this routine, these names
## cannot overlap the variable names locally declared within the
## scope of this routine or its decendents.
##
## Input:
## $1 - Variable name to an array whose values contain label names
## of the options and agruments appearing on the command line in the
## order specified by it. An option label is simply the option while
## arguments other than options are assigned labels 'Arg<N>' where
## <N> represents the order in which the argument was encountered
## starting with 1.
## $2 - Variable name to an associative array whose key is either the
## option or argument label and whose value represents the value
## associated to that label.
##
## Output:
## When Failure:
## SYSERR - Identifies the reason for failure.
##
###############################################################################
function VirtCmmdOptionHelpVerify () {
OptionsArgsVerify 'VirtCmmdOptionHelpDef' "$1" "$2"
}
VirtCmmdOptionHelpDef () {
cat <<OPTIONARGS_HELP
--help single false=EXIST=true "OptionsArgsBooleanVerify \\<--help\\>" optional "-h -help"
--Ignore-Unknown-OptArgs single --Ignore-Unknown-OptArgs "" optional ""
OPTIONARGS_HELP
return 0
}
###############################################################################
##
## Purpose:
## Examine command line options/arguments to ensure reasonable values
## were provided.
##
## Assumption:
## Since bash variable names are passed to this routine, these names
## cannot overlap the variable names locally declared within the
## scope of this routine or its decendents.
##
## Input:
## $1 - Variable name to an array whose values contain the label names
## of the options and agruments appearing on the command line in the
## order specified by it.
## $2 - Variable name to an associative array whose key is either the
## option or argument label and whose value represents the value
## associated to that label.
## 'VirtCmmdOptionsArgsDef' - A callback function that supplies a table
## containing constraint information used, for example, to
## verify the values of the arguments/options.
##
## Output:
## When Successful:
## All the arguments/options passes a "sniff' test.
## When Failure:
## SYSERR - Contains a message that specifically indicates why the
## option/argument failed its verification.
##
###############################################################################
function VirtCmmdOptionsArgsVerify () {
OptionsArgsVerify 'VirtCmmdOptionsArgsDef' "$1" "$2"
}
###############################################################################
##
## Purpose:
## Provides an interface to obtain a list of the options/arguments accepted
## by the command in question.
##
## Output:
## When Successful:
## SYSOUT - Each argument's/option's constraint information is written
## as a separate line out.
##
###############################################################################
function VirtCmmdOptionsArgsDef () {
# optArgName cardinality default verifyFunction presence
cat <<OPTIONARGS
Arg1 single Error "OptionsArgsMessageIssue \'Please override VirtCmmdOptionsArgsDef.\'" required
OPTIONARGS
return 1
}
##############################################################################
##
## Purpose:
## Determine if command specific help should be generated instead of
## running the command.
##
## Input:
## $1 - Variable name to an array whose values contain the label names
## of the options and agruments appearing on the command line in the
## order specified by it.
## $2 - Variable name to an associative array whose key is either the
## option or argument label and whose value represents the value
## associated to that label.
##
## Output:
## When Successful:
## Indicates request to display help for running command.
## When Failure:
## Indicates absence of request to display help.
##
###############################################################################
function VirtCmmdHelpIsDisplay () {
if AssociativeMapKeyExist "$2" '--help'; then
eval local -r helpIndValue=\"\$\{$2\[\'\-\-help\'\]\}\"
return `eval $helpIndValue`;
fi
return 1;
}
##############################################################################
##
## Purpose:
## To generate 'standard' help documention for this given command. Help
## documentation succinctly describes the command's prupose, format,
## option label names, their values, expected arguments and perhaps
## examples.
##
## Input:
## $1 - Variable name to an array whose values contain the label names
## of the options and agruments appearing on the command line in the
## order specified by it.
## $2 - Variable name to an associative array whose key is either the
## option or argument label and whose value represents the value
## associated to that label.
##
## Output:
## When Successful:
## SYSOUT - Provides help text.
## When Failure:
## SYSERR - Reflects reason for failure.
##
###############################################################################
function VirtCmmdHelpDisplay () {
ScriptUnwind "$LINENO" "Please override '$FUNCNAME'"
}
##############################################################################
##
## Purpose:
## Determine if command specific help should be generated instead of
## running the command.
##
## Input:
## $1 - Variable name to an array whose values contain the label names
## of the options and agruments appearing on the command line in the
## order specified by it.
## $2 - Variable name to an associative array whose key is either the
## option or argument label and whose value represents the value
## associated to that label.
##
## Output:
## When Successful:
## Indicates request to display help for running command.
## When Failure:
## Indicates absence of request to display help.
##
###############################################################################
function VirtCmmdVersionIsDisplay () {
if AssociativeMapKeyExist "$2" '--version'; then
eval local -r versionIndValue=\"\$\{$2\[\'\-\-version\'\]\}\"
return `eval $versionIndValue`;
fi
return 1;
}
###############################################################################
##
## Purpose:
## Validate the version option on the command line, if entered, without
## considering the other command options.
##
## Assumption:
## Since bash variable names are passed to this routine, these names
## cannot overlap the variable names locally declared within the
## scope of this routine or its decendents.
##
## Input:
## $1 - Variable name to an array whose values contain label names
## of the options and agruments appearing on the command line in the
## order specified by it. An option label is simply the option while
## arguments other than options are assigned labels 'Arg<N>' where
## <N> represents the order in which the argument was encountered
## starting with 1.
## $2 - Variable name to an associative array whose key is either the
## option or argument label and whose value represents the value
## associated to that label.
##
## Output:
## When Failure:
## SYSERR - Identifies the reason for failure.
##
###############################################################################
function VirtCmmdOptionVersionVerify () {
OptionsArgsVerify 'VirtCmmdOptionVersionDef' "$1" "$2"
}
VirtCmmdOptionVersionDef () {
cat <<OPTIONARGS_VERSION
--version single false=EXIST=true "OptionsArgsBooleanVerify \\<--version\\>" optional "-version --ver -ver"
--Ignore-Unknown-OptArgs single --Ignore-Unknown-OptArgs "" optional ""
OPTIONARGS_VERSION
return 0
}
##############################################################################
##
## Purpose:
## Generate standard version documention for given command. Version
## documentation provides component version number, licensing, and
## link to issue reporting.
##
## Input:
## $1 - Variable name to an array whose values contain the label names
## of the options and agruments appearing on the command line in the
## order specified by it.
## $2 - Variable name to an associative array whose key is either the
## option or argument label and whose value represents the value
## associated to that label.
##
## Output:
## When Successful:
## SYSOUT - Provides version text.
## When Failure:
## SYSERR - Reflects reason for failure.
##
###############################################################################
function VirtCmmdVersionDisplay () {
ScriptUnwind "$LINENO" "Please override '$FUNCNAME'"
}
##############################################################################
##
## Purpose:
## Execute the command's implementation.
##
## Assumption:
## Since bash variable names are passed to this routine, these names
## cannot overlap the variable names locally declared within the
## scope of this routine or its decendents.
##
## Input:
## $1 - Variable name to an array whose values contain the label names
## of the options and agruments appearing on the command line in the
## order specified by it.
## $2 - Variable name to an associative array whose key is either the
## option or argument label and whose value represents the value
## associated to that label.
##
## Output:
## When failure:
## SYSERR - Reflects error message.
##
###############################################################################
function VirtCmmdExecute () {
ScriptUnwind "$LINENO" "Please override '$FUNCNAME'"
}
###############################################################################
#
# The MIT License (MIT)
# Copyright (c) 2014-2015 Richard Moyse [email protected]
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#
###############################################################################