about summary refs log tree commit diff
diff options
context:
space:
mode:
authorMartijn Dekker <martijn@inlv.org>2019-06-29 00:17:42 +0200
committerPeter Stephenson <p.stephenson@samsung.com>2019-07-01 09:42:22 +0100
commit700ec49581650ea1f0bffacb207a30145e278417 (patch)
treeb5fb684dd667ee80c7b42fee878ed1aae3645d52
parent4384fdc7f0b5a1de78af344446fcbb90b7182c9e (diff)
downloadzsh-700ec49581650ea1f0bffacb207a30145e278417.tar.gz
zsh-700ec49581650ea1f0bffacb207a30145e278417.tar.xz
zsh-700ec49581650ea1f0bffacb207a30145e278417.zip
44469: correct error on missing option argument
-rw-r--r--ChangeLog5
-rw-r--r--Src/builtin.c17
-rw-r--r--Test/B10getopts.ztst17
3 files changed, 34 insertions, 5 deletions
diff --git a/ChangeLog b/ChangeLog
index 7c1d327d5..1eb2ef24a 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,8 @@
+2019-07-01  Peter Stephenson  <p.stephenson@samsung.com>
+
+	* Martijn: 44469: Src/builtin.c, Test/B10getopts.ztst: correct
+	error on missing option argument.
+
 2019-06-24  Peter Stephenson  <p.w.stephenson@ntlworld.com>
 
 	* 44446: Src/parse.c, Test/A04redirect.ztst: fix here document
diff --git a/Src/builtin.c b/Src/builtin.c
index e863cc4bb..9b9e76c77 100644
--- a/Src/builtin.c
+++ b/Src/builtin.c
@@ -5511,14 +5511,12 @@ bin_getopts(UNUSED(char *name), char **argv, UNUSED(Options ops), UNUSED(int fun
     /* check for legality */
     if(opch == ':' || !(p = memchr(optstr, opch, lenoptstr))) {
 	p = "?";
-    err:
 	zsfree(zoptarg);
 	setsparam(var, ztrdup(p));
 	if(quiet) {
 	    zoptarg = metafy(optbuf, lenoptbuf, META_DUP);
 	} else {
-	    zwarn(*p == '?' ? "bad option: %c%c" :
-		  "argument expected after %c%c option",
+	    zwarn("bad option: %c%c",
 		  "?-+"[lenoptbuf], opch);
 	    zoptarg=ztrdup("");
 	}
@@ -5529,8 +5527,17 @@ bin_getopts(UNUSED(char *name), char **argv, UNUSED(Options ops), UNUSED(int fun
     if(p[1] == ':') {
 	if(optcind == lenstr) {
 	    if(!args[zoptind]) {
-		p = ":";
-		goto err;
+		zsfree(zoptarg);
+		if(quiet) {
+		    setsparam(var, ztrdup(":"));
+		    zoptarg = metafy(optbuf, lenoptbuf, META_DUP);
+		} else {
+		    setsparam(var, ztrdup("?"));
+		    zoptarg = ztrdup("");
+		    zwarn("argument expected after %c%c option",
+			  "?-+"[lenoptbuf], opch);
+		}
+		return 0;
 	    }
 	    p = ztrdup(args[zoptind++]);
 	} else
diff --git a/Test/B10getopts.ztst b/Test/B10getopts.ztst
index 7eba5a4b1..72c9e209e 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
+
+  set -- -x
+  OPTIND=1
+  while getopts x: opt; do
+    echo "$opt,${OPTARG:-Empty}"
+  done
+0:missing option-argument (error message mode)
+>?,Empty
+?(eval):3: argument expected after -x option
+
+  set -- -x
+  OPTIND=1
+  while getopts :x: opt; do
+    echo "$opt,${OPTARG:-Empty}"
+  done
+0:missing option-argument (quiet mode)
+>:,x