From owner-freebsd-hackers Wed Sep 25 19:45:52 2002 Delivered-To: freebsd-hackers@freebsd.org Received: from mx1.FreeBSD.org (mx1.FreeBSD.org [216.136.204.125]) by hub.freebsd.org (Postfix) with ESMTP id 4C6AA37B401 for ; Wed, 25 Sep 2002 19:45:50 -0700 (PDT) Received: from dan.emsphone.com (dan.emsphone.com [199.67.51.101]) by mx1.FreeBSD.org (Postfix) with ESMTP id C3F6F43E65 for ; Wed, 25 Sep 2002 19:45:49 -0700 (PDT) (envelope-from dan@dan.emsphone.com) Received: (from dan@localhost) by dan.emsphone.com (8.12.6/8.12.5) id g8Q2jnLa061057; Wed, 25 Sep 2002 21:45:49 -0500 (CDT) (envelope-from dan) Date: Wed, 25 Sep 2002 21:45:49 -0500 From: Dan Nelson To: Matthew Emmerton Cc: freebsd-hackers@FreeBSD.ORG Subject: Re: perceived strangeness with getopt(1,3) Message-ID: <20020926024549.GL16302@dan.emsphone.com> References: <009b01c26500$3f7e91a0$1200a8c0@gsicomp.on.ca> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <009b01c26500$3f7e91a0$1200a8c0@gsicomp.on.ca> X-OS: FreeBSD 5.0-CURRENT X-message-flag: Outlook Error User-Agent: Mutt/1.5.1i Sender: owner-freebsd-hackers@FreeBSD.ORG Precedence: bulk List-ID: List-Archive: (Web Archive) List-Help: (List Instructions) List-Subscribe: List-Unsubscribe: X-Loop: FreeBSD.ORG In the last episode (Sep 25), Matthew Emmerton said: > Maybe I'm missing something huge, but getopt(1,3) aren't working the > way I think they should. > > gabby# getopt k:s: -k -s > -k -s -- > gabby# > > Wha? Neither of these options specified arguments! I guess you > could consider that -k's argument was '-s', but I was pretty sure > that an option's argument couldn't start with a dash character (to > avoid the ambiguity that I'm hitting right now.) But then how could you specify a filename argument that starts with a dash? If you accept that arguments can start with dashes there is no ambiguity. > I'm pretty sure I'm the one that's confused (not getopt), since I get > the same behaviour on -STABLE and -CURRENT. Can someone tell me how > to accomplish what I want to do? Basically, I want this: > > gabby# getopt k:s: -k arg1 -s > getopt: option requires an argument -- k > -k arg1 -- You mean "-- s" here I suppose, since -k does have an argument. getopt does this already. > gabby# getopt k:s: -k -s arg2 > getopt: option requires an argument -- k > -s arg2 -- > gabby# You'll just have to catch that in your switch-processing code, and print an error if you get an argument that starts with a dash. case $i in -s ) case $2 in -* ) echo "getopt: option requires an argument -- $i" ; exit 1 ;; esac flag_s=$2 shift; shift ;; ... esac -- Dan Nelson dnelson@allantgroup.com To Unsubscribe: send mail to majordomo@FreeBSD.org with "unsubscribe freebsd-hackers" in the body of the message