PBM: getopt_long doesn't process more than one long option table

V

Vince C.

Hi.

I've isntalled Bloodshed Dev-C++ 4.9.9 for Windows. It comes with MinGW
3.4.2. I'm trying to use getopt_long() in my console application to parse
multiple command line arguments syntaxes. I do that by looping through many
arrays of struct options. It looks like getopt_long() only processes the
first array of options; it returns -1 immediately on the second array
onwards. Here's the code I used:

#include <cstdlib>
#include <iostream>
#include <getopt.h>

using namespace std;

int main(int argc, char *argv[])
{
static option instArray[] = {
{ "install", required_argument, NULL, 'f' },
{ "name", required_argument, NULL, 'l' },
{ "description", required_argument, NULL, 'n' },
{ "title", required_argument, NULL, 'd' },
{ }
};
static option helpArray[] = {
{ "help", no_argument, NULL, 'h' },
{ "version", no_argument, NULL, 'v' },
{ }
};
static struct optItem {
char* name;
char* shortOpts;
option* opts;
} optArray[] = {
{ "Help", "-f:l:n:d:", helpArray },
{ "Install", "hv", instArray }
};
const unsigned nArrayLength = sizeof(optArray) / sizeof(optArray[0]);

// Loop twice with long option arrays
for ( int i = 0, option,; i < nArrayLength; i++ )
{
int optIndex = 0;
cout << "Arg table: " << optArray.name << endl;
while ( (option = getopt_long(argc, argv,
optArray.shortOpts, optArray.opts,
&optIndex)) != -1
)
if ( option == 1 )
cout << " Argument " << optarg << " found." << endl;
}
}


Here's the output:

Arg table: Help
Argument install found.
D:\Projects\Examples\Getopt\getopt.exe: unknown option -- v
D:\Projects\Examples\Getopt\getopt.exe: unknown option -- b
Arg table: Install


I expected

Arg table: Help
Argument install found.
D:\Projects\Examples\Getopt\getopt.exe: unknown option -- v
D:\Projects\Examples\Getopt\getopt.exe: unknown option -- b
Arg table: Install
Argument install found.
D:\Projects\Examples\Getopt\getopt.exe: unknown option -- v
D:\Projects\Examples\Getopt\getopt.exe: unknown option -- b

Did I do something wrong or is it a bug?

Thanks in advance,
Vince C.
 
R

red floyd

Vince said:
Hi.

I've isntalled Bloodshed Dev-C++ 4.9.9 for Windows. It comes with MinGW
3.4.2. I'm trying to use getopt_long() in my console application to parse
multiple command line arguments syntaxes. I do that by looping through many
arrays of struct options. It looks like getopt_long() only processes the
first array of options; it returns -1 immediately on the second array
onwards. Here's the code I used:

Unfortunately, Vince, getopt_long() is not part of the C++ standard
(ISO/IEC 14882:1998 or 14882:2003), and as such, is off-topic here.
However, I would recommend you ask in gnu.g++.help, where it would be
on-topic.
[code redacted]
 
V

Vince C.

red said:
Unfortunately, Vince, getopt_long() is not part of the C++ standard
(ISO/IEC 14882:1998 or 14882:2003), and as such, is off-topic here.
However, I would recommend you ask in gnu.g++.help, where it would be
on-topic.
[code redacted]

Sorry, I thought comp.std.c++ was about C++ standards and comp.lang.c++
about everything else. I have no descriptions about the forums in my NNTP
client. I also thought gnu.g++ was about compiler g++ only...

It's a bit tricky to find the appropriate group, I'm sorry :s ...

Thanks for the info. I'll post over gnu.g++.
 
V

Vince C.

red said:
Unfortunately, Vince, getopt_long() is not part of the C++ standard
(ISO/IEC 14882:1998 or 14882:2003), and as such, is off-topic here.
However, I would recommend you ask in gnu.g++.help, where it would be
on-topic.
[code redacted]

Too bad... I see only *one* post ("Spammed by FAX") in gnu.g++ :( . I think
that forum is dead... I just hope there is a chance I get a response
somewhere...
 
L

Lionel B

red said:
Unfortunately, Vince, getopt_long() is not part of the C++ standard
(ISO/IEC 14882:1998 or 14882:2003), and as such, is off-topic here.
However, I would recommend you ask in gnu.g++.help, where it would be
on-topic.
[code redacted]

Too bad... I see only *one* post ("Spammed by FAX") in gnu.g++

Is there actually such a ng? Try gnu.g++.help.
 
J

James Kanze

red said:
Unfortunately, Vince, getopt_long() is not part of the C++ standard
(ISO/IEC 14882:1998 or 14882:2003), and as such, is off-topic here.
However, I would recommend you ask in gnu.g++.help, where it would be
on-topic.
[code redacted]
Sorry, I thought comp.std.c++ was about C++ standards and comp.lang.c++
about everything else.

comp.std.c++ is about the C++ standard itself. comp.lang.c++ is
about everything that concerns C++ in general, but excludes
platform and implementation specific issues. The distinction
isn't always very clear. Discussion about threading in C++ is
certainly relevant, but basic threading algorithms aren't, nor
are details of a platform-specific threading API. The classic
"test" is that if the answer would be more or less the same for
most platforms, but different for a different language, the post
belongs here, but if the answer would be more or less the same
for different languages, but different for different platforms,
it's off topic. But of course, that test supposes that you know
the answer before posing the question:).

In this case, getopts_long is very much 1) a GNUism, and 2)
available in C and (probably) in other languages as well.
Questions about how best to encapsulate it in a C++ class (or
whether there exist C++ alternatives) would probably be on
topic, but that's about it.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,763
Messages
2,569,562
Members
45,038
Latest member
OrderProperKetocapsules

Latest Threads

Top