Skip to content
This repository was archived by the owner on Apr 29, 2021. It is now read-only.

Commit 0d743bc

Browse files
committed
Support command line options that take values
Add support of command line options that take valuea. This paves way for adding more features like logging to a specified file and filtering tests based on a pattern given by user.
1 parent 0360811 commit 0d743bc

File tree

1 file changed

+53
-42
lines changed

1 file changed

+53
-42
lines changed

libexec/bats

Lines changed: 53 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -57,53 +57,64 @@ export BATS_PREFIX="$(abs_dirname "$BATS_LIBEXEC")"
5757
export BATS_CWD="$(abs_dirname .)"
5858
export PATH="$BATS_LIBEXEC:$PATH"
5959

60-
options=()
60+
unset count_flag pretty
61+
[ -t 0 ] && [ -t 1 ] && pretty="1"
62+
[ -n "$CI" ] && pretty=""
63+
6164
arguments=()
62-
for arg in "$@"; do
63-
if [ "${arg:0:1}" = "-" ]; then
64-
if [ "${arg:1:1}" = "-" ]; then
65-
options[${#options[*]}]="${arg:2}"
66-
else
67-
index=1
68-
while option="${arg:$index:1}"; do
69-
[ -n "$option" ] || break
70-
options[${#options[*]}]="$option"
71-
let index+=1
72-
done
73-
fi
74-
else
65+
66+
while [[ $# -gt 0 ]]; do
67+
arg="$1"
68+
69+
if [ ! ${arg:0:1} = "-" ]; then
7570
arguments[${#arguments[*]}]="$arg"
71+
shift
72+
continue
7673
fi
77-
done
7874

79-
unset count_flag pretty
80-
[ -t 0 ] && [ -t 1 ] && pretty="1"
81-
[ -n "$CI" ] && pretty=""
75+
options=()
76+
pat="^-[a-z][a-z]+"
77+
opt_grouped="false" #whether a option is in a group or not
78+
if [[ $arg =~ $pat ]]; then
79+
index=1
80+
while opt="${arg:$index:1}"; do
81+
[ -n "$opt" ] || break
82+
options[${#options[*]}]="-$opt"
83+
let index+=1
84+
done
85+
opt_grouped="true"
86+
else
87+
options[${#options[*]}]=$arg
88+
fi
89+
90+
for opt in ${options[@]}; do
91+
case $opt in
92+
"-h" | "--help" )
93+
help
94+
exit 0
95+
;;
96+
"-v" | "--version" )
97+
version
98+
exit 0
99+
;;
100+
"-c" | "--count" )
101+
count_flag="-c"
102+
;;
103+
"-t" | "--tap" )
104+
pretty=""
105+
;;
106+
"-p" | "--pretty" )
107+
pretty="1"
108+
;;
109+
* )
110+
usage >&2
111+
exit 1
112+
;;
113+
esac
114+
done
82115

83-
for option in "${options[@]}"; do
84-
case "$option" in
85-
"h" | "help" )
86-
help
87-
exit 0
88-
;;
89-
"v" | "version" )
90-
version
91-
exit 0
92-
;;
93-
"c" | "count" )
94-
count_flag="-c"
95-
;;
96-
"t" | "tap" )
97-
pretty=""
98-
;;
99-
"p" | "pretty" )
100-
pretty="1"
101-
;;
102-
* )
103-
usage >&2
104-
exit 1
105-
;;
106-
esac
116+
shift
117+
continue
107118
done
108119

109120
if [ "${#arguments[@]}" -eq 0 ]; then

0 commit comments

Comments
 (0)