-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path_shared_lib
More file actions
140 lines (114 loc) · 2.58 KB
/
_shared_lib
File metadata and controls
140 lines (114 loc) · 2.58 KB
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
#
# https://misc.flogisoft.com/bash/tip_colors_and_formatting
#
TERM_NORMAL='\e[0m'
TERM_BOLD='\e[1m'
TERM_DIM='\e[2m'
TERM_UNDERLINE='\e[4m'
TERM_BLINK='\e[5m'
TERM_FOREGROUND_DEFAULT='\e[39m'
TERM_FOREGROUND_RED='\e[31m'
TERM_FOREGROUND_GREEN='\e[32m'
TERM_FOREGROUND_YELLOW='\e[33m'
TERM_FOREGROUND_BLUE='\e[34m'
TERM_FOREGROUND_MAGENTA='\e[35m'
TERM_FOREGROUND_CYAN='\e[36m'
TERM_FOREGROUND_NORMAL=$TERM_FOREGROUND_DEFAULT
TERM_FOREGROUND_WARNING=$TERM_FOREGROUND_YELLOW
TERM_FOREGROUND_ERROR=$TERM_FOREGROUND_RED
#
# show help
#
function showhelp () {
desc=$1; shift
optline=$1; shift
opttext=$1; shift
cat <<END
$_progname: $desc
Version: $_ver
Author: $_author
$_progname $optline
$opttext
END
}
#
# echo with verbosity threshold
#
function vecho () {
local level=${1:-0}
(( _verbose > level )) && {
cat - | while IFS= read -r line; do
echo -e $line
done
}
}
#
# Exit with error code and message.
#
function die () {
>/dev/stderr cat -
exit ${1:-1}
}
#
# Parsing command-line arguments.
#
function my_getopt () {
short_opt="${1}hv"; shift
long_opt="${1},help,verbose"; shift
cont=$1; shift # name to a function to continue processing options.
desc=$1; shift
optline=$(cat <<END
$1
[-h|--help] [-v|--verbose]
[--] [<additional args>]
END
); shift
opttext=$(cat <<END
$1
-h --help Help, and you are reading it.
-v --verbose Increase verbosity for each use.
END
); shift
# !!! _getopt is intentionally LEAKED out of this function.
_getopt=$(getopt -n "$_progname" -o "$short_opt" -l "$long_opt" -- "$@")
if [[ $? -ne 0 ]]; then
showhelp "$desc" "$optline" "$opttext"
die 1 <<END
Error in parsing options.
END
fi
eval set -- "$_getopt"
local n_shift=0
while true; do
case "$1" in
-h|--help)
showhelp "$desc" "$optline" "$opttext"
shift; ((n_shift++))
exit 0
;;
-v|--verbose)
((_verbose++))
shift; ((n_shift++))
continue
;;
--)
shift; ((n_shift++))
break
;;
esac
"$cont" "$@" # return the number of shifts to do next
ret=$?
shift $ret; ((n_shift+=ret))
done
return $n_shift
}
#
# dump parameters (for sanity check)
#
function dumpparam () {
cat -
echo
for name in LKHH_DIR LKHH_BIN LKHH_ARENA LKHH_IMAGE LKHH_LINUX; do
echo "export $name=$(echo $(eval "echo \${$name}"))"
done
}