-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse-argv.sh
60 lines (52 loc) · 1.67 KB
/
parse-argv.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
#!/bin/bash -e
# shellcheck disable=SC1091
source ./util.sh
function parse-argv {
if ! declare -p programs 2> /dev/null | grep -q 'declare \-a'; then
printf 'The "programs" variable must be declared as an array before invoking parse-argv\n' >&2
return 1
fi
if ! declare -p options 2> /dev/null | grep -q 'declare \-a'; then
printf 'The "options" variable must be declared as an array before invoking parse-argv\n' >&2
return 1
fi
if ! declare -p arguments 2> /dev/null | grep -q 'declare \-a'; then
printf 'The "arguments" variable must be declared as an array before invoking parse-argv\n' >&2
return 1
fi
if ! declare -p ARGS 2> /dev/null | grep -q 'declare \-A'; then
printf 'The "ARGS" variable must be declared as an array before invoking parse-argv\n' >&2
return 1
fi
local USAGE
USAGE="$1"
# shellcheck disable=SC2086
set -- $2
local e opt io
for e in "$@"; do
# printf 'arg: "%s"\n' "$e"
if [[ $e == -[a-Z0-9]* ]]; then
# printf -- 'short - '
:
fi
if [[ $e == --[a-Z0-9_-]* ]]; then
e="${e#--*}"
# opt="${e%%=*}"
arg="${e#*=}"
index-of "$arg" options io "$_DOCOPT_OPTION_POSITION_LONG" "$_DOCOPT_OPTION_STRUCT_LENGTH"
if [[ "$io" == '-1' ]]; then
printf '%s' "$USAGE" >&2
return 1
else
ARGS[$arg]=true
fi
fi
done
return 0
}
if [[ ( "$0" == "parse-argv.sh" ) || ( "$0" == "./parse-argv.sh" ) ]]; then
programs=()
options=()
arguments=()
parse-argv "$@"
fi