-
-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathparse_params
executable file
·44 lines (34 loc) · 960 Bytes
/
parse_params
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
#!/usr/bin/env bash
set -o errexit
set -o nounset
set -o pipefail
set -o errtrace
(shopt -p inherit_errexit &>/dev/null) && shopt -s inherit_errexit
SCRIPT_DIR="$(cd -P -- "$(dirname -- "$0")" && pwd -P)"
readonly SCRIPT_DIR
# shellcheck source=./init.bash
source "$SCRIPT_DIR"/init.bash
declare -A opts=()
declare -a args=()
foo() {
l.parse_params opts args "$@"
}
foo -a 3 -b12 -c=5 foo -d= bar -e -3 -4 a -F=abc -gh w -ij=5 -km= -5n -xzy --beep=boop baz --no-wow
echo "[${#opts[@]} Options]"
for key in "${!opts[@]}"; do
echo "$key: ${opts[$key]}"
done
echo
echo "[${#args[@]} Arguments]"
declare i=0
for key in "${args[@]}"; do
echo "$i: $key"
(( i+=1 ))
done
echo "------Use Options------"
# NOTE: Use ${opts[c]:-} instead of ${opts[c]}. If `set -o nounset`, ${opts[c]} will throw error when option omitted.
if [[ -n ${opts[c]:-} ]]; then
echo "c=${opts[c]}"
fi
echo "------Use Arguments------"
echo "${args[0]} ${args[1]} ${args[2]}"