Skip to content
This repository was archived by the owner on Dec 23, 2018. It is now read-only.

Commit 22417c5

Browse files
committed
Parse options so they can be combined (e.g. -kv)
1 parent 12bbef5 commit 22417c5

File tree

1 file changed

+66
-36
lines changed

1 file changed

+66
-36
lines changed

bin/php-build

Lines changed: 66 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,39 @@ PHP_BUILD_VERSION="0.1.4"
55
set -E
66
exec 3<&2 # preserve original stderr at fd 3
77

8+
9+
lib() {
10+
parse_options() {
11+
OPTIONS=()
12+
ARGUMENTS=()
13+
local arg option index
14+
15+
for arg in "$@"; do
16+
if [ "${arg:0:1}" = "-" ]; then
17+
if [ "${arg:1:1}" = "-" ]; then
18+
OPTIONS[${#OPTIONS[*]}]="${arg:2}"
19+
else
20+
index=1
21+
while option="${arg:$index:1}"; do
22+
[ -n "$option" ] || break
23+
OPTIONS[${#OPTIONS[*]}]="$option"
24+
index=$(($index+1))
25+
done
26+
fi
27+
else
28+
ARGUMENTS[${#ARGUMENTS[*]}]="$arg"
29+
fi
30+
done
31+
}
32+
33+
if [ "$1" == "--$FUNCNAME" ]; then
34+
declare -f "$FUNCNAME"
35+
exit
36+
fi
37+
}
38+
lib "$1"
39+
40+
841
resolve_link() {
942
$(type -p greadlink readlink | head -1) "$1"
1043
}
@@ -207,7 +240,7 @@ version() {
207240

208241
usage() {
209242
{ version
210-
echo "usage: php-build [-v|--verbose] definition prefix"
243+
echo "usage: php-build [-k|--keep] [-v|--verbose] definition prefix"
211244
echo " php-build --definitions"
212245
} >&2
213246

@@ -231,34 +264,40 @@ fi
231264

232265

233266
unset VERBOSE
267+
unset KEEP_BUILD_PATH
234268
PHP_BUILD_ROOT="$(abs_dirname "$0")/.."
235269

236-
case "$1" in
237-
"-h" | "--help" )
238-
usage without_exiting
239-
{ echo
240-
echo " -v/--verbose Verbose mode: print compilation status to stdout"
241-
echo " --definitions List all built-in definitions"
242-
echo
243-
} >&2
244-
exit 0
245-
;;
246-
"--definitions" )
247-
list_definitions
248-
exit 0
249-
;;
250-
"--version" )
251-
version
252-
exit 0
253-
;;
254-
"-v" | "--verbose" )
255-
VERBOSE=true
256-
shift
257-
;;
258-
esac
259-
270+
parse_options "$@"
271+
for option in "${OPTIONS[@]}"; do
272+
case "$option" in
273+
"h" | "help" )
274+
usage without_exiting
275+
{ echo
276+
echo " -k/--keep Do not remove source tree after installation"
277+
echo " -v/--verbose Verbose mode: print compilation status to stdout"
278+
echo " --definitions List all built-in definitions"
279+
echo
280+
} >&2
281+
exit 0
282+
;;
283+
"definitions" )
284+
list_definitions
285+
exit 0
286+
;;
287+
"k" | "keep" )
288+
KEEP_BUILD_PATH=true
289+
;;
290+
"v" | "verbose" )
291+
VERBOSE=true
292+
;;
293+
"version" )
294+
version
295+
exit 0
296+
;;
297+
esac
298+
done
260299

261-
DEFINITION_PATH="$1"
300+
DEFINITION_PATH="${ARGUMENTS[0]}"
262301
if [ -z "$DEFINITION_PATH" ]; then
263302
usage
264303
elif [ ! -e "$DEFINITION_PATH" ]; then
@@ -271,20 +310,11 @@ elif [ ! -e "$DEFINITION_PATH" ]; then
271310
fi
272311
fi
273312

274-
PREFIX_PATH="$2"
313+
PREFIX_PATH="${ARGUMENTS[1]}"
275314
if [ -z "$PREFIX_PATH" ]; then
276315
usage
277316
fi
278317

279-
OPTIONS="$3"
280-
for option in $OPTIONS; do
281-
case "$option" in
282-
"-k" | "--keep" )
283-
KEEP_BUILD_PATH="y"
284-
;;
285-
esac
286-
done
287-
288318
# Create a needed directory, if it doesn't exist.
289319
if [ ! -d "$PREFIX_PATH/libexec" ] || [ ! -d "$PREFIX_PATH/etc/conf.d" ]; then
290320
mkdir -p "$PREFIX_PATH/"{libexec,etc/conf.d}

0 commit comments

Comments
 (0)