diff --git a/lib/getopts_long.bash b/lib/getopts_long.bash index b99ae04..9e0f469 100644 --- a/lib/getopts_long.bash +++ b/lib/getopts_long.bash @@ -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}"}" diff --git a/test/bats/github_15.bats b/test/bats/github_15.bats new file mode 100644 index 0000000..11aa1f5 --- /dev/null +++ b/test/bats/github_15.bats @@ -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' +} diff --git a/test/bats/option_supplied.bats b/test/bats/option_supplied.bats index b5cda42..658329f 100644 --- a/test/bats/option_supplied.bats +++ b/test/bats/option_supplied.bats @@ -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' +}