lib: Add option parsing for short flags followed by number

This commit is contained in:
Ethan P 2020-07-06 18:01:11 -07:00
parent 38f56c1bc7
commit e4999a7ffb
No known key found for this signature in database
GPG Key ID: 6963FD04F6CF35EA
2 changed files with 40 additions and 2 deletions

View File

@ -69,6 +69,12 @@ shiftval() {
if [[ -n "${OPT_VAL+x}" ]]; then
return 0
fi
# If it's a short flag followed by a number, use the number.
if [[ "$OPT" =~ ^-[[:alpha:]][[:digit:]]{1,}$ ]]; then
OPT_VAL="${OPT:2}"
return
fi
OPT_VAL="${_ARGV[$_ARGV_INDEX]}"
((_ARGV_INDEX++))

View File

@ -5,9 +5,12 @@ setup() {
pos2 \
--flag1 \
-v3=for_val3 \
-v4 for_val4 \
-v4 \
-v55 \
-vn for_val4 \
--flag2
source "${LIB}/print.sh"
source "${LIB}/opt.sh"
}
@ -60,11 +63,40 @@ test:long_value_explicit() {
fail 'Failed to find option.'
}
test:short_value_implicit_number() {
description "Parse short options in '-k0' syntax."
while shiftopt; do
if [[ "$OPT" = "-v4" ]]; then
shiftval
assert_opt_value '4'
return
fi
done
fail 'Failed to find option.'
}
test:short_value_implicit_number2() {
description "Parse short options in '-k0' syntax."
while shiftopt; do
if [[ "$OPT" = "-v55" ]]; then
shiftval
assert_opt_value '55'
return
fi
done
fail 'Failed to find option.'
}
test:short_value_implicit() {
description "Parse short options in '-k value' syntax."
while shiftopt; do
if [[ "$OPT" = "-v4" ]]; then
if [[ "$OPT" = "-vn" ]]; then
shiftval
assert_opt_value 'for_val4'
return