-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathshArg.sh
executable file
·208 lines (182 loc) · 4.92 KB
/
shArg.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
#!/usr/bin/env bash
declare -A _SH_ARGUMENTS=()
declare -A _SH_SWITCHES=()
declare -A _SH_TYPES=()
declare -A _SH_AUTOS=()
declare -A _SH_HOOK_FUNCTIONS=()
declare AUTO_EXPORTS=true
shArgs.arg(){
local variableName=$1
local shortName=$2
local longName=$3
local argType=$4
local auto=$5
local hookFunction=$6
if [ ! -z "$shortName" ]; then
_SH_SWITCHES[$shortName]=$variableName
fi
if [ ! -z "$longName" ]; then
_SH_SWITCHES[$longName]=$variableName
fi
if [ -n "$argType" ]; then
isHook=true
case $argType in
"PARAMETER"|"FLAG"|"true"|"false")
isHook=false ;;
esac
if [ "$isHook" == "false" ]; then
_SH_TYPES[$variableName]=$argType
else
_SH_HOOK_FUNCTIONS[$variableName]=$argType
fi
else
_SH_TYPES[$variableName]="PARAMETER"
fi
if [ "$argType" == "FLAG" ]; then
_SH_ARGUMENTS[$variableName]=false
else
_SH_ARGUMENTS[$variableName]=""
fi
if [ "$auto" == false ]; then
_SH_AUTOS[$variableName]=false
else
_SH_AUTOS[$variableName]=true
fi
if [ ! -z "$hookFunction" ]; then
_SH_HOOK_FUNCTIONS[$variableName]=$hookFunction
fi
}
_strindex() {
x="${1%%$2*}"
[[ "$x" = "$1" ]] && echo -1 || echo "${#x}"
}
_processFlag() {
local _varName=$1
local autoExport=$2
_SH_ARGUMENTS[$_varName]=true
if [ "$autoExport" == true ]; then
eval "$_varName=true"
fi
}
_processParameter() {
local _varValue=1
local value=$2
local autoExport=$3
if [[ "$value" == *","* ]]; then
local _listInput=$value
local arr
BFS=$IFS
IFS=, read -a arr <<<"${_listInput}"
IFS=$BFS
listData="${arr[@]}"
_varValue=$listData
_SH_ARGUMENTS[$_varName]="$listData"
else
_varValue=$value
fi
_SH_ARGUMENTS[$_varName]=$_varValue
if [ "$autoExport" == true ]; then
existing=$(eval "echo \$$_varName")
if [ -n "$existing" ]; then
_warn "autoexported variable \"$_varName\" contains a value and will be overridden by shArg"
fi
eval "$_varName=\"$_varValue\""
fi
}
_processSplitData() {
local name=$1
local value=$2
local _varName
local argType
local autoExport
_varName=${_SH_SWITCHES[$name]}
if [ ! -z "$_varName" ]; then
argType=${_SH_TYPES[$_varName]}
autoExport="$AUTO_EXPORTS"
hookFunction=${_SH_HOOK_FUNCTIONS[$_varName]}
if [ -z "$value" ]; then
_processFlag "$_varName" "$autoExport"
else
_processParameter "$_varName" "$value" "$autoExport"
fi
if [ ! -z "$hookFunction" ]; then
eval "$hookFunction \"${_SH_ARGUMENTS[$_varName]}\""
fi
else
_warn "Unknown argument $name"
fi
}
_trim() {
local value=$1
result=$(echo "$value" | xargs)
echo $result
}
_processLine(){
local input_string=$1
local firstChar="${input_string:0:1}"
if [ "$firstChar" != "-" ]; then
return 0 # not a switch exit
fi
local size=${#input_string}
local spaceIndex=$(_strindex "$input_string" " ")
local name=${input_string:0:spaceIndex}
local value=${input_string:spaceIndex+1:size}
local valueSize=${#input_string}
value=$(_trim "$value")
if [ "$spaceIndex" == "-1" ]; then
name=$value
value=""
fi
if [[ "$name" == *"="* ]]; then
name=${input_string:0:size}
BFS=$IFS
IFS='='
read -ra arr <<<"$name"
IFS=$BFS
name=${arr[0]}
value=${arr[1]}
value=$(_trim "$value")
fi
_processSplitData "$name" "$value"
}
shArgs.parse(){
local input_string=$@
local char=""
local tmp=""
local previousChar=""
for (( i=0; i<${#input_string}; i++ )); do
char=${input_string:$i:1}
if [ "$char" == "-" ]; then
if [ "$i" -gt "0" ]; then
previousChar=${input_string:$i-1:1}
if [ "$previousChar" == " " ]; then
if [ ! -z "$tmp" ]; then
_processLine "$tmp"
tmp=""
fi
if [ "${input_string:$i:2}" == "--" ]; then
tmp+="$char"
i=$((i+1))
fi
fi
fi
fi
tmp+="$char"
done
if [ ! -z "$tmp" ]; then
_processLine "$tmp"
tmp=""
fi
}
shArgs.val(){
echo ${_SH_ARGUMENTS[$1]}
}
# display functions
_warn(){
if [ -z "$SHARG_DISABLE_WARNINGS" ]; then
echo -e "\033[33m@shArg Warning: $1\033[0m"
fi
}
shArgs.disableAutoExport(){
AUTO_EXPORTS=false
}