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

[PATCH] zparseopts: allow omitting array



it's always been an error if none of -a, -A, or at least one spec with =array
is given to zparseopts. but i don't see why this needs to be the case. there
are situations where you just don't care about storing the options. my
previous message contains an example

dana


diff --git a/Doc/Zsh/mod_zutil.yo b/Doc/Zsh/mod_zutil.yo
index 76907352f..50568308a 100644
--- a/Doc/Zsh/mod_zutil.yo
+++ b/Doc/Zsh/mod_zutil.yo
@@ -233,12 +233,14 @@ This builtin simplifies the parsing of options in positional parameters,
 i.e. the set of arguments given by tt($*).  Each var(spec) describes one
 option and must be of the form `var(opt)[tt(=)var(array)]'.  If an option
 described by var(opt) is found in the positional parameters it is copied
-into the var(array) specified with the tt(-a) option; if the optional
+into the var(array) or var(assoc) specified with the tt(-a) or tt(-A)
+option respectively; if the optional
 `tt(=)var(array)' is given, it is instead copied into that array, which
 should be declared as a normal array and never as an associative array.
 
-Note that it is an error to give any var(spec) without an
-`tt(=)var(array)' unless one of the tt(-a) or tt(-A) options is used.
+If none of tt(-a), tt(-A), or a spec with `tt(=)var(array)' is given,
+no options are copied.  This technique can be used for error checking
+or for discarding unused options.
 
 Unless the tt(-E) option is given, parsing stops at the first string
 that isn't described by one of the var(spec)s.  Even with tt(-E),
diff --git a/Src/Modules/zutil.c b/Src/Modules/zutil.c
index 9b2721a09..c2b0ba903 100644
--- a/Src/Modules/zutil.c
+++ b/Src/Modules/zutil.c
@@ -1909,7 +1909,7 @@ bin_zparseopts(char *nam, char **args, UNUSED(Options ops), UNUSED(int func))
 		f |= ZOF_SAME;
 	    }
 	}
-	a = NULL;
+	a = defarr; /* May be NULL */
 	if (*p == '=') {
 	    *p++ = '\0';
 	    f |= flags;
@@ -1924,9 +1924,6 @@ bin_zparseopts(char *nam, char **args, UNUSED(Options ops), UNUSED(int func))
 	} else if (*p) {
 	    zwarnnam(nam, "invalid option description: %s", args[-1]);
 	    return 1;
-	} else if (!(a = defarr) && !assoc) {
-	    zwarnnam(nam, "no default array defined: %s", args[-1]);
-	    return 1;
 	}
 	for (p = n = o; *p; p++) {
 	    if (*p == '\\' && p[1])
diff --git a/Test/V12zparseopts.ztst b/Test/V12zparseopts.ztst
index a2743ea0e..89bdb2132 100644
--- a/Test/V12zparseopts.ztst
+++ b/Test/V12zparseopts.ztst
@@ -119,6 +119,13 @@
 0:multiple arrays
 >ret: 0, optv: -c-d, aa: -a, ab: -b 1, argv: -ab1 -c -d
 
+  () {
+    zparseopts - a b: c:- z
+    print -r - ret: $?, argv: $argv
+  } -ab1 -c -d -e -z
+0:no arrays
+>ret: 0, argv: -ab1 -c -d -e -z
+
   for 1 in '-a - -b - - -b' '-a -- -b -- -- -b' '-a 1 -b - - -b'; do
     # -D alone strips - out
     () {




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