Parsing getopt (c). How to skip argument?
by jmgibson1981 from LinuxQuestions.org on (#6MSZ0)
Code:// return struct based on args
REGCHECKS
arg_process(int argc,
char ** args)
{
// declare struct and initialize
REGCHECKS t1;
init_regchecks_struct(&t1,
false,
false);
// load linux version into struct
// fail if os-release file not found
if (get_linux_version(&t1) != 0) {
init_regchecks_struct(&t1,
true,
true);
t1.bailout = true;
return(t1);
};
int option = 0;
int optcounter = 1;
bool patternbypass = false;
while ((option = getopt(argc,
args,
"ap")) != -1) {
if (t1.bailout) {
break;
}
switch (option)
{
case 'a':
if (args[optcounter + 1]) {
t1.architecture = strdup(args[optcounter + 1]);
optcounter += 2;
} else {
init_regchecks_struct(&t1,
true,
true);
}
break;
case 'p':
// the arg here may start with a -
if (args[optcounter +1]) {
test_formatting(args[optcounter + 1],
&t1);
optcounter += 2;
} else {
init_regchecks_struct(&t1,
true,
true);
}
break;
default:
init_regchecks_struct(&t1,
true,
true);
break;
}
}
return(t1);
}For the p option sometimes I may want my input to start with a -. Code:-p -doc or some such. is there a way to skip past the next option and ignore it. I tried setting optind + 1 and it works but it ignores any other args. The ultimate goal is to have no specific required order for arguments.
REGCHECKS
arg_process(int argc,
char ** args)
{
// declare struct and initialize
REGCHECKS t1;
init_regchecks_struct(&t1,
false,
false);
// load linux version into struct
// fail if os-release file not found
if (get_linux_version(&t1) != 0) {
init_regchecks_struct(&t1,
true,
true);
t1.bailout = true;
return(t1);
};
int option = 0;
int optcounter = 1;
bool patternbypass = false;
while ((option = getopt(argc,
args,
"ap")) != -1) {
if (t1.bailout) {
break;
}
switch (option)
{
case 'a':
if (args[optcounter + 1]) {
t1.architecture = strdup(args[optcounter + 1]);
optcounter += 2;
} else {
init_regchecks_struct(&t1,
true,
true);
}
break;
case 'p':
// the arg here may start with a -
if (args[optcounter +1]) {
test_formatting(args[optcounter + 1],
&t1);
optcounter += 2;
} else {
init_regchecks_struct(&t1,
true,
true);
}
break;
default:
init_regchecks_struct(&t1,
true,
true);
break;
}
}
return(t1);
}For the p option sometimes I may want my input to start with a -. Code:-p -doc or some such. is there a way to skip past the next option and ignore it. I tried setting optind + 1 and it works but it ignores any other args. The ultimate goal is to have no specific required order for arguments.