Code Comments

Programming Forum and web based access to our favorite programming groups.
For Programmers: Free Programming Magazines | New: Database administration forum
Registration is free! Edit your profileCalendarFind other membersFrequently Asked QuestionsSearch -> 
Post New Thread











Thread
Author

Shouldn't getopt_long() return ':' for options requiring an argument?
Hello, consider this C program and its accompanying test script:

#include <stdio.h>
#include <getopt.h>

static void handle_args(int, char **);

int
main(int argc, char **argv)
{
handle_args(argc, argv);

return 0;
}

#define REQUIRES_ARGUMENT 1

static void
handle_args(int argc, char **argv)
{
const struct option long_options[] =
{
{"option_with_argument", REQUIRES_ARGUMENT, NULL, 0},
{NULL, 0, NULL, 0},
};

const char *short_options = "";

int c = 0;

while((c = getopt_long(argc, argv, short_options, long_options, NULL))
!= -1)
{
switch(c)
{
case ':':
{
printf("Missing parameter.\n");

break;
}
case '?':
{
printf("Unknown option.\n");

break;
}
}
}
}

#!/bin/bash
echo "Testing with parameter --option_with_argument=4711"
./getopt_long_problem --option_with_argument=4711
echo -e "\nTesting with parameter --option_with_argument"
./getopt_long_problem --option_with_argument
echo -e "\nTesting with parameter --unknown_option"
./getopt_long_problem --unknown_option

The output when I run the script is:
$ ./probtest.sh
Testing with parameter --option_with_argument=4711

Testing with parameter --option_with_argument
getopt_long_problem: option requires an argument -- option_with_argument
Unknown option.

Testing with parameter --unknown_option
getopt_long_problem: unknown option -- unknown_option
Unknown option.

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?

/ Mikael



Report this thread to moderator Post Follow-up to this message
Old Post
Eric Lilja
04-25-05 01:57 AM


Re: Shouldn't getopt_long() return ':' for options requiring an argument?
"Måns Rullgård" <mru@inprovide.com> wrote:
> "Eric Lilja" <minder_thisshouldberemoved@gmail.com> writes:
> 
>
> [...]
> 
>
> 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.
>

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?

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

/ E



Report this thread to moderator Post Follow-up to this message
Old Post
Eric Lilja
04-25-05 01:57 AM


Re: Shouldn't getopt_long() return ':' for options requiring an argument?
"Måns Rullgård" <mru@inprovide.com> wrote:
> "Eric Lilja" <minder_thisshouldberemoved@gmail.com> writes:
> 
>
> It means the string passed to getopt() (short_options in your example)
> should start with a colon.
>

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?

/ E



Report this thread to moderator Post Follow-up to this message
Old Post
Eric Lilja
04-25-05 01:57 AM


Sponsored Links




Last Thread Next Thread Next
Search this forum -> 
Post New Thread

Unix Programming archive

Show a Printable Version Send to friend Email This Page to Someone! subscribe to this thread Receive updates to this thread
Computer Consultants
Programming Jobs
Visual Basic Controls
SQL Server Programming
Webservices
Java Security
Visual Studio
C# Programming
Visual J++
Software engineering
Open source Software
Perl Programming
PHP Programming
ASP Programming
ASP .NET Programming
Visual Basic Programming
Windows Scripting Host
Java Programming
Java Help
Java Beans
VBScript
Cobol
MAC Applications
Unix Programming
Forum Jump:
All times are GMT. The time now is 07:21 PM.

 
Free MCSE Braindumps | Real Estate Topics

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.