Skip to content

Commit 95574c0

Browse files
committed
[update] : Re-wrote with getopt
1 parent f20d694 commit 95574c0

File tree

1 file changed

+85
-67
lines changed

1 file changed

+85
-67
lines changed

tools/msg.sh

Lines changed: 85 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/usr/bin/env bash
22

3-
set -e
3+
set -eu
44

55
script_path="$( cd -P "$( dirname "$(readlink -f "$0")" )" && cd .. && pwd )"
66

@@ -101,96 +101,114 @@ msg_error() {
101101
bash "${script_path}/tools/msg.sh" -a "msg.sh" error "${1}"
102102
}
103103

104+
# Check color
105+
# Usage check_color <str>
106+
check_color(){
107+
case "${2}" in
108+
"black" | "red" | "green" | "yellow" | "blue" | "magenta" | "cyan" | "white")
109+
return 0
110+
;;
111+
*)
112+
return 1
113+
;;
114+
esac
115+
}
116+
117+
ARGUMENT=("${@}")
118+
OPTS="a:c:l:no:p:r:s:t:xh"
119+
OPTL="nocolor,bash-debug,help,nolabel,noappname,noadjust"
120+
if ! OPT=($(getopt -o ${OPTS} -l ${OPTL} -- "${ARGUMENT[@]}")); then
121+
exit 1
122+
fi
104123

105-
while getopts "a:c:l:no:p:r:s:t:xh-:" arg; do
106-
case "${arg}" in
107-
a)
108-
appname="${OPTARG}"
124+
eval set -- "${OPT[@]}"
125+
unset OPT OPTS OPTL
126+
127+
while true; do
128+
case "${1}" in
129+
-a)
130+
appname="${2}"
131+
shift 2
109132
;;
110-
c)
111-
adjust_chr="${OPTARG}"
133+
-c)
134+
adjust_chr="${2}"
135+
shift 2
112136
;;
113-
l)
137+
-l)
114138
customized_label=true
115-
msg_label="${OPTARG}"
139+
msg_label="${2}"
140+
shift 2
116141
;;
117-
n)
142+
-n | --nocolor)
118143
nocolor=true
144+
shift 1
119145
;;
120-
o)
121-
echo_opts="${OPTARG}"
146+
-o)
147+
echo_opts="${2}"
148+
shift 2
122149
;;
123-
p)
124-
output="${OPTARG}"
150+
-p)
151+
output="${2}"
125152
customized_output=true
153+
shift 2
126154
;;
127-
r)
155+
-r)
128156
customized_label_color=true
129-
case "${OPTARG}" in
130-
"black" | "red" | "green" | "yellow" | "blue" | "magenta" | "cyan" | "white")
131-
labelcolor="${OPTARG}"
132-
;;
133-
*)
134-
msg_error "The wrong color."
135-
exit 1
136-
;;
137-
esac
157+
if check_color "${2}"; then
158+
labelcolor="${2}"
159+
else
160+
msg_error "The wrong color."
161+
exit 1
162+
fi
163+
shift 2
138164
;;
139-
s)
140-
label_space="${OPTARG}"
165+
-s)
166+
label_space="${2}"
167+
shift 2
141168
;;
142-
t)
169+
-t)
143170
customized_text_color=true
144-
case "${OPTARG}" in
145-
"black" | "red" | "green" | "yellow" | "blue" | "magenta" | "cyan" | "white")
146-
textcolor="${OPTARG}"
147-
;;
148-
*)
149-
msg_error "The wrong color."
150-
exit 1
151-
;;
152-
esac
171+
if check_color "${2}"; then
172+
textcolor="${2}"
173+
else
174+
msg_error "The wrong color."
175+
exit 1
176+
fi
177+
shift 2
153178
;;
154-
x)
179+
-x | --bash_debug)
155180
bash_debug=true
156181
set -xv
182+
shift 1
157183
;;
158-
h)
184+
-h | --help)
159185
_help
160186
shift 1
161187
exit 0
162188
;;
163-
-)
164-
case "${OPTARG}" in
165-
"nocolor")
166-
nocolor=true
167-
;;
168-
"bash-debug")
169-
bash_debug=true
170-
set -xv
171-
;;
172-
"help")
173-
_help
174-
exit 0
175-
;;
176-
"nolabel")
177-
nolabel=true
178-
;;
179-
"noappname")
180-
noappname=true
181-
;;
182-
"noadjust")
183-
noadjust=true
184-
;;
185-
*)
186-
_help
187-
exit 1
188-
;;
189-
esac
190-
esac
189+
--nolabel)
190+
nolabel=true
191+
shift 1
192+
;;
193+
--noappname)
194+
noappname=true
195+
shift 1
196+
;;
197+
--noadjust)
198+
noadjust=true
199+
shift 1
200+
;;
201+
--)
202+
shift 1
203+
break
204+
;;
205+
*)
206+
_help
207+
exit 1
208+
;;
209+
esac
191210
done
192211

193-
shift "$((OPTIND - 1))"
194212

195213
# Color echo
196214
#

0 commit comments

Comments
 (0)