-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoptions.sh
181 lines (159 loc) · 5.13 KB
/
options.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
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
#!/usr/bin/env bash
set -o errexit -o pipefail -o nounset -o noclobber
if [[ -z "$BASH_VERSION" ]]; then
echo "This script requires bash."
exit 1
fi
if [[ "$BASH_VERSION" < "4.0" ]]; then
echo "This script requires bash version 4.0 or higher."
if [[ "$(uname -s)" == "Darwin"* ]]; then
echo "You can install it with Homebrew: brew install bash"
fi
exit 1
fi
if [[ -z "$(command -v getopt)" ]]; then
echo "getopt is not installed. Please install it and try again."
exit 1
fi
getopt --test > /dev/null && true
if [[ $? -ne 4 ]]; then
echo "I'm sorry, \"getopt --test\" has failed in this environment."
echo "Please install the newer version of getopt and try again."
exit 1
fi
__options_bash_skip_help=1
__options_bash_skip_version=1
__options_bash_program_name=""
__options_bash_program_version=""
__options_bash_program_description=""
declare -A __options_bash_command_names
declare -A __options_bash_command_descriptions
declare -A __options_bash_command_has_arg
declare -A __options_bash_command_aliases
__options_bash_check_commands=1
declare -A __options_bash
__options_bash_args=""
__options_bash_command=""
define_program() {
# name version description
__options_bash_program_name="$1"
__options_bash_program_version="$2"
__options_bash_program_description="$3"
}
define_command() {
# names description has_arg
# has_arg: "" - no argument
# has_arg: "arg" - required argument, where "arg" is the name of the argument
local names
IFS=', ' read -ra names <<< "$1"
__options_bash_command_names["${names[0]}"]="$1"
__options_bash_command_descriptions["${names[0]}"]="${2:-}"
__options_bash_command_has_arg["${names[0]}"]="${3:-}"
for name in "${names[@]}"; do
if [[ "$name" == "--"* ]]; then
if [[ "$name" == "--" ]]; then
echo "Error: Invalid long option '$name'"
exit 1
fi
elif [[ "$name" == "-"* ]]; then
if [[ ! "$name" =~ ^-[a-zA-Z0-9]$ ]]; then
echo "Error: Invalid short option '$name'"
exit 1
fi
else
__options_bash_check_commands=0
fi
__options_bash_command_aliases["$name"]="${names[0]}"
done
}
parse_options() {
local long_options=""
local short_options=""
for option in "${!__options_bash_command_aliases[@]}"; do
local command="${__options_bash_command_aliases[$option]}"
if [[ "$option" == "--"* ]]; then
if [[ -n "$long_options" ]]; then long_options+=","; fi
long_options+="${option#--}"
if [[ -n "${__options_bash_command_has_arg[$command]}" ]]; then long_options+=":"; fi
elif [[ "$option" == "-"* ]]; then
short_options+="${option#-}"
if [[ -n "${__options_bash_command_has_arg[$command]}" ]]; then short_options+=":"; fi
fi
done
local parsed
local status=0
parsed=$(getopt -o "$short_options" -l "$long_options" -n "${__options_bash_program_name}" -- "$@") || status=$?
if [[ $status -ne 0 ]]; then
echo "Try '${__options_bash_program_name} --help' for more information."
exit 1
fi
eval set -- "$parsed"
while true; do
if [[ "$1" == "--" ]]; then
shift
break
fi
local option="${__options_bash_command_aliases["$1"]}"
if [[ -z "$option" ]]; then
echo "Error: Unknown option '$1'"
echo "Try '${__options_bash_program_name} --help' for more information."
exit 1
fi
shift
if [[ -n "${__options_bash_command_has_arg[$option]}" ]]; then
__options_bash["$option"]="$1"
shift
else
__options_bash["$option"]=""
fi
done
if [[ ${__options_bash_check_commands} -eq 0 ]]; then
if [ "$#" -eq 0 ]; then
echo "Error: No command specified"
echo "Try '${__options_bash_program_name} --help' for more information."
exit 1
fi
local unknown_command=0
for command in "${!__options_bash_command_aliases[@]}"; do
if [[ "$command" == "-"* ]]; then continue; fi
if [[ "$command" == "$1" ]]; then
unknown_command=1
break
fi
done
if [[ $unknown_command -eq 0 ]]; then
echo "Error: Unknown command '$1'"
echo "Try '${__options_bash_program_name} --help' for more information."
exit 1
else
__options_bash_command="${__options_bash_command_aliases[$1]}"
fi
fi
__options_bash_args="$@"
}
show_help() {
echo "${__options_bash_program_name} ${__options_bash_program_version}"
echo "${__options_bash_program_description}"
echo ""
echo "Usage: ${__options_bash_program_name} [options] [command] [command options]"
echo ""
echo "Options:"
for option in "${!__options_bash_command_names[@]}"; do
if [[ "$option" == "--"* ]]; then continue; fi
if [[ "$option" != "-"* ]]; then continue; fi
echo " ${__options_bash_command_names[$option]}"
done
for option in "${!__options_bash_command_names[@]}"; do
if [[ "$option" != "--"* ]]; then continue; fi
echo " ${__options_bash_command_names[$option]}"
done
echo ""
echo "Commands:"
for command in "${!__options_bash_command_names[@]}"; do
if [[ "$command" == "-"* ]]; then continue; fi
echo " ${__options_bash_command_names[$command]}"
done
}
show_version() {
echo "${__options_bash_program_name} ${__options_bash_program_version}"
}