Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: [BUG] getopts OPTIND - yash's behaviour



dana wrote on Tue, Apr 13, 2021 at 18:28:50 -0500:
> +  # This function is written so it can be easily referenced against other shells
> +  t() {
> +    local o i=0 n=$1
> +    shift
> +    while [ $i -lt $n ]; do
> +      i=$(( i + 1 ))
> +      getopts a: o "$@" 2> /dev/null
> +    done
> +    printf '<%d>' "$OPTIND"
> +  }
> +  # Try all these the native way, then the POSIX_BUILTINS way
> +  for 1 in no_posix_builtins posix_builtins; do (
> +    setopt $1
> +    print -rn - "$1: "
> +    <3> t 1 -a

I'm guessing yash notices that -a (at index 1) takes an argument, so it
goes to the top of the loop for the word at index 2, where it increments
OPTIND before noticing that index 2 is actually past the end of the
array.

> +    <3> t 1 -w

I get "1:2" in yash 2.51 interactively.

> +    <3> t 2 -a -w

Consistent with other shells.

> +    <7> t 4 -a -w -e -r -a

- The first word is the -a option.

- The second word is the argument to the -a option.

- The third word is -w, so this call will return an error.  Before
  returning, since the next word to parse is the fourth one, set OPTIND
  to 4.

> +    <7> t 5 -a -w -e -a -w -e

Consistent with other shells.

> +    <7> t 5 -a -w -e -r -ax -a

Consistent with other shells.

> +    print
> +  ); done
> +0:OPTIND calculation with and without POSIX_BUILTINS (workers/42248)
> +>no_posix_builtins: <1><1><3><5><7><6>
> +>posix_builtins: <2><2><3><6><7><7>
> 
> 




Messages sorted by: Reverse Date, Date, Thread, Author