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

Re: [BUG] getopts OPTIND



dana wrote on Sun, Apr 18, 2021 at 00:16:52 -0500:
> On 14 Apr 2021, at 08:08, Daniel Shahaf <d.s@xxxxxxxxxxxxxxxxxx> wrote:
> > Some more cases to test:
> > .
> >       t 0
> >       t 1 foo
> >       t 1 -- foo
> >       t 1 -b
> > .
> > where -b doesn't take an argument.
> 
> `t 0` doesn't test anything, the loop is just skipped.

Yeah, but OPTIND still gets printed, so it does test *something*.

> `t 1 -b` tests the same thing as `t 1 -w`, but i guess it's confusing
> that i picked -a -w -e -r as the options; i've changed them to -a -b
> -c -d. I've also added the two foo ones you suggested, as well as just
> `t 1`. (All three behaved the same as other shells already)

*nod*

> +upon exit.  (The tt(POSIX_BUILTINS) option disables this, and also changes
> +the way the value is calculated to match other shells).  tt(OPTARG)

s/)./.)/

Cheers,

Daniel

> +++ b/Test/B10getopts.ztst
> @@ -96,3 +96,32 @@
>    done
>  0:missing option-argument (quiet mode)
>  >:,x
> +
> +  # 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: "
> +    t 1
> +    t 1 foo
> +    t 1 -- foo
> +    t 1 -a
> +    t 1 -b
> +    t 2 -a -b
> +    t 4 -a -b -c -d -a
> +    t 5 -a -b -c -a -b -c
> +    t 5 -a -b -c -d -ax -a
> +    print
> +  ); done
> +0:OPTIND calculation with and without POSIX_BUILTINS (workers/42248)
> +>no_posix_builtins: <1><1><2><1><1><3><5><7><6>
> +>posix_builtins: <1><1><2><2><2><3><6><7><7>
> 
> 




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