Source-Changes-D archive

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

Re: CVS commit: othersrc/external/bsd/getopt2



On Fri, Jul 15, 2011 at 07:41:03AM +0100, David Laight wrote:
> On Fri, Jul 15, 2011 at 05:30:48AM +0000, Alistair G. Crooks wrote:
> ...
> > Log Message:
> > Initial import of getopt2 into othersrc repository.
> ...
> > In addition, the optstring does the usual ':' for an option with an
> > argument, and uses ';' to denote an option that optionally takes an
> > argument.
> 
> Hmmm... optional arguments shouldn't really be allowed - they go against
> the standard command line syntax rules.

Well, no-one is forcing anyone to use them.  However, in some
circumstances, they are extremely useful, and one of the reasons that
I have used getopt_long in the past (see netpgp and netpgpkeys, for
example) is because of the optional argument support in that.

Anyway, the driver for this one was being able to parse quickly and
efficiently without global variables. The interface for getopt(3)
sucks hugely in this regard. And I know this isn't a generally useful
thing, which is why it's in othersrc.

> It isn't actually obvious to me how this is parsed, since the general
> recommendation is to have a space between any option and its argument.

The optional argument parsing follows the same rules as getopt(3) ':'
option string parsing - the next character in argv[n] is examined - if
it's 0x0, then look at argv[n + 1] - if it's a '-', then there's no
argument.  If it's another character, then assume argv[n + 1] is the
optional argument.  This works for normal arguments, as well as tar's
"cvf" style arguments too.

If you've finished arguments, and want to use an optional argument as
the last one, then invoke with "--" to end the arguments.

Regards,
Alistair


Home | Main Index | Thread Index | Old Index