Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[GH-13] Support for starting OPTSPEC with a space #21

Merged
merged 7 commits into from
Dec 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/getopts_long.bash
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ getopts_long() {
set -- "${args[@]}"
fi

builtin getopts "${optspec_short}" "${optvar}" "${@}" || return 1
builtin getopts -- "${optspec_short}" "${optvar}" "${@}" || return 1
[[ "${!optvar}" == '-' ]] || return 0

if [[ "${OPTARG}" == *=* ]]; then
Expand Down
92 changes: 92 additions & 0 deletions test/bats/github_13.bats
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
#!/usr/bin/env bats

load ../test_helper
export GETOPTS_LONG_TEST_BIN='getopts_long-github_13'

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

@test "${FEATURE}: long variable, silent" {
compare '-v user_val' \
'--variable=user_val' \
'/^OPTIND: /d'
expect "${bash_getopts_lines[4]}" == 'OPTIND: 3'
expect "${getopts_long_lines[4]}" == 'OPTIND: 2'
}
@test "${FEATURE}: long variable, verbose" {
compare '-v user_val' \
'--variable=user_val' \
'/^OPTIND: /d'
expect "${bash_getopts_lines[4]}" == 'OPTIND: 3'
expect "${getopts_long_lines[4]}" == 'OPTIND: 2'
}

@test "${FEATURE}: toggle followed by long variable, silent" {
compare '-t -v user_val' \
'--toggle --variable=user_val' \
'/^OPTIND: /d'
expect "${bash_getopts_lines[5]}" == 'OPTIND: 4'
expect "${getopts_long_lines[5]}" == 'OPTIND: 3'
}
@test "${FEATURE}: toggle followed by long variable, verbose" {
compare '-t -v user_val' \
'--toggle --variable=user_val' \
'/^OPTIND: /d'
expect "${bash_getopts_lines[5]}" == 'OPTIND: 4'
expect "${getopts_long_lines[5]}" == 'OPTIND: 3'
}

@test "${FEATURE}: long variable followed by toggle, silent" {
compare '-v user_val -t' \
'--variable=user_val --toggle' \
'/^OPTIND: /d'
expect "${bash_getopts_lines[5]}" == 'OPTIND: 4'
expect "${getopts_long_lines[5]}" == 'OPTIND: 3'
}
@test "${FEATURE}: long variable followed by toggle, verbose" {
compare '-v user_val -t' \
'--variable=user_val --toggle' \
'/^OPTIND: /d'
expect "${bash_getopts_lines[5]}" == 'OPTIND: 4'
expect "${getopts_long_lines[5]}" == 'OPTIND: 3'
}

@test "${FEATURE}: terminator followed by long variable, silent" {
compare '-t -- -v user_val' \
'--toggle -- --variable=user_val' \
'/^\$@: /d'
expect "${bash_getopts_lines[5]}" == '$@: -v user_val'
expect "${getopts_long_lines[5]}" == '$@: --variable=user_val'
}
@test "${FEATURE}: terminator followed by long variable, verbose" {
compare '-t -- -v user_val' \
'--toggle -- --variable=user_val' \
'/^\$@: /d'
expect "${bash_getopts_lines[5]}" == '$@: -v user_val'
expect "${getopts_long_lines[5]}" == '$@: --variable=user_val'
}

@test "${FEATURE}: long variable followed by terminator, silent" {
compare '-v user_val -- -t' \
'--variable=user_val -- --toggle' \
'/^(OPTIND|\$@): /d'
expect "${bash_getopts_lines[4]}" == 'OPTIND: 4'
expect "${getopts_long_lines[4]}" == 'OPTIND: 3'
expect "${bash_getopts_lines[5]}" == '$@: -t'
expect "${getopts_long_lines[5]}" == '$@: --toggle'
}
@test "${FEATURE}: long variable followed by terminator, verbose" {
compare '-v user_val -- -t' \
'--variable=user_val -- --toggle' \
'/^(OPTIND|\$@): /d'
expect "${bash_getopts_lines[4]}" == 'OPTIND: 4'
expect "${getopts_long_lines[4]}" == 'OPTIND: 3'
expect "${bash_getopts_lines[5]}" == '$@: -t'
expect "${getopts_long_lines[5]}" == '$@: --toggle'
}
39 changes: 39 additions & 0 deletions test/bin/getopts_long-github_13-silent
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#!/usr/bin/env bash

TOPDIR="$(cd "$(dirname "${0}")"/../.. && pwd)"
# shellcheck disable=SC1090
source "${TOPDIR}/lib/getopts_long.bash"

while getopts_long ': toggle option: variable:' OPTKEY; do
case ${OPTKEY} in
'toggle')
printf 'toggle triggered'
;;
'option')
printf 'option supplied'
;;
'variable')
printf 'value supplied'
;;
'?')
printf "INVALID OPTION"
;;
':')
printf "MISSING ARGUMENT"
;;
*)
printf "NEVER REACHED"
;;
esac
printf ' -- '
[[ -z "${OPTARG+SET}" ]] && echo 'OPTARG is unset' || echo "OPTARG=${OPTARG}"
done

