forked from nk412/optparse
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoptparse.bash
More file actions
151 lines (131 loc) · 4.82 KB
/
optparse.bash
File metadata and controls
151 lines (131 loc) · 4.82 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
# Optparse - a BASH wrapper for getopts
# @author : nk412 / nagarjuna.412@gmail.com
optparse_usage=""
optparse_contractions=""
optparse_defaults=""
optparse_process=""
optparse_arguments_string=""
# -----------------------------------------------------------------------------------------------------------------------------
optparse.throw_error() {
echo "OPTPARSE: ERROR: $1"
exit 1
}
# -----------------------------------------------------------------------------------------------------------------------------
optparse.define() {
if [ $# -lt 3 ]; then
optparse.throw_error "optparse.define <short> <long> <variable> [<desc>] [<default>] [<value>]"
fi
while [ "$#" -gt 0 ] ; do
local option="$1"
local key="$( echo $option | awk -F "=" '{print $1}' )";
local value="$( echo $option | awk -F "=" '{print $2}' )";
#essentials: shortname, longname, description
case "$key" in
(short)
local shortname="$value"
if [ ${#shortname} -ne 1 ]; then
optparse.throw_error "short name expected to be one character long"
fi
local short="-${shortname}"
;;
(long)
local longname="$value"
if [ ${#longname} -lt 2 ]; then
optparse.throw_error "long name expected to be atleast one character long"
fi
local long="--${longname}"
;;
(desc)
local desc="$value"
;;
(default)
local default="$value"
;;
(variable)
local variable="$value"
;;
(value)
local val="$value"
;;
esac
shift
done
if [ "$variable" = "" ]; then
optparse.throw_error "You must give a variable for option: ($short/$long)"
fi
if [ "$val" = "" ]; then
val="\$OPTARG"
fi
# build OPTIONS and help
optparse_usage="${optparse_usage}#NL#TB${short} $(printf "%-25s %s" "${long}:" "${desc}")"
if [ "$default" != "" ]; then
optparse_usage="${optparse_usage} [default:$default]"
fi
optparse_contractions="${optparse_contractions}#NL#TB#TB${long})#NL#TB#TB#TBparams+=(\"${short}\");;"
if [ "$default" != "" ]; then
optparse_defaults="${optparse_defaults}#NL${variable}=${default}"
fi
optparse_arguments_string="${optparse_arguments_string}${shortname}"
if [ "$val" = "\$OPTARG" ]; then
optparse_arguments_string="${optparse_arguments_string}:"
fi
optparse_process="${optparse_process}#NL#TB#TB${shortname})#NL#TB#TB#TB${variable}=\"$val\";;"
}
# -----------------------------------------------------------------------------------------------------------------------------
optparse.build() {
# Building getopts header here
# Function usage
cat << EOF | sed 's/#NL/\
/g;s/#TB/ /g'
usage() {
cat << XXX
usage: \$0 [OPTIONS]
OPTIONS:
$optparse_usage
-? --help : usage
XXX
}
# Contract long options into short options
params=()
while [ \$# -ne 0 ]; do
param="\$1"
shift
case "\$param" in
$optparse_contractions
"-?"|--help)
usage
exit 0;;
*)
if [[ "\$param" == --* ]]; then
echo -e "Unrecognized long option: \$param"
usage
exit 1
fi
params+=("\$param");;
esac
done
set -- "\${params[@]}"
# Set default variable values
$optparse_defaults
# Process using getopts
while getopts "$optparse_arguments_string" option; do
case \$option in
# Substitute actions for different variables
$optparse_process
:)
echo "Option - \$OPTARG requires an argument"
exit 1;;
*)
usage
exit 1;;
esac
done
EOF
# Unset global variables
unset optparse_usage
unset optparse_process
unset optparse_arguments_string
unset optparse_defaults
unset optparse_contractions
}
# -----------------------------------------------------------------------------------------------------------------------------