I want to be able to support commands such as:
./myProgram -f required_arg1 -v required_arg2
and
./myProgram -fv required_arg1 required_arg2
Where required_arg1 belongs to -f and required_arg2 belongs to -v in both cases.
When the code below runs with the following command: ./myProgram -fv required_arg1 required_arg2, v is instantly the argument to -f, but I want them to be independent of each other, but have their own optargs.
int opt;
int fflag = 0;
int iflag = 0;
while ((opt = getopt(argc, argv, "f:v:i:")) != -1) {
switch (opt) {
case 'f':
if (fflag) {
cerr << argv[0] << ": cannot have more than one -f in same command.\n";
return false;
}
else {
fflag = 1;
}
if (fflag && iflag) {
cerr << argv[0] << ": cannot have -i and -f in the same command.\n";
return false;
}
cout << "arg for -f: " << optarg << "\n";
break;
case 'i':
iflag = 1;
if (fflag && iflag) {
cerr << argv[0] << ": cannot have -i and -f in the same command.\n";
return false;
}
cout << "arg for -i: " << optarg << "\n";
break;
case 'v':
cout << "arg for -v: " << optarg << "\n";
break;
default:
break;
}
}