shift $(( OPTIND - 1 ))
[[ "${1}" == "--" ]] && shift

echo "OPTERR: ${OPTERR}"
echo "OPTKEY: ${OPTKEY}"
echo "OPTARG: ${OPTARG}"
echo "OPTIND: ${OPTIND}"
echo "\$@: ${*}"
36 changes: 36 additions & 0 deletions test/bin/getopts_long-github_13-verbose
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#!/usr/bin/env bash

TOPDIR="$(cd "$(dirname "${0}")"/../.. && pwd)"
# shellcheck disable=SC1090
source "${TOPDIR}/lib/getopts_long.bash"

while getopts_long ' toggle option: variable:' OPTKEY; do
case ${OPTKEY} in
'toggle')
printf 'toggle triggered'
;;
'option')
printf 'option supplied'
;;
'variable')
printf 'value supplied'
;;
'?')
printf "INVALID OPTION or MISSING ARGUMENT"
;;
*)
printf "NEVER REACHED"
;;
esac
printf ' -- '
[[ -z "${OPTARG+SET}" ]] && echo 'OPTARG is unset' || echo "OPTARG=${OPTARG}"
done

shift $(( OPTIND - 1 ))
[[ "${1}" == "--" ]] && shift

echo "OPTERR: ${OPTERR}"
echo "OPTKEY: ${OPTKEY}"
echo "OPTARG: ${OPTARG}"
echo "OPTIND: ${OPTIND}"
echo "\$@: ${*}"
36 changes: 18 additions & 18 deletions test/test_helper.bash
Original file line number Diff line number Diff line change
Expand Up @@ -9,29 +9,29 @@ PATH="${TOPDIR}/bin:${PATH}"
debug() {
cat >&3 <<END_OF_MESSAGE

EXPECTED (bash getopts)
––––––––––––––––––––––––
EXPECTED (eg: bash getopts)
––––––––––––––---––––––––––
${2}

RETURNED (getopts_long)
––––––––––––––––––––––––
RECEIVED (eg: getopts_long)
––––––––––––––---––––––––––
${1}

END_OF_MESSAGE
}

expect() {
if ! test "${@}"; then
case ${1} in
-[[:alpha:]])
debug "[[ ${1} ACTUAL ]]" "${!#}"
;;
*)
debug "${!#}" "${1}"
;;
esac
return 1
fi
if ! test "${@}"; then
case ${1} in
-[[:alpha:]])
debug "[[ ${1} ACTUAL ]]" "${!#}"
;;
*)
debug "${!#}" "${1}"
;;
esac
return 1
fi
}

compare() {
Expand All @@ -44,7 +44,7 @@ compare() {
bash_getopts_status=${status}
export bash_getopts_output bash_getopts_lines bash_getopts_status

run "getopts_long-${BATS_TEST_DESCRIPTION##* }" ${2}
run "${GETOPTS_LONG_TEST_BIN:-getopts_long}-${BATS_TEST_DESCRIPTION##* }" ${2}
getopts_long_output="${output}"
getopts_long_lines=( "${lines[@]}" )
getopts_long_status=${status}
Expand All @@ -55,6 +55,6 @@ compare() {
getopts_long_output="$(echo "${getopts_long_output}" | sed -E "${3}")"
fi

expect "${bash_getopts_output}" == "${getopts_long_output}"
expect "${bash_getopts_status}" == "${getopts_long_status}"
expect "${getopts_long_output}" == "${bash_getopts_output}"
expect "${getopts_long_status}" == "${bash_getopts_status}"
}
Loading