diff --git a/CHANGELOG.md b/CHANGELOG.md index ceffac6..1fdd45c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] - Fixes Makefile +- Fixes -f- not to be interpreted as -f -- ## [3.3.1] - 2024-07-30 diff --git a/examples/your-script.sh b/examples/your-script.sh index 9491774..6915cb4 100755 --- a/examples/your-script.sh +++ b/examples/your-script.sh @@ -36,7 +36,7 @@ REST='' # shellcheck disable=SC2004,SC2034,SC2145,SC2194 parse() { OPTIND=$(($#+1)) - while OPTARG= && [ $# -gt 0 ]; do + while OPTARG= && [ "${REST}" != x ] && [ $# -gt 0 ]; do case $1 in --?*=*) OPTARG=$1; shift eval 'set -- "${OPTARG%%\=*}" "${OPTARG#*\=}"' ${1+'"$@"'} @@ -47,7 +47,7 @@ parse() { ;; -[fh]?*) OPTARG=$1; shift eval 'set -- "${OPTARG%"${OPTARG#??}"}" -"${OPTARG#??}"' ${1+'"$@"'} - OPTARG= ;; + [ "$2" = -- ] && set -- "$1" unknown -- && REST=x; OPTARG= ;; esac case $1 in '-f'|'--flag') diff --git a/lib/getoptions_base.sh b/lib/getoptions_base.sh index ddfe76b..8aae4fa 100644 --- a/lib/getoptions_base.sh +++ b/lib/getoptions_base.sh @@ -69,7 +69,7 @@ getoptions() { _0 "${_def:-$2}() {" _1 'OPTIND=$(($#+1))' - _1 'while OPTARG= && [ $# -gt 0 ]; do' + _1 "while OPTARG= && [ \"\${$_rest}\" != x ] && [ \$# -gt 0 ]; do" [ "$_abbr" ] && getoptions_abbr "$@" args() { @@ -138,7 +138,7 @@ getoptions() { _op() { _3 "$1) OPTARG=\$1; shift" _wa '"${OPTARG%"${OPTARG#??}"}" '"$2"'"${OPTARG#??}"' - _4 "$3" + _4 "${4:-}$3" } _3 '--?*=*) OPTARG=$1; shift' _wa '"${OPTARG%%\=*}" "${OPTARG#*\=}"' @@ -146,7 +146,8 @@ getoptions() { _3 "--no-*|--without-*) unset OPTARG ;;" [ "$_alt" ] || { [ "$_opts" ] && _op "-[$_opts]?*" "" ";;" - [ ! "$_flags" ] || _op "-[$_flags]?*" - "OPTARG= ;;" + [ ! "$_flags" ] || _op "-[$_flags]?*" - "OPTARG= ;;" \ + '[ "$2" = -- ] && set -- "$1" unknown -- && '"$_rest=x; " } [ "$_plus" ] && { [ "$_nflags" ] && _op "+[$_nflags]?*" + "unset OPTARG ;;" diff --git a/spec/getoptions_base_spec.sh b/spec/getoptions_base_spec.sh index 6ad8a02..57d7b7c 100644 --- a/spec/getoptions_base_spec.sh +++ b/spec/getoptions_base_spec.sh @@ -131,6 +131,15 @@ Describe "getoptions()" End End + Context "when the combined short option ends in -" + parser_definition() { setup ARGS; flag FLAG -f; } + It "displays error" + When run parse -f- + The stderr should eq "Unrecognized option: --" + The status should be failure + End + End + Context "when specified unknown long option" parser_definition() { setup ARGS; } It "displays error"