Skip to content

Commit 0b4180c

Browse files
committed
refactor: unify error message format and refactor related functions ℹ️
- keep `usage` function simple - use `-s`/`-h` option for optional argument of `die` function - use bash builtin `type -P` instead of `which` command 🐚 - rename var, use `COLOR_INDEX` instead of `COUNT`
1 parent 52f6a07 commit 0b4180c

File tree

16 files changed

+297
-196
lines changed

16 files changed

+297
-196
lines changed

bin/a2l

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ readonly args
7272
################################################################################
7373

7474
readonly -a ROTATE_COLORS=(33 35 36 31 32 37 34)
75-
COUNT=0
75+
COLOR_INDEX=0
7676
rotateColorPrint() {
7777
local content=$*
7878
# - if stdout is a terminal, turn on color output.
@@ -82,7 +82,7 @@ rotateColorPrint() {
8282
if [[ ! -t 1 || $content =~ ^[[:space:]]*$ ]]; then
8383
printf '%s\n' "$content"
8484
else
85-
local color=${ROTATE_COLORS[COUNT++ % ${#ROTATE_COLORS[@]}]}
85+
local color=${ROTATE_COLORS[COLOR_INDEX++ % ${#ROTATE_COLORS[@]}]}
8686
printf '\e[1;%sm%s\e[0m\n' "$color" "$content"
8787
fi
8888
}

bin/ap

Lines changed: 33 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -19,23 +19,43 @@ readonly PROG_VERSION='2.x-dev'
1919
# util functions
2020
################################################################################
2121

22-
redPrint() {
22+
errorMsgPrint() {
23+
local errorMsg="$PROG: $*"
2324
# if stdout is a terminal, turn on color output.
2425
# '-t' check: is a terminal?
2526
# check isatty in bash https://stackoverflow.com/questions/10022323
2627
if [ -t 1 ]; then
27-
printf '\e[1;31m%s\e[0m\n' "$*"
28+
printf '\e[1;31m%s\e[0m\n' "$errorMsg"
2829
else
29-
printf '%s\n' "$*"
30+
printf '%s\n' "$errorMsg"
3031
fi
31-
}
32+
} >&2
3233

3334
die() {
34-
redPrint "Error: $*" >&2
35-
exit 1
36-
}
37-
38-
# `realpath` command existed on Linux and macOS, return resolved physical path
35+
local prompt_help=false exit_status=2
36+
while (($# > 0)); do
37+
case "$1" in
38+
-h)
39+
prompt_help=true
40+
shift
41+
;;
42+
-s)
43+
exit_status=$2
44+
shift 2
45+
;;
46+
*)
47+
break
48+
;;
49+
esac
50+
done
51+
52+
(($# > 0)) && errorMsgPrint "$*"
53+
$prompt_help && echo "Try '$PROG --help' for more information."
54+
55+
exit "$exit_status"
56+
} >&2
57+
58+
# `realpath` command exists on Linux and macOS, return resolved physical path
3959
# - realpath command on macOS do NOT support option `-e`;
4060
# combined `[ -e $file ]` to check file existence first.
4161
# - How can I get the behavior of GNU's readlink -f on a Mac?
@@ -45,14 +65,7 @@ realpath() {
4565
}
4666

4767
usage() {
48-
local -r exit_code=${1:-0}
49-
(($# > 0)) && shift
50-
local -r out=$(((exit_code != 0) + 1))
51-
52-
# NOTE: $'foo' is the escape sequence syntax of bash
53-
(($# > 0)) && redPrint "$*"$'\n' >&"$out"
54-
55-
cat >&"$out" <<EOF
68+
cat <<EOF
5669
Usage: $PROG [OPTION]... [FILE]...
5770
convert to Absolute Path.
5871
@@ -65,7 +78,7 @@ Options:
6578
-V, --version display version information and exit
6679
EOF
6780

68-
exit "$exit_code"
81+
exit
6982
}
7083

7184
progVersion() {
@@ -92,7 +105,7 @@ while (($# > 0)); do
92105
break
93106
;;
94107
-*)
95-
usage 2 "$PROG: unrecognized option '$1'"
108+
die -h "unrecognized option '$1'"
96109
;;
97110
*)
98111
# if not option, treat all follow files as args
@@ -113,8 +126,8 @@ has_error=false
113126

114127
for f in "${files[@]}"; do
115128
realpath "$f" || {
116-
redPrint "error: $f does not exists!" >&2
117129
has_error=true
130+
errorMsgPrint "$f: No such file or directory!"
118131
}
119132
done
120133

bin/c

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,25 +18,43 @@ readonly PROG_VERSION='2.x-dev'
1818
# util functions
1919
################################################################################
2020

21-
printErrorMsg() {
21+
redPrint() {
2222
# if stdout is a terminal, turn on color output.
2323
# '-t' check: is a terminal?
2424
# check isatty in bash https://stackoverflow.com/questions/10022323
2525
if [ -t 1 ]; then
26-
printf '\e[1;31m%s\e[0m\n\n' "Error: $*"
26+
printf '\e[1;31m%s\e[0m\n' "$*"
2727
else
28-
printf '%s\n\n' "Error: $*"
28+
printf '%s\n' "$*"
2929
fi
3030
}
3131

32-
usage() {
33-
local -r exit_code=${1:-0}
34-
(($# > 0)) && shift
35-
local -r out=$(((exit_code != 0) + 1))
36-
37-
(($# > 0)) && printErrorMsg "$*" >&"$out"
32+
die() {
33+
local prompt_help=false exit_status=2
34+
while (($# > 0)); do
35+
case "$1" in
36+
-h)
37+
prompt_help=true
38+
shift
39+
;;
40+
-s)
41+
exit_status=$2
42+
shift 2
43+
;;
44+
*)
45+
break
46+
;;
47+
esac
48+
done
49+
50+
(($# > 0)) && redPrint "$PROG: $*"
51+
$prompt_help && echo "Try '$PROG --help' for more information."
52+
53+
exit "$exit_status"
54+
} >&2
3855

39-
cat >&"$out" <<EOF
56+
usage() {
57+
cat <<EOF
4058
Usage: $PROG [OPTION]... [command [command_args ...]]
4159
Run command and put output to system clipper.
4260
If no command is specified, read from stdin(pipe).
@@ -53,7 +71,7 @@ Options:
5371
-V, --version display version information and exit
5472
EOF
5573

56-
exit "$exit_code"
74+
exit
5775
}
5876

5977
progVersion() {
@@ -90,7 +108,7 @@ while (($# > 0)); do
90108
break
91109
;;
92110
-*)
93-
usage 2 "unrecognized option '$1'"
111+
die -h "unrecognized option '$1'"
94112
;;
95113
*)
96114
# if not option, treat all follow arguments as command

bin/coat

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,6 @@ readonly PROG_VERSION='2.x-dev'
1818
# parse options
1919
################################################################################
2020

21-
progVersion() {
22-
printf '%s version: %s\n' "$PROG" "$PROG_VERSION"
23-
printf 'cat executable: %s\n' "$(command -v cat)"
24-
exit
25-
}
26-
2721
usage() {
2822
cat <<EOF
2923
Usage: $PROG [OPTION]... [FILE]...
@@ -34,12 +28,18 @@ Support options:
3428
--version output version information and exit
3529
All other options and arguments are delegated to command cat,
3630
more info see the help/man of command cat(e.g. cat --help).
37-
cat executable: $(command -v cat)
31+
cat executable: $(type -P cat)
3832
EOF
3933

4034
exit
4135
}
4236

37+
progVersion() {
38+
printf '%s version: %s\n' "$PROG" "$PROG_VERSION"
39+
printf 'cat executable: %s\n' "$(type -P cat)"
40+
exit
41+
}
42+
4343
args=("$@")
4444
# check arguments in reverse, so last option wins.
4545
for ((idx = $# - 1; idx >= 0; --idx)); do
@@ -58,15 +58,15 @@ unset args idx
5858
[ -t 1 ] || exec cat "$@"
5959

6060
readonly -a ROTATE_COLORS=(33 35 36 31 32 37 34)
61-
COUNT=0
61+
COLOR_INDEX=0
6262
# CAUTION: print content WITHOUT new line
6363
rotateColorPrint() {
6464
local content=$*
6565
# skip color for white space
6666
if [[ $content =~ ^[[:space:]]*$ ]]; then
6767
printf %s "$content"
6868
else
69-
local color=${ROTATE_COLORS[COUNT++ % ${#ROTATE_COLORS[@]}]}
69+
local color=${ROTATE_COLORS[COLOR_INDEX++ % ${#ROTATE_COLORS[@]}]}
7070
printf '\e[1;%sm%s\e[0m' "$color" "$content"
7171
fi
7272
}

bin/cp-into-docker-run

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,34 @@ redPrint() {
2828
}
2929

3030
die() {
31-
redPrint "Error: $*" >&2
32-
exit 1
33-
}
31+
local prompt_help=false exit_staus=2
32+
while (($# > 0)); do
33+
case "$1" in
34+
-h)
35+
prompt_help=true
36+
shift
37+
;;
38+
-s)
39+
exit_staus=$2
40+
shift 2
41+
;;
42+
*)
43+
break
44+
;;
45+
esac
46+
done
47+
48+
(($# > 0)) && redPrint "$PROG: $*"
49+
$prompt_help && echo "Try '$PROG --help' for more information."
50+
51+
exit "$exit_staus"
52+
} >&2
3453

3554
isAbsolutePath() {
3655
[[ "$1" =~ ^/ ]]
3756
}
3857

39-
# `realpath` command existed on Linux and macOS, return resolved physical path
58+
# `realpath` command exists on Linux and macOS, return resolved physical path
4059
# - realpath command on macOS do NOT support option `-e`;
4160
# combined `[ -e $file ]` to check file existence first.
4261
# - How can I get the behavior of GNU's readlink -f on a Mac?
@@ -46,14 +65,7 @@ realpath() {
4665
}
4766

4867
usage() {
49-
local -r exit_code=${1:-0}
50-
(($# > 0)) && shift
51-
local -r out=$(((exit_code != 0) + 1))
52-
53-
# NOTE: $'foo' is the escape sequence syntax of bash
54-
(($# > 0)) && redPrint "$*"$'\n' >&"$out"
55-
56-
cat >&"$out" <<EOF
68+
cat <<EOF
5769
Usage: $PROG [OPTION]... command [command-args]...
5870
5971
Copy the command into docker container
@@ -82,7 +94,7 @@ miscellaneous:
8294
-V, --version display version information and exit
8395
EOF
8496

85-
exit "$exit_code"
97+
exit
8698
}
8799

88100
progVersion() {
@@ -140,7 +152,7 @@ while (($# > 0)); do
140152
break
141153
;;
142154
-*)
143-
usage 2 "$PROG: unrecognized option '$1'"
155+
die -h "unrecognized option '$1'"
144156
;;
145157
*)
146158
# if not option, treat all follow arguments as command
@@ -153,7 +165,7 @@ done
153165
readonly container_name docker_user docker_workdir docker_tmpdir docker_command_cp_path verbose args
154166

155167
[ -n "$container_name" ] ||
156-
usage 1 "No destination docker container name, specified by option -c/--container!"
168+
die -h "requires destination docker container name, specified by option -c/--container!"
157169

158170
if [ -n "$docker_workdir" ]; then
159171
isAbsolutePath "$docker_workdir" ||
@@ -171,7 +183,7 @@ fi
171183
# check docker command existence
172184
########################################
173185

174-
command -v docker &>/dev/null || die 'docker command not found!'
186+
type -P docker &>/dev/null || die 'docker command not found!'
175187

176188
########################################
177189
# prepare vars for docker operation

0 commit comments

Comments
 (0)