forked from timeweb/rebrain-bash
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbonus.sh
More file actions
181 lines (166 loc) · 4.59 KB
/
bonus.sh
File metadata and controls
181 lines (166 loc) · 4.59 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
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
#!/usr/bin/env bash
set -o errexit
print_help() {
cat << EOF
Usage:
$0 [--help] [--foo=<value>] [--bar=<value>...] [-f] [-b]
[--multi <value>...] [<command>] [<arguments>]
Commands:
foobar some subcommand.
Options:
--foo=<value> proc foo option.
--bar=<value>... proc bar option with multiple values.
-f proc f flag.
-b proc b flag.
--multi <value>... proc multivalue option.
--help print this message and exit.
EOF
exit 0
}
get_optarg() {
if [[ "$1" =~ .+=.+ ]]; then
opt="${1%%=*}"; arg="${1#*=}"; sft=1
elif [[ ! "$1" =~ .+=$ ]] && \
[ "$2" ] && [ "${2:0:1}" != '-' ]
then
opt="$1"; arg="$2"; sft=2
else
opt="$1"
if [[ "$1" =~ .+=$ ]]; then opt="${1:0: -1}"; fi
echo "$0: Missing argument for: $opt" >&2
exit 1
fi
}
spin() {
ppid="$1"
message="$2"
delay=.1
chars="/-\|*"
tput civis # Hide cursor.
while [ -d /proc/$ppid ]
do
for (( i=0; i<${#chars}; i++ )); do
sleep "$delay"
echo -en "[ ${chars:$i:1} ] $message" "\r"
done
done
echo -e "[ Done ] $message"
tput cnorm # Bring cursor back.
}
foobar() {
local help="
Usage:
$0 foobar [--help] [<command>] [<options>]
Commands and options:
spin <time> spin amount time in seconds.
--help show this message and exit.
"
if [[ ! "$@" ]];then
echo "$help"; exit 0
fi
while (( "$#" ))
do
case "$1" in
spin)
sp=1; # spin
if [ "$2" ] && [ "${2:0:1}" != '-' ]
then
tm="$2"
shift
fi
shift
;;
--help) echo "$help"; exit 0;;
esac
done
if [ "$sp" ]
then
sleep "$tm" &
spin "$!" "Spinning $tm seconds"
fi
}
[[ "$@" ]] || print_help
while (( "$#" ))
do
case "$1" in
foobar)
# Command.
shift; foobar "$@"; shift "$#";;
--multi)
opt="$1"; shift # Save option name and shift.
while (( "$#" ))
do
if [[ "$1" =~ ^--$ ]]
then
eop=1; shift # End of the options.
elif [[ "$1" =~ ^-$|--.+|-[^-]+ ]] && [ ! "$eop" ]
then
echo \
"$0: Only positional arguments allowed after $opt" >&2
echo \
"Use -- (end of opts) for allow options as arguments."
exit 1
fi
multi+=("$1"); shift
done
;;
--help)
print_help;;
--)
# End of the options.
# Save all following options as positional arguments.
shift; args+=("$@")
shift "$#"
;;
--foo|--foo=*)
get_optarg "$1" "$2"
foo+=("$arg") # Single value.
shift "$sft";;
--bar|--bar=*)
get_optarg "$1" "$2"
# Multiple values.
# Accept single value if it passed with '='.
bar+=("$arg")
if [ "$bar" == "$2" ]
then
shift 2 # Shift for prevent doublicate first value
# and option name. I.e. skip this: --bar A
while (( "$#" ))
do
if [[ ! "$1" =~ ^-+ ]]
then
bar+=("$1"); shift
else
break
fi
done
sft=0 # Don't shift after parser to prevent
# next argument skipping.
fi
shift "$sft";;
-*)
# Short options. -abc as -a -b -c
# Split $1 to characters.
for i in $(seq 2 ${#1}); do opts+=("-${1:i-1:1}"); done
for opt in "${opts[@]}"
do
case "$opt" in
-f) f=1;;
-b) b=1;;
*) echo "$0: Bad option: $opt" >&2; exit 1;;
esac
done
shift
;;
*)
args+=("$1") # Save positional arguments.
shift
;;
esac
done
[ "$foo" ] && echo "Proc foo: <${foo[@]}>"
[ "$bar" ] && echo "Proc bar: <${bar[@]}>"
[ "$f" ] && echo "Proc f: <$f>"
[ "$b" ] && echo "Proc b: <$b>"
[ "$multi" ] && echo "Proc multi: <${multi[@]}>"
[ "$args" ] && echo "Positional: <${args[@]}>"