Zsh Mailing List Archive
Messages sorted by:
Reverse Date,
Date,
Thread,
Author
Re: [BUG] getopts OPTIND - yash's behaviour
- X-seq: zsh-workers 48567
- From: Daniel Shahaf <d.s@xxxxxxxxxxxxxxxxxx>
- To: dana <dana@xxxxxxx>
- Cc: Zsh hackers list <zsh-workers@xxxxxxx>, franciscodezuviria@xxxxxxxxx
- Subject: Re: [BUG] getopts OPTIND - yash's behaviour
- Date: Wed, 14 Apr 2021 13:04:27 +0000
- Archived-at: <https://zsh.org/workers/48567>
- In-reply-to: <C8AAF10F-8FF7-47D6-8760-5960D62E91F7@dana.is>
- List-id: <zsh-workers.zsh.org>
- References: <CA++-COw=KgUBJc1PMq=L5be2VeJGXBjwtsRfRx7+7GSMspcQJQ@mail.gmail.com> <0877C4E8-4CA3-453F-A16B-99E576F60E8D@dana.is> <C8AAF10F-8FF7-47D6-8760-5960D62E91F7@dana.is>
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