Zsh Mailing List Archive
Messages sorted by:
Reverse Date,
Date,
Thread,
Author
Re: [BUG] getopts OPTIND
- X-seq: zsh-workers 42249
- From: dana <dana@xxxxxxx>
- To: franciscodezuviria@xxxxxxxxx
- Subject: Re: [BUG] getopts OPTIND
- Date: Tue, 9 Jan 2018 16:48:17 -0600
- Cc: zsh-workers@xxxxxxx
- Dkim-signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=dana-is.20150623.gappssmtp.com; s=20150623; h=mime-version:subject:from:in-reply-to:date:cc :content-transfer-encoding:message-id:references:to; bh=O0Dosq3Ss6xkl8y1ncbfnXGXQoiG0sTfkusG18YQrdA=; b=Qa1YeP3wR4h81eiULzSnPdYlQhejPwxmucvy0mXTHuZLtvkjHSnGN6E7nOlskgmBCY vqdpxqzlQX3qZhikADy4Qdr5bbkfGMbCck79S3sx2lq8z08vwIY3CDOEhQSqLgsFFQFa xTOWA+bv7bwaaOQZGi8EzEjDtawcomYSfgBc+yGahusLtcTREomwyGnaBuc1iK0nbSF2 Yh2EU3LR7SUKBZbBmr+cOHlwjtbUa/U1zjvdNssYmiBqpwGcYUQ4jVqQRs2TyscMaFKF eonPysUP53G0w1kOgaOfhtcqim62ujVtAAQSjTgrCZYTmU9TOF5nbv3u9F+Cp0Em0+na aPlA==
- In-reply-to: <CA++-COw=KgUBJc1PMq=L5be2VeJGXBjwtsRfRx7+7GSMspcQJQ@mail.gmail.com>
- List-help: <mailto:zsh-workers-help@zsh.org>
- List-id: Zsh Workers List <zsh-workers.zsh.org>
- List-post: <mailto:zsh-workers@zsh.org>
- List-unsubscribe: <mailto:zsh-workers-unsubscribe@zsh.org>
- Mailing-list: contact zsh-workers-help@xxxxxxx; run by ezmlm
- References: <CA++-COw=KgUBJc1PMq=L5be2VeJGXBjwtsRfRx7+7GSMspcQJQ@mail.gmail.com>
On 9 Jan 2018, at 10:22, Francisco de Zuviría Allende <franciscodezuviria@xxxxxxxxx> wrote:
>Execution in bash: ... OPTIND is 5, next -a
>execution in zsh: ... OPTIND is 4, next -r
I think this fixes it? At least, zsh gives the same output as bash and dash when
i do this. Peter's left an ominous warning about changes to this function that
deserves recognition though...
dana
diff --git a/Src/builtin.c b/Src/builtin.c
index 0211f2721..2550b29f2 100644
--- a/Src/builtin.c
+++ b/Src/builtin.c
@@ -5437,6 +5437,9 @@ bin_getopts(UNUSED(char *name), char **argv, UNUSED(Options ops), UNUSED(int fun
if(opch == ':' || !(p = memchr(optstr, opch, lenoptstr))) {
p = "?";
err:
+ /* Keep OPTIND correct if the user doesn't return after the error */
+ optcind = 0;
+ zoptind++;
zsfree(zoptarg);
setsparam(var, ztrdup(p));
if(quiet) {
diff --git a/Test/B10getopts.ztst b/Test/B10getopts.ztst
index 7eba5a4b1..f6977accb 100644
--- a/Test/B10getopts.ztst
+++ b/Test/B10getopts.ztst
@@ -79,3 +79,20 @@
test_getopts +x
1:one illegal option, + variant
>test_getopts:3: bad option: +x
+
+ t() { local o; repeat $1 { getopts a: o "${@:2}" 2>&1 }; print -r $OPTIND }
+ t 4 -a -w -e -r -a
+ t 5 -a -w -e -a -w -e
+ t 5 -a -w -e -r -ax -a
+0:OPTIND calculation after error (workers/42248)
+*>*: bad option: -e
+*>*: bad option: -r
+*>*: argument expected after -a option
+>6
+*>*: bad option: -e
+*>*: bad option: -e
+>7
+*>*: bad option: -e
+*>*: bad option: -r
+*>*: argument expected after -a option
+>7
Messages sorted by:
Reverse Date,
Date,
Thread,
Author