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

[PATCH] zparseopts -G: always add options with optional args to array with =



this is a fix for my earlier `zparseopts -G` changes that i was sitting on for
for some reason:

my intention (and what i documented) was that long options with optional
arguments would always get added to the array with the =, even if the optarg
wasn't given. but i missed that in both the code and the tests. this fixes it

dana


diff --git a/Src/Modules/zutil.c b/Src/Modules/zutil.c
index 9b2721a09..ef99303d2 100644
--- a/Src/Modules/zutil.c
+++ b/Src/Modules/zutil.c
@@ -1662,15 +1662,15 @@ add_opt_val(Zoptdesc d, char *arg)
 	v->str = NULL;
 	if (d->arr)
 	    d->arr->num += (arg ? 2 : 1);
-    } else if (arg) {
+    } else if (arg || d->flags & ZOF_GNUL) {
 	/* 3 here is '-' + '=' + NUL */
-	char *s = (char *) zhalloc(strlen(d->name) + strlen(arg) + 3);
+	char *s = (char *) zhalloc(strlen(d->name) + strlen(arg ? arg : "") + 3);
 
 	*s = '-';
 	strcpy(s + 1, d->name);
 	if (d->flags & ZOF_GNUL)
 	    strcat(s, "=");
-	strcat(s, arg);
+	strcat(s, arg ? arg : "");
 	v->str = s;
 	if (d->arr)
 	    d->arr->num += 1;
diff --git a/Test/V12zparseopts.ztst b/Test/V12zparseopts.ztst
index a2743ea0e..e465d0e0c 100644
--- a/Test/V12zparseopts.ztst
+++ b/Test/V12zparseopts.ztst
@@ -247,7 +247,7 @@
 0:zparseopts -G, separate parameters
 >ret: 0, optv: --foo bar, argv: 1 2 3
 >ret: 0, optv: --foo=bar, argv: 1 2 3
->ret: 0, optv: --foo, argv: bar 1 2 3
+>ret: 0, optv: --foo=, argv: bar 1 2 3
 
   for spec in -foo: -foo:- -foo::; do
     () {
@@ -340,4 +340,4 @@
 ?(anon):zparseopts:2: bad option: -f
 >ret: 1, gopt: -G, optv: , argv: -foobar 1 2 3
 >ret: 0, gopt: -G, optv: -foo=bar, argv: 1 2 3
->ret: 0, gopt: -G, optv: -foo, argv: bar 1 2 3
+>ret: 0, gopt: -G, optv: -foo=, argv: bar 1 2 3




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