-
Notifications
You must be signed in to change notification settings - Fork 3
/
FunctionStarter.sh
executable file
·391 lines (308 loc) · 12.6 KB
/
FunctionStarter.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
#!/bin/bash
function usage()
{
cat << _EOT_
FunctionStarter
-------------------- author: xshoji
This script generates a template of bash function.
Usage:
./$(basename "$0") --naming scriptName [ --description Description --required paramName,sample,description --optional paramName,sample,description,defaultValue(omittable) --flag flagName,description ]
Required:
-n, --naming scriptName : Script name.
Optional:
-d, --description Description : Description of this script. [ example: --description "ScriptStarter's description here." ]
-r, --required paramName,sample,description : Required parameter setting. [ example: --required id,1001,"Primary id here." ]
-o, --optional paramName,sample,description,defaultValue(omittable) : Optional parameter setting. [ example: --option name,xshoji,"User name here.",defaultUser ]
-f, --flag flagName,description : Optional flag parameter setting. [ example: --flag dryRun,"Dry run mode." ]
--debug : Enable debug mode
_EOT_
[[ "${1+x}" != "" ]] && { exit "${1}"; }
exit 1
}
function printColored() { local B="\033[0;"; local C=""; case "${1}" in "red") C="31m";; "green") C="32m";; "yellow") C="33m";; "blue") C="34m";; esac; printf "%b%b\033[0m" "${B}${C}" "${2}"; }
# Parse parameters
ARGS_REQUIRED=()
ARGS_OPTIONAL=()
ARGS_FLAG=()
ARGS_DESCRIPTION=()
for ARG in "$@"
do
SHIFT="true"
{ [[ "${ARG}" == "--debug" ]]; } && { shift 1; set -eux; SHIFT="false"; }
{ [[ "${ARG}" == "--naming" ]] || [[ "${ARG}" == "-n" ]]; } && { shift 1; NAMING="${1}"; SHIFT="false"; }
{ [[ "${ARG}" == "--required" ]] || [[ "${ARG}" == "-r" ]]; } && { shift 1; ARGS_REQUIRED+=("${1}"); SHIFT="false"; }
{ [[ "${ARG}" == "--optional" ]] || [[ "${ARG}" == "-o" ]]; } && { shift 1; ARGS_OPTIONAL+=("${1}"); SHIFT="false"; }
{ [[ "${ARG}" == "--flag" ]] || [[ "${ARG}" == "-f" ]]; } && { shift 1; ARGS_FLAG+=("${1}"); SHIFT="false"; }
{ [[ "${ARG}" == "--description" ]] || [[ "${ARG}" == "-d" ]]; } && { shift 1; ARGS_DESCRIPTION+=("${1}"); SHIFT="false"; }
{ [[ "${SHIFT}" == "true" ]] && [[ "$#" -gt 0 ]]; } && { shift 1; }
done
[[ -n "${HELP+x}" ]] && { usage 0; }
# Check required parameters
[[ -z "${NAMING+x}" ]] && { printColored yellow "[!] --naming is required.\n"; INVALID_STATE="true"; }
# Check invalid state and display usage
[[ -n "${INVALID_STATE+x}" ]] && { usage; }
# Initialize optional variables
[[ -z "${DESCRIPTION+x}" ]] && { DESCRIPTION=""; }
FUNCTION_NAME="${NAMING}"
# Define constant variable
PROVISIONAL_STRING=$(openssl rand -hex 12 | fold -w 12 | head -1)
#==========================================
# Functions
#==========================================
function parseValue() {
echo "${1}" |sed "s/\\\,/${PROVISIONAL_STRING}/g" |awk -F',' '{print $'"${2}"'}' |sed "s/${PROVISIONAL_STRING}/,/g"
}
function toVarName() {
local PARAM_NAME="${1}"
echo "${PARAM_NAME}" | perl -pe 's/(?:^|_|-)(.)/\U$1/g' | perl -ne 'print lc(join("_", split(/(?=[A-Z])/)))' |awk '{print toupper($1)}'
}
function printFunctionDocumentBase() {
echo "#######################################"
local HAS_DESCRIPTION=""
for ARG in "$@"
do
HAS_DESCRIPTION="true"
echo "# ${ARG}"
done
[ "${HAS_DESCRIPTION}" == "true" ] && { echo "#"; }
echo "# Usage:"
}
function printUsageExecutionExample() {
local PARAM_NAME
local SAMPLE
# Add required parameters
for ARG in "$@"
do
PARAM_NAME=$(parseValue "${1}" 1)
SAMPLE=$(parseValue "${1}" 2)
echo -n ' '"--"${PARAM_NAME}" ${SAMPLE}"
shift 1
done
}
function printUsageExecutionExampleFlag() {
local PARAM_NAME
# Add required parameters
for ARG in "$@"
do
PARAM_NAME=$(parseValue "${1}" 1)
echo -n ' '"--"${PARAM_NAME}""
shift 1
done
}
function printParameterDescription() {
local PARAM_NAME
local SAMPLE
local DESCRIPTION
local PARAM_NAME_SHORT
local IS_USED_SHORT_PARAM
for ARG in "$@"
do
# - [csv - Printing column separated by comma using Awk command line - Stack Overflow](https://stackoverflow.com/questions/26842504/printing-column-separated-by-comma-using-awk-command-line)
PARAM_NAME=$(parseValue "${1}" 1)
SAMPLE=$(parseValue "${1}" 2)
DESCRIPTION=$(parseValue "${1}" 3)
PARAM_NAME_SHORT=$(cut -c 1 <<<"${PARAM_NAME}")
[ "${SAMPLE}" == "" ] && { SAMPLE="${PARAM_NAME}"; }
[ "${DESCRIPTION}" == "" ] && { DESCRIPTION="${SAMPLE} means "${PARAM_NAME}"."; }
IS_USED_SHORT_PARAM=$(grep "${PARAM_NAME_SHORT}" <<<$(echo ${ARGS_SHORT[@]+"${ARGS_SHORT[@]}"}) || true)
echo -n "# --"${PARAM_NAME}""
# if [ "${SHORT}" == "true" ] && [ "${IS_USED_SHORT_PARAM}" == "" ]; then
# ARGS_SHORT+=("${PARAM_NAME_SHORT}")
# echo -n ",-${PARAM_NAME_SHORT}"
# fi
echo " ${SAMPLE} : ${DESCRIPTION}"
shift 1
done
}
function printParameterDescriptionFlag() {
local PARAM_NAME
local PARAM_NAME_SHORT
local DESCRIPTION
local IS_USED_SHORT_PARAM
for ARG in "$@"
do
PARAM_NAME=$(parseValue "${1}" 1)
PARAM_NAME_SHORT=$(parseValue "${1}" 2)
DESCRIPTION=$(parseValue "${1}" 3)
[ "${DESCRIPTION}" == "" ] && { DESCRIPTION="Enable "${PARAM_NAME}" flag."; }
IS_USED_SHORT_PARAM=$(grep "${PARAM_NAME_SHORT}" <<<$(echo ${ARGS_SHORT[@]+"${ARGS_SHORT[@]}"}) || true)
echo -n "# --"${PARAM_NAME}""
# if [ "${SHORT}" == "true" ] && [ "${IS_USED_SHORT_PARAM}" == "" ]; then
# ARGS_SHORT+=("${PARAM_NAME_SHORT}")
# echo -n ",-${PARAM_NAME_SHORT}"
# fi
echo " : ${DESCRIPTION}"
shift 1
done
}
function printFunctionDocumentBottomPart() {
echo "#######################################"
}
function printFunctionTopPart() {
echo "function ${1}() {"
}
function printLocalDeclarationArgument() {
local PARAM_NAME
local VAR_NAME
for ARG in "$@"
do
PARAM_NAME=$(parseValue "${1}" 1)
VAR_NAME=$(toVarName "${PARAM_NAME}")
echo -n ' local '"${VAR_NAME}"'="";'
shift 1
done
}
function printParseArgument() {
local PARAM_NAME
local PARAM_NAME_SHORT
local VAR_NAME
local CONDITION
local IS_USED_SHORT_PARAM=$(grep "1${PARAM_NAME_SHORT}" <<<$(echo ${ARGS_SHORT[@]+"${ARGS_SHORT[@]}"}) || true)
for ARG in "$@"
do
PARAM_NAME=$(parseValue "${1}" 1)
PARAM_NAME_SHORT=$(cut -c 1 <<<${1})
VAR_NAME=$(toVarName "${PARAM_NAME}")
CONDITION='[ "${_ARG}" == "--'""${PARAM_NAME}""'" ]'
IS_USED_SHORT_PARAM=$(grep "1${PARAM_NAME_SHORT}" <<<$(echo ${ARGS_SHORT[@]+"${ARGS_SHORT[@]}"}) || true)
# if [ "${SHORT}" == "true" ] && [ "${IS_USED_SHORT_PARAM}" == "" ]; then
# ARGS_SHORT+=("1${PARAM_NAME_SHORT}")
# CONDITION='('"${CONDITION}"' || [ "${_ARG}" == "-'"${PARAM_NAME_SHORT}"'" ])'
# fi
echo -n "${CONDITION}"' && { shift 1; '"${VAR_NAME}"'="${1}"; _SHIFT="false"; }; '
shift 1
done
}
function printParseArgumentFlag() {
local PARAM_NAME
local PARAM_NAME_SHORT
local VAR_NAME
local CONDITION
for ARG in "$@"
do
PARAM_NAME=$(parseValue "${1}" 1)
PARAM_NAME_SHORT=$(cut -c 1 <<<${1})
VAR_NAME=$(toVarName "${PARAM_NAME}")
CONDITION='[ "${_ARG}" == "--'""${PARAM_NAME}""'" ]'
IS_USED_SHORT_PARAM=$(grep "1${PARAM_NAME_SHORT}" <<<$(echo ${ARGS_SHORT[@]+"${ARGS_SHORT[@]}"}) || true)
# if [ "${SHORT}" == "true" ] && [ "${IS_USED_SHORT_PARAM}" == "" ]; then
# ARGS_SHORT+=("1${PARAM_NAME_SHORT}")
# CONDITION='('"${CONDITION}"' || [ "${_ARG}" == "-'"${PARAM_NAME_SHORT}"'" ])'
# fi
echo -n "${CONDITION}"' && { shift 1; '"${VAR_NAME}"'="true"; _SHIFT="false"; }; '
shift 1
done
}
function printCheckRequiredArgument() {
local PARAM_NAME
local VAR_NAME
for ARG in "$@"
do
PARAM_NAME=$(parseValue "${1}" 1)
VAR_NAME=$(toVarName "${PARAM_NAME}")
echo ' [ "${'"${VAR_NAME}"'}" == "" ] && { echo "[!] '${FUNCTION_NAME}'() requires --'""${PARAM_NAME}""' "; _INVALID_STATE="true"; }'
shift 1
done
}
function printVariableRequired() {
local PARAM_NAME
local VAR_NAME
echo ' echo " Required arguments"'
for ARG in "$@"
do
PARAM_NAME=$(parseValue "${1}" 1)
VAR_NAME=$(toVarName "${PARAM_NAME}")
echo ' echo " - '""${PARAM_NAME}""': ${'"${VAR_NAME}"'}"'
shift 1
done
}
function printVariableOptional() {
local PARAM_NAME
local VAR_NAME
echo ' echo " Optional arguments"'
for ARG in "$@"
do
PARAM_NAME=$(parseValue "${1}" 1)
VAR_NAME=$(toVarName "${PARAM_NAME}")
echo ' echo " - '""${PARAM_NAME}""': ${'"${VAR_NAME}"'}"'
shift 1
done
}
#==========================================
# Main
#==========================================
HAS_REQUIRED="false"
HAS_OPTION="false"
HAS_FLAG="false"
HAS_OPTION_OR_FLAG="false"
[[ ${#ARGS_REQUIRED[@]} -gt 0 ]] && { HAS_REQUIRED="true"; }
[[ ${#ARGS_OPTIONAL[@]} -gt 0 ]] && { HAS_OPTION="true"; }
[[ ${#ARGS_FLAG[@]} -gt 0 ]] && { HAS_FLAG="true"; }
{ [[ "${HAS_OPTION}" == "true" ]] || [[ "${HAS_FLAG}" == "true" ]]; } && { HAS_OPTION_OR_FLAG="true"; }
# Print usage example
printFunctionDocumentBase ${ARGS_DESCRIPTION[@]+"${ARGS_DESCRIPTION[@]}"}
echo -n "# ${FUNCTION_NAME}"
# - [Bash empty array expansion with `set -u` - Stack Overflow](https://stackoverflow.com/questions/7577052/bash-empty-array-expansion-with-set-u)
printUsageExecutionExample ${ARGS_REQUIRED[@]+"${ARGS_REQUIRED[@]}"}
[[ "${HAS_OPTION_OR_FLAG}" == "true" ]] && { echo -n " ["; }
printUsageExecutionExample ${ARGS_OPTIONAL[@]+"${ARGS_OPTIONAL[@]}"}
printUsageExecutionExampleFlag ${ARGS_FLAG[@]+"${ARGS_FLAG[@]}"}
[[ "${HAS_OPTION_OR_FLAG}" == "true" ]] && { echo -n " ]"; }
echo ""
echo "# "
if [[ "${HAS_REQUIRED}" == "true" ]]; then
echo "# Required arguments:"
printParameterDescription "${ARGS_REQUIRED[@]}"
echo "# "
fi
echo "# Optional arguments:"
if [[ "${HAS_OPTION_OR_FLAG}" == "true" ]]; then
printParameterDescription ${ARGS_OPTIONAL[@]+"${ARGS_OPTIONAL[@]}"}
printParameterDescriptionFlag ${ARGS_FLAG[@]+"${ARGS_FLAG[@]}"}
fi
printFunctionDocumentBottomPart
printFunctionTopPart "${FUNCTION_NAME}"
cat << "__EOT__"
# Argument parsing
local _ARG=""; local _SHIFT=""; local _INVALID_STATE=""
__EOT__
printLocalDeclarationArgument ${ARGS_REQUIRED[@]+"${ARGS_REQUIRED[@]}"}
printLocalDeclarationArgument ${ARGS_OPTIONAL[@]+"${ARGS_OPTIONAL[@]}"}
printLocalDeclarationArgument ${ARGS_FLAG[@]+"${ARGS_FLAG[@]}"}
echo
echo -n " for _ARG in \"\$@\"; do local _SHIFT=\"true\"; "
printParseArgument ${ARGS_REQUIRED[@]+"${ARGS_REQUIRED[@]}"}
printParseArgument ${ARGS_OPTIONAL[@]+"${ARGS_OPTIONAL[@]}"}
printParseArgumentFlag ${ARGS_FLAG[@]+"${ARGS_FLAG[@]}"}
echo "([ \"\${_SHIFT}\" == \"true\" ] && [ \"\$#\" -gt 0 ]) && { shift 1; }; done"
[[ "${HAS_REQUIRED}" == "true" ]] && { echo " # Check required arguments"; }
printCheckRequiredArgument ${ARGS_REQUIRED[@]+"${ARGS_REQUIRED[@]}"}
# Check invalid state
echo " # Check invalid state"
echo " [ \"\${_INVALID_STATE}\" == \"true\" ] && { exit 1; }"
echo " "
echo " # Main"
if [[ "${HAS_REQUIRED}" == "true" || "${HAS_OPTION_OR_FLAG}" == "true" ]] ; then
echo ' echo ""'
echo ' echo "'${FUNCTION_NAME}'()"'
REQUIRED_EOT="true"
fi
if [[ "${HAS_REQUIRED}" == "true" ]]; then
printVariableRequired "${ARGS_REQUIRED[@]}"
fi
if [[ "${HAS_OPTION_OR_FLAG}" == "true" ]]; then
printVariableOptional ${ARGS_OPTIONAL[@]+"${ARGS_OPTIONAL[@]}"} ${ARGS_FLAG[@]+"${ARGS_FLAG[@]}"}
fi
[[ -n "${REQUIRED_EOT+x}" ]] && { echo ' echo ""'; }
echo "}"
# STARTER_URL=https://raw.githubusercontent.com/xshoji/bash-script-starter/master/ScriptStarter.sh
# curl -sf ${STARTER_URL} |bash -s - \
# -n FunctionStarter \
# -a xshoji \
# -d "This script generates a template of bash function." \
# -r naming,scriptName,"Script name." \
# -o description,"Description","Description of this script. [ example: --description \"ScriptStarter's description here.\" ]" \
# -o required,"paramName\,sample\,description","Required parameter setting. [ example: --required id\,1001\,\"Primary id here.\" ]" \
# -o optional,"paramName\,sample\,description\,defaultValue(omittable)","Optional parameter setting. [ example: --option name\,xshoji\,\"User name here.\"\,defaultUser ]" \
# -o flag,"flagName\,description","Optional flag parameter setting. [ example: --flag dryRun\,\"Dry run mode.\" ]" \
# -s > /tmp/test.sh; open /tmp/test.sh