Skip to content

Commit

Permalink
Fixed github #15
Browse files Browse the repository at this point in the history
  • Loading branch information
UrsaDK committed Dec 8, 2024
1 parent 37f62dd commit f263a9c
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 1 deletion.
12 changes: 11 additions & 1 deletion lib/getopts_long.bash
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,17 @@ getopts_long() {
builtin getopts "${optspec_short}" "${optvar}" "${@}" || return 1
[[ "${!optvar}" == '-' ]] || return 0

printf -v "${optvar}" "%s" "${OPTARG%%=*}"
if [[ "${OPTARG}" == *=* ]]; then
printf -v "${optvar}" "%s" "${OPTARG%%=*}"
else
for optspec in $(echo "${optspec_long}" | tr ' ' '\n' | sort -ur); do
if [[ "${optspec}" == *: && "${OPTARG}" == "${optspec%?}"* ]]; then
printf -v "${optvar}" "%s" "${optspec%?}"
break
fi
done
[[ "${!optvar}" == '-' ]] && printf -v "${optvar}" "%s" "${OPTARG}"
fi

if [[ "${optspec_long}" =~ (^|[[:space:]])${!optvar}:([[:space:]]|$) ]]; then
OPTARG="${OPTARG#"${!optvar}"}"
Expand Down
22 changes: 22 additions & 0 deletions test/bats/github_15.bats
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#!/usr/bin/env bats

load ../test_helper

@test "${FEATURE}: short option, silent" {
compare '-o-- user_arg' \
'-o-- user_arg'
}
@test "${FEATURE}: short option, verbose" {
compare '-o-- user_arg' \
'-o-- user_arg'
}

@test "${FEATURE}: long option, silent" {
compare '-o-- user_arg' \
'--option-- user_arg'
}

@test "${FEATURE}: long option, verbose" {
compare '-o-- user_arg' \
'--option-- user_arg'
}
21 changes: 21 additions & 0 deletions test/bats/option_supplied.bats
Original file line number Diff line number Diff line change
Expand Up @@ -182,3 +182,24 @@ load ../test_helper
compare '-o =user_val' \
'--option =user_val'
}

# option with an adjoined value

@test "${FEATURE}: short option, adjoined value, silent" {
compare '-ouser_val' \
'-ouser_val'
}
@test "${FEATURE}: short option, adjoined value, verbose" {
compare '-ouser_val' \
'-ouser_val'
}

@test "${FEATURE}: long option, adjoined value, silent" {
compare '-ouser_val' \
'--optionuser_val'
}

@test "${FEATURE}: long option, adjoined value, verbose" {
compare '-ouser_val' \
'--optionuser_val'
}

0 comments on commit f263a9c

Please sign in to comment.