From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 3283 invoked from network); 2 Sep 2005 16:14:30 -0000 Received: from news.dotsrc.org (HELO a.mx.sunsite.dk) (130.225.247.88) by ns1.primenet.com.au with SMTP; 2 Sep 2005 16:14:30 -0000 Received: (qmail 10160 invoked from network); 2 Sep 2005 16:14:23 -0000 Received: from sunsite.dk (130.225.247.90) by a.mx.sunsite.dk with SMTP; 2 Sep 2005 16:14:23 -0000 Received: (qmail 15431 invoked by alias); 2 Sep 2005 16:14:15 -0000 Mailing-List: contact zsh-users-help@sunsite.dk; run by ezmlm Precedence: bulk X-No-Archive: yes X-Seq: 9367 Received: (qmail 15348 invoked from network); 2 Sep 2005 16:14:13 -0000 Received: from news.dotsrc.org (HELO a.mx.sunsite.dk) (130.225.247.88) by sunsite.dk with SMTP; 2 Sep 2005 16:14:13 -0000 Received: (qmail 8319 invoked from network); 2 Sep 2005 16:14:13 -0000 Received: from vms046pub.verizon.net (206.46.252.46) by a.mx.sunsite.dk with SMTP; 2 Sep 2005 16:14:08 -0000 Received: from candle.brasslantern.com ([71.116.79.190]) by vms046.mailsrvcs.net (Sun Java System Messaging Server 6.2-2.05 (built Apr 28 2005)) with ESMTPA id <0IM70028D6FFCG60@vms046.mailsrvcs.net> for zsh-users@sunsite.dk; Fri, 02 Sep 2005 11:14:04 -0500 (CDT) Received: from candle.brasslantern.com (IDENT:schaefer@localhost [127.0.0.1]) by candle.brasslantern.com (8.12.11/8.12.11) with ESMTP id j82GE2PI022536 for ; Fri, 02 Sep 2005 09:14:03 -0700 Received: (from schaefer@localhost) by candle.brasslantern.com (8.12.11/8.12.11/Submit) id j82GE23m022535 for zsh-users@sunsite.dk; Fri, 02 Sep 2005 09:14:02 -0700 Date: Fri, 02 Sep 2005 16:14:02 +0000 From: Bart Schaefer Subject: Re: How to handle unknown options in zparseopts In-reply-to: <20050831154454.GA122@DervishD> To: Zsh Users Message-id: <1050902161402.ZM22534@candle.brasslantern.com> MIME-version: 1.0 X-Mailer: Z-Mail (5.0.0 30July97) Content-type: text/plain; charset=us-ascii References: <20050831154454.GA122@DervishD> Comments: In reply to DervishD "How to handle unknown options in zparseopts" (Aug 31, 5:44pm) X-Spam-Checker-Version: SpamAssassin 3.0.4 (2005-06-05) on f.primenet.com.au X-Spam-Level: X-Spam-Status: No, score=-2.5 required=5.0 tests=AWL,BAYES_00 autolearn=ham version=3.0.4 On Aug 31, 5:44pm, DervishD wrote: > > zparseopts -D -A OPTIONS -- -help+ > > tmp=$argv[1] > > [[ "$tmp[1]" == "-" ]] && { > print "ERROR, unknown option \"$tmp\"" > return 1 > } > > That's pretty ugly and not very clean, neither :( Why are you messing around with the tmp variable? zparseopts -D -A OPTIONS -- -help+ [[ $1 = -* ]] && { print -u2 "ERROR, unknown option \"$1\"" return 1 } > But, worse, zparseopts spits an error in this case: > > set -- -a > zparseopts -a array -- a: zparseopts -- a::=array (( $#array == 1 )) && { print -u2 "ERROR, required argument of \"-a\" is missing" return 1 } When you use 'a:' you tell zparseopts that *it* should require an argument; but what you tell zparseopts doesn't have to literally reflect whether *you* require one. However, that does mean that the argument must be given in the same word as the option (e.g. "-axyz" rather than "-a xyz") so that may not fit your needs. There are two drawbacks to zparseopts; that's one of them, and the other is that it doesn't differentiate empty strings from omitted arguments very effectively. You might want to try a hybrid approach: Call zparseopts -D -E to strip all the long options, then use getopts to process the rest of them and issue error messages. However, that means that the order of the options on the command line must not matter.