help-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Help-bash] Allow getopts to do what is capable with getopt?


From: Eduardo A . Bustamante López
Subject: Re: [Help-bash] Allow getopts to do what is capable with getopt?
Date: Tue, 24 Mar 2015 19:46:53 -0600
User-agent: Mutt/1.5.23 (2014-03-12)

Yes, I read the article.

> This URL demonstrates that it is not robust with respect to all the needs.
Well, I said *most*, not all. POSIX mandates that arguments *follow* options,
so, being able to handle: foo bar -of   is not really a plus, it's just
featuritis. Unless you are being forced to support that format (i.e. if you're
parsing options for another program that does support that syntax), you pretty
much get to choose, and... you should choose *not* to support options after
arguments.

The main drawbacks of getopts is that it doesn't handle long options and that
it doesn't support these quirks that other option processors do support. The
cool thing is that you don't have to care about that. Just don't use long
options and these quirks, *you don't have to support these*.

If that's not an option, and you do have to support long options, well, it's a
bit sad, isn't it? How do you know the getopt you're getting is the one from
util-linux?

# openbsd
$ getopt -- ho: -ho 'hello world' 'bye bye'
getopt: unknown option -- h
getopt: unknown option -- o
 - ho: - hello world - bye bye --
$ getopt ho: -ho 'hello world' 'bye bye'
 -h -o hello world -- bye bye

# util-linux
address@hidden ~ % getopt -- ho: -ho 'hello world' 'bye bye'
 -h -o 'hello world' -- 'bye bye'
address@hidden ~ % getopt ho: -ho 'hello world' 'bye bye'   
 -h -o hello world -- bye bye

# netbsd (native getopt) (pkg getopt is also from util-linux, apparently).
address@hidden /usr/bin/getopt -- ho: -ho 'hello world' 'bye bye'
 -- ho: -ho hello world bye bye
address@hidden /usr/bin/getopt ho: -ho 'hello world' 'bye bye'
 -h -o hello world -- bye bye

Three getopt, and all three have different results. As you can see, the only
one that is safe is util-linux getopt. If you're *certain* that you'll always
be on systems with util-linux getopt, then I guess... ok, go with it. If not,
well, if you have bash, getopts is the same, independent of the system ;-)

-- 
Eduardo Bustamante | https://dualbus.me/



reply via email to

[Prev in Thread] Current Thread [Next in Thread]