Skip to content

Commit

Permalink
Fixes -f- not to be interpreted as -f --
Browse files Browse the repository at this point in the history
  • Loading branch information
ko1nksm committed Aug 8, 2024
1 parent 193f8e9 commit db06151
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
4 changes: 2 additions & 2 deletions examples/your-script.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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+'"$@"'}
Expand All @@ -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')
Expand Down
7 changes: 4 additions & 3 deletions lib/getoptions_base.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -138,15 +138,16 @@ getoptions() {
_op() {
_3 "$1) OPTARG=\$1; shift"
_wa '"${OPTARG%"${OPTARG#??}"}" '"$2"'"${OPTARG#??}"'
_4 "$3"
_4 "${4:-}$3"
}
_3 '--?*=*) OPTARG=$1; shift'
_wa '"${OPTARG%%\=*}" "${OPTARG#*\=}"'
_4 ";;"
_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 ;;"
Expand Down
9 changes: 9 additions & 0 deletions spec/getoptions_base_spec.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit db06151

Please sign in to comment.