-
Notifications
You must be signed in to change notification settings - Fork 1
/
argparser.sh
117 lines (96 loc) · 2.29 KB
/
argparser.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
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
#!/bin/bash
# Argument parse
function parse_args() {
local _i __i _argument _nextargument
local count=0
#scope only func
#parse long args
function parse_long() {
local _arg _nextarg _name _value _i
#strip --
_arg=${1#--}
_nextarg=$2
#strip after =
_name="${_arg%%=*}"
#if arg contains =
if [[ $_arg == *=* ]]; then
#strip before =
_value="${_arg#*=}"
#if next argument doesnt contain a dash it means it's the value
elif [[ ! $_nextarg == -* ]]; then
_value="$_nextarg"
else
_value=true
fi
#convert dash to camelcase
if [[ $_name =~ "-" ]]; then
_name=$(echo "$_name" | awk -F"-" '{for(i=2;i<=NF;i++){$i=toupper(substr($i,1,1)) substr($i,2)}} 1' OFS="")
fi
eval "$_name=\$_value"
#set short if mapping is defined
if [[ "${ARGPARSER_MAP[@]}" =~ "$_name" ]]; then
for _i in "${!ARGPARSER_MAP[@]}"; do
if [[ ${ARGPARSER_MAP[${_i}]} == "$_name" ]]; then
_name=$ARGPARSER_SHORT_PREFIX$_i
eval "$_name"=true
fi
done
fi
}
#parse short args
function parse_short() {
local _i _arg _name _letter
#strip -
_arg=${1#-}
#for each char
for _i in $(seq 1 ${#_arg}); do
_letter=${_arg:_i-1:1}
_name=$ARGPARSER_SHORT_PREFIX$_letter
eval "$_name"=true
#set long if mapping is defined
if [[ ${ARGPARSER_MAP[${_letter}]} ]]; then
_name=${ARGPARSER_MAP[${_letter}]}
eval "$_name"=true
fi
done
}
#parse argument
function parse_argument() {
local _name _value
_name="$ARGPARSER_ARGUMENT_PREFIX$count"
# shellcheck disable=SC2034
_value=$1
eval "$_name=\$_value"
}
#default short prefix if not configured
[ -z "$ARGPARSER_SHORT_PREFIX" ] && ARGPARSER_SHORT_PREFIX="opt"
#default argument prefix if not configured
[ -z "$ARGPARSER_ARGUMENT_PREFIX" ] && ARGPARSER_ARGUMENT_PREFIX="argument"
#init all short opt to false
for _i in {{a..z},{A..Z}}; do
_name=$ARGPARSER_SHORT_PREFIX$_i
eval $_name=false
done
allargs=("$@")
__i=0
#analyze args
for _i in "${!allargs[@]}"; do
_argument=${allargs[$__i]}
_nextargument=${allargs[$__i + 1]}
case $_argument in
--*)
parse_long "$_argument" "$_nextargument"
#skip next arg
[[ ! $_nextargument == -* ]] && ((__i++))
;;
-*)
parse_short "$_argument"
;;
*)
((count++))
parse_argument "$_argument"
;;
esac
((__i++))
done
}