For Programmers: Free Programming Magazines  


Home > Archive > Unix Programming > April 2005 > Re: Shouldn't getopt_long() return ':' for options requiring an









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Re: Shouldn't getopt_long() return ':' for options requiring an
Måns Rullgård

2005-04-24, 8:57 pm

"Eric Lilja" <minder_thisshouldberemoved@gmail.com> writes:

> Hello, consider this C program and its accompanying test script:


[...]

> As you can see, getopt_long() returns '?' for the case where the option
> requires arguments and none was supplied, I was under the impression that it
> should return ':' for that case and '?' for unknown options (it prints the
> correct diagnostic, though). Is this a bug in the implementation of
> getopt_long() on my system or are there simply different valid variants of
> the implementation?


man 3 getopt:

If the first character (following any optional '+'or '-' described
above) of optstring is a colon (':'), then getopt() returns ':' instead
of '?' to indicate a missing option argument.

--
Måns Rullgård
mru@inprovide.com
Måns Rullgård

2005-04-24, 8:57 pm

"Eric Lilja" <minder_thisshouldberemoved@gmail.com> writes:

> "Måns Rullgård" <mru@inprovide.com> wrote:
>
> Hmm, I must be daft or something, but does that mean that getopt_long() only
> returns ':' if the user had supplied --:option_with_argument on the command
> line?


It means the string passed to getopt() (short_options in your example)
should start with a colon.

--
Måns Rullgård
mru@inprovide.com
Måns Rullgård

2005-04-25, 8:58 am

"Eric Lilja" <minder_thisshouldberemoved@gmail.com> writes:

> Thank you, Måns, changing short_options from "" to ":" seems to work, then
> '?' is returned for unknown options and ':' is returned for options that
> require a value but none was supplied. Does this change to short_options
> alter the behaviour in any other way?


If the man page is correct, no.

--
Måns Rullgård
mru@inprovide.com
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2010 codecomments.com