-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathopts-parser.sh
115 lines (89 loc) · 2.92 KB
/
opts-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
SUPPORTED_OPTS=""
SHORT_OPTS=""
MERGEABLE_OPTS=""
KEEP_OPTS=true
## OPTIONS PARSING 6 ###########################################################
if [[ $# -eq 0 ]] ; then
main
exit $?
fi
unset opt optn optm optv optt optk
optv="$*" ; optt=()
while [[ -n "$1" ]] ; do
if [[ "$1" =~ \ && -n "$optn" ]] ; then
declare "$optn=$1"
unset optn && shift && continue
elif [[ $1 =~ ^-{1}[a-zA-Z0-9]{1,2}+.*$ ]] ; then
optm=${1:1}
if [[ \ $SHORT_OPTS\ =~ \ $optm:!?([a-zA-Z0-9_]*) ]] ; then
opt="${BASH_REMATCH[1]}"
else
declare -F showOptWarn &>/dev/null && showOptWarn "-$optm"
shift && continue
fi
if [[ -z "$optn" ]] ; then
optn=$opt
else
# shellcheck disable=SC2015
[[ -z "$optk" ]] && ( declare -F showOptValWarn &>/dev/null && showOptValWarn "--$optn" ) || declare "$optn=true"
optn=$opt
fi
if [[ ! $SUPPORTED_OPTS\ =~ !?$optn\ ]] ; then
declare -F showOptWarn &>/dev/null && showOptWarn "-$optm"
shift && continue
fi
if [[ ${BASH_REMATCH[0]:0:1} == "!" ]] ; then
declare "$optn=true" ; unset optn ; optk=true
else
unset optk
fi
shift && continue
elif [[ "$1" =~ ^-{2}[a-zA-Z]{1}[a-zA-Z0-9_-]+.*$ ]] ; then
opt=${1:2}
if [[ $opt == *=* ]] ; then
IFS="=" read -ra opt <<< "$opt"
optm="${opt[0]}" ; optm=${optm//-/_}
if [[ ! $SUPPORTED_OPTS\ =~ $optm\ ]] ; then
declare -F showOptWarn &>/dev/null && showOptWarn "--${opt[0]//_/-}"
shift && continue
fi
# shellcheck disable=SC2015
[[ -n "${!optm}" && $MERGEABLE_OPTS\ =~ $optm\ ]] && declare "$optm=${!optm} ${opt[*]:1:99}" || declare "$optm=${opt[*]:1:99}"
unset optm && shift && continue
else
# shellcheck disable=SC2178
opt=${opt//-/_}
if [[ -z "$optn" ]] ; then
# shellcheck disable=SC2128
optn=$opt
else
# shellcheck disable=SC2015
[[ -z "$optk" ]] && ( declare -F showOptValWarn &>/dev/null && showOptValWarn "--$optn" ) || declare "$optn=true"
# shellcheck disable=SC2128
optn=$opt
fi
if [[ ! $SUPPORTED_OPTS\ =~ !?$optn\ ]] ; then
declare -F showOptWarn &>/dev/null && showOptWarn "--${optn//_/-}"
shift && continue
fi
if [[ ${BASH_REMATCH[0]:0:1} == "!" ]] ; then
declare "$optn=true" ; unset optn ; optk=true
else
unset optk
fi
shift && continue
fi
else
if [[ -n "$optn" ]] ; then
# shellcheck disable=SC2015
[[ -n "${!optn}" && $MERGEABLE_OPTS\ =~ $optn\ ]] && declare "$optn=${!optn} $1" || declare "$optn=$1"
unset optn && shift && continue
fi
fi
optt+=("$1") ; shift
done
[[ -n "$optn" ]] && declare "$optn=true"
unset opt optn optm optk
# shellcheck disable=SC2015,SC2086
[[ -n "$KEEP_OPTS" ]] && main $optv || main "${optt[@]}"
################################################################################