diff --git a/lib/getopts_long.bash b/lib/getopts_long.bash index b99ae04..99c2f79 100644 --- a/lib/getopts_long.bash +++ b/lib/getopts_long.bash @@ -20,9 +20,19 @@ 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 + if [[ " ${optspec_long} " == *" ${!optvar}: "* ]]; then OPTARG="${OPTARG#"${!optvar}"}" OPTARG="${OPTARG#=}" @@ -39,7 +49,7 @@ getopts_long() { unset OPTARG && printf -v "${optvar}" '?' fi fi - elif [[ "${optspec_long}" =~ (^|[[:space:]])${!optvar}([[:space:]]|$) ]]; then + elif [[ " ${optspec_long} " == *" ${!optvar} "* ]]; then unset OPTARG else # Invalid option diff --git a/test/bats/github_15.bats b/test/bats/github_15.bats new file mode 100644 index 0000000..71cd9aa --- /dev/null +++ b/test/bats/github_15.bats @@ -0,0 +1,21 @@ +#!/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/invalid_arguments.bats b/test/bats/invalid_arguments.bats index cb4147c..492ecb3 100644 --- a/test/bats/invalid_arguments.bats +++ b/test/bats/invalid_arguments.bats @@ -16,8 +16,8 @@ load ../test_helper compare '-i' \ '--invalid' \ '/^INVALID OPTION -- /d' - expect "${expected_lines[0]}" == 'INVALID OPTION -- OPTARG=i' - expect "${actual_lines[0]}" == 'INVALID OPTION -- OPTARG=invalid' + expect "${bash_getopts_lines[0]}" == 'INVALID OPTION -- OPTARG=i' + expect "${getopts_long_lines[0]}" == 'INVALID OPTION -- OPTARG=invalid' } @test "${FEATURE}: long option, verbose" { compare '-i' \ @@ -41,8 +41,8 @@ load ../test_helper compare '-i user_arg' \ '--invalid user_arg' \ '/^INVALID OPTION -- /d' - expect "${expected_lines[0]}" == 'INVALID OPTION -- OPTARG=i' - expect "${actual_lines[0]}" == 'INVALID OPTION -- OPTARG=invalid' + expect "${bash_getopts_lines[0]}" == 'INVALID OPTION -- OPTARG=i' + expect "${getopts_long_lines[0]}" == 'INVALID OPTION -- OPTARG=invalid' } @test "${FEATURE}: long option, extra arguments, verbose" { compare '-i user_arg' \ @@ -55,28 +55,28 @@ load ../test_helper @test "${FEATURE}: short option, terminator, extra arguments, silent" { compare '-i -- user_arg' \ '-i -- user_arg' - expect "${actual_lines[5]}" == '$@: user_arg' + expect "${getopts_long_lines[5]}" == '$@: user_arg' } @test "${FEATURE}: short option, terminator, extra arguments, verbose" { compare '-i -- user_arg' \ '-i -- user_arg' \ 's/getopts_long-verbose/getopts-verbose/g' - expect "${actual_lines[6]}" == '$@: user_arg' + expect "${getopts_long_lines[6]}" == '$@: user_arg' } @test "${FEATURE}: long option, terminator, extra arguments, silent" { compare '-i -- user_arg' \ '--invalid -- user_arg' \ '/^INVALID OPTION -- /d' - expect "${expected_lines[0]}" == 'INVALID OPTION -- OPTARG=i' - expect "${actual_lines[0]}" == 'INVALID OPTION -- OPTARG=invalid' - expect "${actual_lines[5]}" == '$@: user_arg' + expect "${bash_getopts_lines[0]}" == 'INVALID OPTION -- OPTARG=i' + expect "${getopts_long_lines[0]}" == 'INVALID OPTION -- OPTARG=invalid' + expect "${getopts_long_lines[5]}" == '$@: user_arg' } @test "${FEATURE}: long option, terminator, extra arguments, verbose" { compare '-i -- user_arg' \ '--invalid -- user_arg' \ 's/getopts_long-verbose: (.*) invalid$/getopts-verbose: \1 i/g' - expect "${actual_lines[6]}" == '$@: user_arg' + expect "${getopts_long_lines[6]}" == '$@: user_arg' } # terminator followed by options @@ -84,24 +84,24 @@ load ../test_helper @test "${FEATURE}: terminator, short option, extra arguments, silent" { compare '-- -i user_arg' \ '-- -i user_arg' - expect "${actual_lines[4]}" == '$@: -i user_arg' + expect "${getopts_long_lines[4]}" == '$@: -i user_arg' } @test "${FEATURE}: terminator, short option, extra arguments, verbose" { compare '-- -i user_arg' \ '-- -i user_arg' \ 's/getopts_long-verbose/getopts-verbose/g' - expect "${actual_lines[4]}" == '$@: -i user_arg' + expect "${getopts_long_lines[4]}" == '$@: -i user_arg' } @test "${FEATURE}: terminator, long option, extra arguments, silent" { compare '-- -i user_arg' \ '-- --invalid user_arg' \ '/^\$@: /d' - expect "${actual_lines[4]}" == '$@: --invalid user_arg' + expect "${getopts_long_lines[4]}" == '$@: --invalid user_arg' } @test "${FEATURE}: terminator, long option, extra arguments, verbose" { compare '-- -i user_arg' \ '-- --invalid user_arg' \ '/^\$@: /d' - expect "${actual_lines[4]}" == '$@: --invalid user_arg' + expect "${getopts_long_lines[4]}" == '$@: --invalid user_arg' } diff --git a/test/bats/no_arguments.bats b/test/bats/no_arguments.bats index 6660472..95c29b4 100644 --- a/test/bats/no_arguments.bats +++ b/test/bats/no_arguments.bats @@ -21,10 +21,10 @@ load ../test_helper @test "${FEATURE}: terminator, extra arguments, silent" { compare '-- user_arg' \ '-- user_arg' - expect "${actual_lines[4]}" == '$@: user_arg' + expect "${getopts_long_lines[4]}" == '$@: user_arg' } @test "${FEATURE}: terminator, extra arguments, verbose" { compare '-- user_arg' \ '-- user_arg' - expect "${actual_lines[4]}" == '$@: user_arg' + expect "${getopts_long_lines[4]}" == '$@: user_arg' } diff --git a/test/bats/option_supplied.bats b/test/bats/option_supplied.bats index bccb796..658329f 100644 --- a/test/bats/option_supplied.bats +++ b/test/bats/option_supplied.bats @@ -45,23 +45,23 @@ load ../test_helper @test "${FEATURE}: short option, terminator, extra arguments, silent" { compare '-o user_val -- user_arg' \ '-o user_val -- user_arg' - expect "${actual_lines[5]}" == '$@: user_arg' + expect "${getopts_long_lines[5]}" == '$@: user_arg' } @test "${FEATURE}: short option, terminator, extra arguments, verbose" { compare '-o user_val -- user_arg' \ '-o user_val -- user_arg' - expect "${actual_lines[5]}" == '$@: user_arg' + expect "${getopts_long_lines[5]}" == '$@: user_arg' } @test "${FEATURE}: long option, terminator, extra arguments, silent" { compare '-o user_val -- user_arg' \ '--option user_val -- user_arg' - expect "${actual_lines[5]}" == '$@: user_arg' + expect "${getopts_long_lines[5]}" == '$@: user_arg' } @test "${FEATURE}: long option, terminator, extra arguments, verbose" { compare '-o user_val -- user_arg' \ '--option user_val -- user_arg' - expect "${actual_lines[5]}" == '$@: user_arg' + expect "${getopts_long_lines[5]}" == '$@: user_arg' } # multiple same arguments @@ -69,27 +69,27 @@ load ../test_helper @test "${FEATURE}: short option, multiple same arguments, silent" { compare '-o user_val1 -o user_val2' \ '-o user_val1 -o user_val2' - expect "${actual_lines[0]}" == 'option supplied -- OPTARG=user_val1' - expect "${actual_lines[1]}" == 'option supplied -- OPTARG=user_val2' + expect "${getopts_long_lines[0]}" == 'option supplied -- OPTARG=user_val1' + expect "${getopts_long_lines[1]}" == 'option supplied -- OPTARG=user_val2' } @test "${FEATURE}: short option, multiple same arguments, verbose" { compare '-o user_val1 -o user_val2' \ '-o user_val1 -o user_val2' - expect "${actual_lines[0]}" == 'option supplied -- OPTARG=user_val1' - expect "${actual_lines[1]}" == 'option supplied -- OPTARG=user_val2' + expect "${getopts_long_lines[0]}" == 'option supplied -- OPTARG=user_val1' + expect "${getopts_long_lines[1]}" == 'option supplied -- OPTARG=user_val2' } @test "${FEATURE}: long option, multiple same arguments, silent" { compare '-o user_val1 -o user_val2' \ '--option user_val1 --option user_val2' - expect "${actual_lines[0]}" == 'option supplied -- OPTARG=user_val1' - expect "${actual_lines[1]}" == 'option supplied -- OPTARG=user_val2' + expect "${getopts_long_lines[0]}" == 'option supplied -- OPTARG=user_val1' + expect "${getopts_long_lines[1]}" == 'option supplied -- OPTARG=user_val2' } @test "${FEATURE}: long option, multiple same arguments, verbose" { compare '-o user_val1 -o user_val2' \ '--option user_val1 --option user_val2' - expect "${actual_lines[0]}" == 'option supplied -- OPTARG=user_val1' - expect "${actual_lines[1]}" == 'option supplied -- OPTARG=user_val2' + expect "${getopts_long_lines[0]}" == 'option supplied -- OPTARG=user_val1' + expect "${getopts_long_lines[1]}" == 'option supplied -- OPTARG=user_val2' } # terminator followed by options @@ -97,25 +97,25 @@ load ../test_helper @test "${FEATURE}: terminator, short option, extra arguments, silent" { compare '-- -o user_val user_arg' \ '-- -o user_val user_arg' - expect "${actual_lines[4]}" == '$@: -o user_val user_arg' + expect "${getopts_long_lines[4]}" == '$@: -o user_val user_arg' } @test "${FEATURE}: terminator, short option, extra arguments, verbose" { compare '-- -o user_val user_arg' \ '-- -o user_val user_arg' - expect "${actual_lines[4]}" == '$@: -o user_val user_arg' + expect "${getopts_long_lines[4]}" == '$@: -o user_val user_arg' } @test "${FEATURE}: terminator, long option, extra arguments, silent" { compare '-- -o user_val user_arg' \ '-- --option user_val user_arg' \ '/^\$@: /d' - expect "${actual_lines[4]}" == '$@: --option user_val user_arg' + expect "${getopts_long_lines[4]}" == '$@: --option user_val user_arg' } @test "${FEATURE}: terminator, long option, extra arguments, verbose" { compare '-- -o user_val user_arg' \ '-- --option user_val user_arg' \ '/^\$@: /d' - expect "${actual_lines[4]}" == '$@: --option user_val user_arg' + expect "${getopts_long_lines[4]}" == '$@: --option user_val user_arg' } # option without an argument @@ -134,8 +134,8 @@ load ../test_helper compare '-o' \ '--option' \ '/^MISSING ARGUMENT -- /d' - expect "${expected_lines[0]}" == 'MISSING ARGUMENT -- OPTARG=o' - expect "${actual_lines[0]}" == 'MISSING ARGUMENT -- OPTARG=option' + expect "${bash_getopts_lines[0]}" == 'MISSING ARGUMENT -- OPTARG=o' + expect "${getopts_long_lines[0]}" == 'MISSING ARGUMENT -- OPTARG=option' } @test "${FEATURE}: long option, missing value, verbose" { compare '-o' \ @@ -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' +} diff --git a/test/bats/toggle_triggered.bats b/test/bats/toggle_triggered.bats index 2738982..a98e287 100644 --- a/test/bats/toggle_triggered.bats +++ b/test/bats/toggle_triggered.bats @@ -45,23 +45,23 @@ load ../test_helper @test "${FEATURE}: short option, terminator, extra arguments, silent" { compare '-t -- user_arg' \ '-t -- user_arg' - expect "${actual_lines[5]}" == '$@: user_arg' + expect "${getopts_long_lines[5]}" == '$@: user_arg' } @test "${FEATURE}: short option, terminator, extra arguments, verbose" { compare '-t -- user_arg' \ '-t -- user_arg' - expect "${actual_lines[5]}" == '$@: user_arg' + expect "${getopts_long_lines[5]}" == '$@: user_arg' } @test "${FEATURE}: long option, terminator, extra arguments, silent" { compare '-t -- user_arg' \ '--toggle -- user_arg' - expect "${actual_lines[5]}" == '$@: user_arg' + expect "${getopts_long_lines[5]}" == '$@: user_arg' } @test "${FEATURE}: long option, terminator, extra arguments, verbose" { compare '-t -- user_arg' \ '--toggle -- user_arg' - expect "${actual_lines[5]}" == '$@: user_arg' + expect "${getopts_long_lines[5]}" == '$@: user_arg' } # multiple same arguments @@ -69,27 +69,27 @@ load ../test_helper @test "${FEATURE}: short option, multiple same arguments, silent" { compare '-t -t' \ '-t -t' - expect "${actual_lines[0]}" == "${actual_lines[1]}" - expect "${actual_lines[0]}" == 'toggle triggered -- OPTARG is unset' + expect "${getopts_long_lines[0]}" == "${getopts_long_lines[1]}" + expect "${getopts_long_lines[0]}" == 'toggle triggered -- OPTARG is unset' } @test "${FEATURE}: short option, multiple same arguments, verbose" { compare '-t -t' \ '-t -t' - expect "${actual_lines[0]}" == "${actual_lines[1]}" - expect "${actual_lines[0]}" == 'toggle triggered -- OPTARG is unset' + expect "${getopts_long_lines[0]}" == "${getopts_long_lines[1]}" + expect "${getopts_long_lines[0]}" == 'toggle triggered -- OPTARG is unset' } @test "${FEATURE}: long option, multiple same arguments, silent" { compare '-t -t' \ '--toggle --toggle' - expect "${actual_lines[0]}" == "${actual_lines[1]}" - expect "${actual_lines[0]}" == 'toggle triggered -- OPTARG is unset' + expect "${getopts_long_lines[0]}" == "${getopts_long_lines[1]}" + expect "${getopts_long_lines[0]}" == 'toggle triggered -- OPTARG is unset' } @test "${FEATURE}: long option, multiple same arguments, verbose" { compare '-t -t' \ '--toggle --toggle' - expect "${actual_lines[0]}" == "${actual_lines[1]}" - expect "${actual_lines[0]}" == 'toggle triggered -- OPTARG is unset' + expect "${getopts_long_lines[0]}" == "${getopts_long_lines[1]}" + expect "${getopts_long_lines[0]}" == 'toggle triggered -- OPTARG is unset' } # terminator followed by options @@ -97,23 +97,23 @@ load ../test_helper @test "${FEATURE}: terminator, short option, extra arguments, silent" { compare '-- -t user_arg' \ '-- -t user_arg' - expect "${actual_lines[4]}" == '$@: -t user_arg' + expect "${getopts_long_lines[4]}" == '$@: -t user_arg' } @test "${FEATURE}: terminator, short option, extra arguments, verbose" { compare '-- -t user_arg' \ '-- -t user_arg' - expect "${actual_lines[4]}" == '$@: -t user_arg' + expect "${getopts_long_lines[4]}" == '$@: -t user_arg' } @test "${FEATURE}: terminator, long option, extra arguments, silent" { compare '-- -t user_arg' \ '-- --toggle user_arg' \ '/^\$@: /d' - expect "${actual_lines[4]}" == '$@: --toggle user_arg' + expect "${getopts_long_lines[4]}" == '$@: --toggle user_arg' } @test "${FEATURE}: terminator, long option, extra arguments, verbose" { compare '-- -t user_arg' \ '-- --toggle user_arg' \ '/^\$@: /d' - expect "${actual_lines[4]}" == '$@: --toggle user_arg' + expect "${getopts_long_lines[4]}" == '$@: --toggle user_arg' } diff --git a/test/bats/value_supplied.bats b/test/bats/value_supplied.bats index 7ef80b4..2ee34d4 100644 --- a/test/bats/value_supplied.bats +++ b/test/bats/value_supplied.bats @@ -15,15 +15,15 @@ load ../test_helper compare '-v user_val' \ '--variable=user_val' \ '/^OPTIND: /d' - expect "${expected_lines[4]}" == 'OPTIND: 3' - expect "${actual_lines[4]}" == 'OPTIND: 2' + expect "${bash_getopts_lines[4]}" == 'OPTIND: 3' + expect "${getopts_long_lines[4]}" == 'OPTIND: 2' } @test "${FEATURE}: long option, verbose" { compare '-v user_val' \ '--variable=user_val' \ '/^OPTIND: /d' - expect "${expected_lines[4]}" == 'OPTIND: 3' - expect "${actual_lines[4]}" == 'OPTIND: 2' + expect "${bash_getopts_lines[4]}" == 'OPTIND: 3' + expect "${getopts_long_lines[4]}" == 'OPTIND: 2' } # extra arguments @@ -41,15 +41,15 @@ load ../test_helper compare '-v user_val user_arg' \ '--variable=user_val user_arg' \ '/^OPTIND: /d' - expect "${expected_lines[4]}" == 'OPTIND: 3' - expect "${actual_lines[4]}" == 'OPTIND: 2' + expect "${bash_getopts_lines[4]}" == 'OPTIND: 3' + expect "${getopts_long_lines[4]}" == 'OPTIND: 2' } @test "${FEATURE}: long option, extra arguments, verbose" { compare '-v user_val user_arg' \ '--variable=user_val user_arg' \ '/^OPTIND: /d' - expect "${expected_lines[4]}" == 'OPTIND: 3' - expect "${actual_lines[4]}" == 'OPTIND: 2' + expect "${bash_getopts_lines[4]}" == 'OPTIND: 3' + expect "${getopts_long_lines[4]}" == 'OPTIND: 2' } # extra arguments with terminator @@ -57,29 +57,29 @@ load ../test_helper @test "${FEATURE}: short option, terminator, extra arguments, silent" { compare '-v user_val -- user_arg' \ '-v user_val -- user_arg' - expect "${actual_lines[5]}" == '$@: user_arg' + expect "${getopts_long_lines[5]}" == '$@: user_arg' } @test "${FEATURE}: short option, terminator, extra arguments, verbose" { compare '-v user_val -- user_arg' \ '-v user_val -- user_arg' - expect "${actual_lines[5]}" == '$@: user_arg' + expect "${getopts_long_lines[5]}" == '$@: user_arg' } @test "${FEATURE}: long option, terminator, extra arguments, silent" { compare '-v user_val -- user_arg' \ '--variable=user_val -- user_arg' \ '/^OPTIND: /d' - expect "${actual_lines[5]}" == '$@: user_arg' - expect "${expected_lines[4]}" == 'OPTIND: 4' - expect "${actual_lines[4]}" == 'OPTIND: 3' + expect "${getopts_long_lines[5]}" == '$@: user_arg' + expect "${bash_getopts_lines[4]}" == 'OPTIND: 4' + expect "${getopts_long_lines[4]}" == 'OPTIND: 3' } @test "${FEATURE}: long option, terminator, extra arguments, verbose" { compare '-v user_val -- user_arg' \ '--variable=user_val -- user_arg' \ '/^OPTIND: /d' - expect "${actual_lines[5]}" == '$@: user_arg' - expect "${expected_lines[4]}" == 'OPTIND: 4' - expect "${actual_lines[4]}" == 'OPTIND: 3' + expect "${getopts_long_lines[5]}" == '$@: user_arg' + expect "${bash_getopts_lines[4]}" == 'OPTIND: 4' + expect "${getopts_long_lines[4]}" == 'OPTIND: 3' } # multiple same arguments @@ -87,33 +87,33 @@ load ../test_helper @test "${FEATURE}: short option, multiple same arguments, silent" { compare '-v user_val1 -v user_val2' \ '-v user_val1 -v user_val2' - expect "${actual_lines[0]}" == 'value supplied -- OPTARG=user_val1' - expect "${actual_lines[1]}" == 'value supplied -- OPTARG=user_val2' + expect "${getopts_long_lines[0]}" == 'value supplied -- OPTARG=user_val1' + expect "${getopts_long_lines[1]}" == 'value supplied -- OPTARG=user_val2' } @test "${FEATURE}: short option, multiple same arguments, verbose" { compare '-v user_val1 -v user_val2' \ '-v user_val1 -v user_val2' - expect "${actual_lines[0]}" == 'value supplied -- OPTARG=user_val1' - expect "${actual_lines[1]}" == 'value supplied -- OPTARG=user_val2' + expect "${getopts_long_lines[0]}" == 'value supplied -- OPTARG=user_val1' + expect "${getopts_long_lines[1]}" == 'value supplied -- OPTARG=user_val2' } @test "${FEATURE}: long option, multiple same arguments, silent" { compare '-v user_val1 -v user_val2' \ '--variable=user_val1 --variable=user_val2' \ '/^OPTIND: /d' - expect "${actual_lines[0]}" == 'value supplied -- OPTARG=user_val1' - expect "${actual_lines[1]}" == 'value supplied -- OPTARG=user_val2' - expect "${expected_lines[5]}" == 'OPTIND: 5' - expect "${actual_lines[5]}" == 'OPTIND: 3' + expect "${getopts_long_lines[0]}" == 'value supplied -- OPTARG=user_val1' + expect "${getopts_long_lines[1]}" == 'value supplied -- OPTARG=user_val2' + expect "${bash_getopts_lines[5]}" == 'OPTIND: 5' + expect "${getopts_long_lines[5]}" == 'OPTIND: 3' } @test "${FEATURE}: long option, multiple same arguments, verbose" { compare '-v user_val1 -v user_val2' \ '--variable=user_val1 --variable=user_val2' \ '/^OPTIND: /d' - expect "${actual_lines[0]}" == 'value supplied -- OPTARG=user_val1' - expect "${actual_lines[1]}" == 'value supplied -- OPTARG=user_val2' - expect "${expected_lines[5]}" == 'OPTIND: 5' - expect "${actual_lines[5]}" == 'OPTIND: 3' + expect "${getopts_long_lines[0]}" == 'value supplied -- OPTARG=user_val1' + expect "${getopts_long_lines[1]}" == 'value supplied -- OPTARG=user_val2' + expect "${bash_getopts_lines[5]}" == 'OPTIND: 5' + expect "${getopts_long_lines[5]}" == 'OPTIND: 3' } # terminator followed by options @@ -121,25 +121,25 @@ load ../test_helper @test "${FEATURE}: terminator, short option, extra arguments, silent" { compare '-- -v user_val user_arg' \ '-- -v user_val user_arg' - expect "${actual_lines[4]}" == '$@: -v user_val user_arg' + expect "${getopts_long_lines[4]}" == '$@: -v user_val user_arg' } @test "${FEATURE}: terminator, short option, extra arguments, verbose" { compare '-- -v user_val user_arg' \ '-- -v user_val user_arg' - expect "${actual_lines[4]}" == '$@: -v user_val user_arg' + expect "${getopts_long_lines[4]}" == '$@: -v user_val user_arg' } @test "${FEATURE}: terminator, long option, extra arguments, silent" { compare '-- -v user_val user_arg' \ '-- --variable=user_val user_arg' \ '/^\$@: /d' - expect "${actual_lines[4]}" == '$@: --variable=user_val user_arg' + expect "${getopts_long_lines[4]}" == '$@: --variable=user_val user_arg' } @test "${FEATURE}: terminator, long option, extra arguments, verbose" { compare '-- -v user_val user_arg' \ '-- --variable=user_val user_arg' \ '/^\$@: /d' - expect "${actual_lines[4]}" == '$@: --variable=user_val user_arg' + expect "${getopts_long_lines[4]}" == '$@: --variable=user_val user_arg' } # variable without an argument @@ -158,8 +158,8 @@ load ../test_helper compare '-v' \ '--variable' \ '/^MISSING ARGUMENT -- /d' - expect "${expected_lines[0]}" == 'MISSING ARGUMENT -- OPTARG=v' - expect "${actual_lines[0]}" == 'MISSING ARGUMENT -- OPTARG=variable' + expect "${bash_getopts_lines[0]}" == 'MISSING ARGUMENT -- OPTARG=v' + expect "${getopts_long_lines[0]}" == 'MISSING ARGUMENT -- OPTARG=variable' } @test "${FEATURE}: long option, missing value, verbose" { compare '-v' \ @@ -182,15 +182,15 @@ load ../test_helper compare '-v -user_val' \ '--variable=-user_val' \ '/^OPTIND: /d' - expect "${expected_lines[4]}" == 'OPTIND: 3' - expect "${actual_lines[4]}" == 'OPTIND: 2' + expect "${bash_getopts_lines[4]}" == 'OPTIND: 3' + expect "${getopts_long_lines[4]}" == 'OPTIND: 2' } @test "${FEATURE}: long option, value starts with -, verbose" { compare '-v -user_val' \ '--variable=-user_val' \ '/^OPTIND: /d' - expect "${expected_lines[4]}" == 'OPTIND: 3' - expect "${actual_lines[4]}" == 'OPTIND: 2' + expect "${bash_getopts_lines[4]}" == 'OPTIND: 3' + expect "${getopts_long_lines[4]}" == 'OPTIND: 2' } # option with a value that start with an equals sign @@ -208,13 +208,13 @@ load ../test_helper compare '-v =user_val' \ '--variable==user_val' \ '/^OPTIND: /d' - expect "${expected_lines[4]}" == 'OPTIND: 3' - expect "${actual_lines[4]}" == 'OPTIND: 2' + expect "${bash_getopts_lines[4]}" == 'OPTIND: 3' + expect "${getopts_long_lines[4]}" == 'OPTIND: 2' } @test "${FEATURE}: long option, value starts with =, verbose" { compare '-v =user_val' \ '--variable==user_val' \ '/^OPTIND: /d' - expect "${expected_lines[4]}" == 'OPTIND: 3' - expect "${actual_lines[4]}" == 'OPTIND: 2' + expect "${bash_getopts_lines[4]}" == 'OPTIND: 3' + expect "${getopts_long_lines[4]}" == 'OPTIND: 2' } diff --git a/test/test_helper.bash b/test/test_helper.bash index 79b6941..bf7abdc 100644 --- a/test/test_helper.bash +++ b/test/test_helper.bash @@ -7,8 +7,17 @@ FEATURE="$(basename "${BATS_TEST_FILENAME}" '.bats' | tr '_' ' ')" PATH="${TOPDIR}/bin:${PATH}" debug() { - printf '\nACTUAL:\n––––––––\n%s\n' "${2}" >&3 - printf '\nEXPECTED:\n––––––––\n%s\n\n' "${1}" >&3 +cat >&3 <