Skip site navigation (1)Skip section navigation (2)
Date:      Wed, 25 Sep 2002 21:45:49 -0500
From:      Dan Nelson <dnelson@allantgroup.com>
To:        Matthew Emmerton <matt@gsicomp.on.ca>
Cc:        freebsd-hackers@FreeBSD.ORG
Subject:   Re: perceived strangeness with getopt(1,3)
Message-ID:  <20020926024549.GL16302@dan.emsphone.com>
In-Reply-To: <009b01c26500$3f7e91a0$1200a8c0@gsicomp.on.ca>
References:  <009b01c26500$3f7e91a0$1200a8c0@gsicomp.on.ca>

next in thread | previous in thread | raw e-mail | index | archive | help
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




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20020926024549.GL16302>