diff --git a/builtin/pure_osh.py b/builtin/pure_osh.py index a0b3578006..8c901ef710 100644 --- a/builtin/pure_osh.py +++ b/builtin/pure_osh.py @@ -506,6 +506,10 @@ def _GetOpts( my_state.Fail() return 1, '?' + if current == "--": # special case, stop processing remaining args + my_state.IncIndex() + return 1, '?' + flag_char = current[my_state.flag_pos] if my_state.flag_pos < len(current) - 1: diff --git a/spec/builtin-getopts.test.sh b/spec/builtin-getopts.test.sh index a35f1f846a..2220675f90 100644 --- a/spec/builtin-getopts.test.sh +++ b/spec/builtin-getopts.test.sh @@ -389,3 +389,47 @@ err:? err:? err:? ## END + +#### getopts handles '--' #2579 +set -- "-a" "--" +while getopts "a" name; do + case "$name" in + a) + echo "a" + ;; + ?) + echo "?" + ;; + esac +done +echo "name=$name" +echo "$OPTIND" +## STDOUT: +a +name=? +3 +## END + +#### getopts leaves all args after '--' as operands #2579 +set -- "-a" "--" "-c" "operand" +while getopts "a" name; do + case "$name" in + a) + echo "a" + ;; + c) + echo "c" + ;; + ?) + echo "?" + ;; + esac +done +shift $((OPTIND - 1)) +echo "$#" +echo "$@" +## STDOUT: +a +2 +-c operand +## END