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

PATCH (?): GNU-style long options for zsh



On Jul 28,  8:45pm, Juhapekka Tolvanen wrote:
}
} Is it too much work to teach zsh to understand long-options?

Actually, it wasn't hard at all, for the shell itself (I'm not going to
touch the builtin command option parser).  It was fairly simple to make
`--blahblahblah' mean the same as `-o blahblahblah', and then I just
special-cased `--version'.

(Normally I wouldn't use a goto, but given there was another already ...)

This patch is not committed, pending a more thorough commentary from the
rest of zsh-workers.  The whole parseargs() loop could probably be written
better than it is.  Some things of note that result from code that I did
NOT change:

Zsh accepts `-opts-junk' where "opts" are any valid option letters and
"junk" gets ignored.  Thus `--sh-option-letters' and `-sh-option-letters'
mean significantly different things ....

Zsh accepts `+-' and `+b' as end of options; this is necessary for `+b'
but not for `+-' (and could be changed by moving the `action' test in the
patch up one `if' level).  Also, therefore, `+opts-junk' turns off "opts"
and ingores "junk".

Index: Src/init.c
===================================================================
@@ -34,6 +34,8 @@
 
 #include "init.pro"
 
+#include "version.h"
+
 /**/
 int noexitct = 0;
 
@@ -207,13 +209,15 @@
     while (*argv && (**argv == '-' || **argv == '+')) {
 	char *args = *argv;
 	action = (**argv == '-');
-	if(!argv[0][1])
+	if (!argv[0][1])
 	    *argv = "--";
 	while (*++*argv) {
 	    /* The pseudo-option `--' signifies the end of options. *
 	     * `-b' does too, csh-style, unless we're emulating a   *
 	     * Bourne style shell.                                  */
 	    if (**argv == '-' || (!bourne && **argv == 'b')) {
+		if (action && argv[0] == &args[1] && **argv == '-' && *++*argv)
+		    goto longoptions;
 		argv++;
 		goto doneoptions;
 	    }
@@ -230,7 +234,12 @@
 		    zerr("string expected after -o", NULL, 0);
 		    exit(1);
 		}
-		if(!(optno = optlookup(*argv)))
+	    longoptions:
+		if (!strcmp(*argv, "version")) {
+		    fprintf(stderr, "zsh %s (%s-%s-%s)\n",
+			    ZSH_VERSION, MACHTYPE, VENDOR, OSTYPE);
+		    exit(0);
+		} else if (!(optno = optlookup(*argv)))
 		    zerr("no such option: %s", *argv, 0);
 		else if (optno == RESTRICTED)
 		    restricted = action;

-- 
Bart Schaefer                                 Brass Lantern Enterprises
http://www.well.com/user/barts              http://www.brasslantern.com

Zsh: http://www.zsh.org | PHPerl Project: http://phperl.sourceforge.net   



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