-
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[GH-26] Added tests to ensure that long options can start with a hyph…
…en character (#29) This resolves #26 by ensuring that it is possible to define the hyphen (`-`) and options starting with a hyphen (`-XXX`) as valid long options in getopts_long OPTSPEC. On the command line, this allows getopts_long to support both: the `---` as a standalone option, and options starting with three hyphens, such as `—XXX`.
- Loading branch information
Showing
8 changed files
with
227 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
#!/usr/bin/env bats | ||
|
||
load ../test_helper | ||
export GETOPTS_LONG_TEST_BIN='getopts_long-longspec_with_dash' | ||
|
||
@test "${FEATURE}: toggle, silent" { | ||
compare '-t' \ | ||
'---' | ||
} | ||
@test "${FEATURE}: toggle, verbose" { | ||
compare '-t' \ | ||
'---' | ||
} | ||
|
||
@test "${FEATURE}: double toggle, silent" { | ||
compare '-tt' \ | ||
'--- --toggle' \ | ||
'/^OPTIND: /d' | ||
expect "${bash_getopts[6]}" == 'OPTIND: 2' | ||
expect "${getopts_long[6]}" == 'OPTIND: 3' | ||
} | ||
@test "${FEATURE}: double toggle, verbose" { | ||
compare '-tt' \ | ||
'--- --toggle' \ | ||
'/^OPTIND: /d' | ||
expect "${bash_getopts[6]}" == 'OPTIND: 2' | ||
expect "${getopts_long[6]}" == 'OPTIND: 3' | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
#!/usr/bin/env bats | ||
|
||
load ../test_helper | ||
export GETOPTS_LONG_TEST_BIN='getopts_long-longspec_with_dash_colon' | ||
|
||
@test "${FEATURE}: option, silent" { | ||
compare '-o user_val' \ | ||
'--- user_val' | ||
} | ||
@test "${FEATURE}: option, verbose" { | ||
compare '-o user_val' \ | ||
'--- user_val' | ||
} | ||
|
||
# geopts_long does not support option-value adjoined syntax for long options. | ||
# Thus, let's compare it to bash getopts command that uses an invalid option. | ||
@test "${FEATURE}: option with adjacent value, silent" { | ||
compare '-z' \ | ||
'---zz' \ | ||
'/^INVALID OPTION/d' | ||
expect "${bash_getopts[1]}" == 'INVALID OPTION -- OPTARG=z' | ||
expect "${getopts_long[1]}" == 'INVALID OPTION -- OPTARG=-zz' | ||
} | ||
@test "${FEATURE}: option with adjacent value, verbose" { | ||
compare '-z' \ | ||
'---zz' \ | ||
'/-verbose: illegal option --/d' | ||
expect "${bash_getopts[1]}" =~ '-verbose: illegal option -- z' | ||
expect "${getopts_long[1]}" =~ '-verbose: illegal option -- -zz' | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
#!/usr/bin/env bats | ||
|
||
load ../test_helper | ||
export GETOPTS_LONG_TEST_BIN='getopts_long-github_26' | ||
|
||
@test "${FEATURE}: toggle, silent" { | ||
compare '-t user_arg' \ | ||
'---toggle user_arg' | ||
} | ||
@test "${FEATURE}: toggle, verbose" { | ||
compare '-t user_arg' \ | ||
'---toggle user_arg' | ||
} | ||
|
||
@test "${FEATURE}: option, silent" { | ||
compare '-o user_val' \ | ||
'---option user_val' | ||
} | ||
@test "${FEATURE}: option, verbose" { | ||
compare '-o user_val' \ | ||
'---option user_val' | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
#!/usr/bin/env bash | ||
|
||
TOPDIR="$(cd "$(dirname "${0}")"/../.. && pwd)" | ||
# shellcheck disable=SC1090 | ||
source "${TOPDIR}/lib/getopts_long.bash" | ||
|
||
while getopts_long ':to:v: toggle option: variable: -toggle -option:' OPTKEY; do | ||
case ${OPTKEY} in | ||
't'|'toggle'|'-toggle') | ||
printf 'toggle triggered' | ||
;; | ||
'-'|'o'|'option'|'-option') | ||
printf 'option supplied' | ||
;; | ||
'v'|'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 )) | ||
|
||
echo "OPTERR: ${OPTERR}" | ||
echo "OPTKEY: ${OPTKEY}" | ||
echo "OPTARG: ${OPTARG}" | ||
echo "OPTIND: ${OPTIND}" | ||
echo "\$@: ${*}" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
#!/usr/bin/env bash | ||
|
||
TOPDIR="$(cd "$(dirname "${0}")"/../.. && pwd)" | ||
# shellcheck disable=SC1090 | ||
source "${TOPDIR}/lib/getopts_long.bash" | ||
|
||
while getopts_long 'to:v: toggle option: variable: -toggle -option:' OPTKEY; do | ||
case ${OPTKEY} in | ||
't'|'toggle'|'-toggle') | ||
printf 'toggle triggered' | ||
;; | ||
'-'|'o'|'option'|'-option') | ||
printf 'option supplied' | ||
;; | ||
'v'|'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 )) | ||
|
||
echo "OPTERR: ${OPTERR}" | ||
echo "OPTKEY: ${OPTKEY}" | ||
echo "OPTARG: ${OPTARG}" | ||
echo "OPTIND: ${OPTIND}" | ||
echo "\$@: ${*}" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
#!/usr/bin/env bash | ||
|
||
TOPDIR="$(cd "$(dirname "${0}")"/../.. && pwd)" | ||
# shellcheck disable=SC1090 | ||
source "${TOPDIR}/lib/getopts_long.bash" | ||
|
||
while getopts_long ':to:v: -: toggle option: variable:' OPTKEY; do | ||
case ${OPTKEY} in | ||
't'|'toggle') | ||
printf 'toggle triggered' | ||
;; | ||
'-'|'o'|'option') | ||
printf 'option supplied' | ||
;; | ||
'v'|'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 )) | ||
|
||
echo "OPTERR: ${OPTERR}" | ||
echo "OPTKEY: ${OPTKEY}" | ||
echo "OPTARG: ${OPTARG}" | ||
echo "OPTIND: ${OPTIND}" | ||
echo "\$@: ${*}" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
#!/usr/bin/env bash | ||
|
||
TOPDIR="$(cd "$(dirname "${0}")"/../.. && pwd)" | ||
# shellcheck disable=SC1090 | ||
source "${TOPDIR}/lib/getopts_long.bash" | ||
|
||
while getopts_long 'to:v: -: toggle option: variable:' OPTKEY; do | ||
case ${OPTKEY} in | ||
't'|'toggle') | ||
printf 'toggle triggered' | ||
;; | ||
'-'|'o'|'option') | ||
printf 'option supplied' | ||
;; | ||
'v'|'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 )) | ||
|
||
echo "OPTERR: ${OPTERR}" | ||
echo "OPTKEY: ${OPTKEY}" | ||
echo "OPTARG: ${OPTARG}" | ||
echo "OPTIND: ${OPTIND}" | ||
echo "\$@: ${*}" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters