-
Notifications
You must be signed in to change notification settings - Fork 3
/
utils.sh
78 lines (67 loc) · 1.51 KB
/
utils.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
#!/usr/bin/env bash
if [ "${LOADED_UTILS_SH:-}" ]; then
return
else
export LOADED_UTILS_SH=true
fi
die() {
if [ "${1:-}" ]; then
>&2 echo "$1"
fi
exit 1
}
get_arg() {
local arg_type="$1"
shift
local is_required
case "$arg_type" in
required|required-many)
is_required=true
;;
optional|optional-many) ;;
*)
die "Invalid is_required argument \"$2\" in get_arg"
;;
esac
local has_many_values
if [ "${arg_type: -6}" == "-many" ]; then
has_many_values=true
fi
local option_arg="$1"
shift
local args=("$@")
unset out
out=()
local get_next_arg
for arg in "${args[@]}"; do
if [ "${get_next_arg:-}" ]; then
out+=("$arg")
unset get_next_arg
if [ ! "${has_many_values:-}" ]; then
break
fi
# --foo=bar (get the value after '=')
elif [ "${arg:0:$(( ${#option_arg} + 1 ))}" == "$option_arg=" ]; then
out+=("${arg:$(( ${#option_arg} + 1 ))}")
if [ ! "${has_many_values:-}" ]; then
break
fi
# --foo bar (get the next argument)
elif [ "$arg" == "$option_arg" ]; then
get_next_arg=true
fi
done
# arg list ended with --something but no argument was provided next
if [ "${get_next_arg:-}" ]; then
die "Expected argument after \"${args[-1]}"\"
fi
if [ "${out[0]:-}" ]; then
if [ ! "${has_many_values:-}" ]; then
out="${out[0]}"
fi
elif [ "${is_required:-}" ]; then
die "Argument $option_arg is required, but was not found"
else
unset out
fi
}