[SOLVED] Parsing getopt (c). How to skip argument?
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
// 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.
By definition any character followed by a colon (:) requires an argument, two colons means the argument is optional. Since your p is not followed by any colons -doc is not considered a valid argument or even option. I don't think it matters if the argument starts with a -.
It should not matter in what order the -a or -p are entered on the command line. I would guess if you are not following getopt syntax then you need to manually parse your command line arguments.
you need to decide if -p requires an additional parameter, and how do you want to pass it. Usually it should come immediately after the flag, so -a -p -doc is ok, -ap -doc is ok, but -doc -a -p or -p -a -doc or -a -doc -p are not.
Anyway, you need to specify exactly what is expected and allowed and if your requirements are incompatible with getop you need to implement your own parser. Like rsync or ssh.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.