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.
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.