Programming This 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.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
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.
|
|
05-14-2024, 12:15 PM
|
#1
|
Senior Member
Registered: Jun 2015
Location: Tucson, AZ USA
Distribution: Debian
Posts: 1,182
|
Parsing getopt (c). How to skip argument?
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 -. 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.
|
|
|
05-14-2024, 12:50 PM
|
#2
|
Moderator
Registered: Aug 2002
Posts: 26,112
|
Code:
getopt(argc,args,"ap")
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.
Last edited by michaelk; 05-14-2024 at 12:51 PM.
|
|
1 members found this post helpful.
|
05-14-2024, 12:57 PM
|
#3
|
LQ Addict
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 22,639
|
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.
|
|
1 members found this post helpful.
|
05-14-2024, 12:58 PM
|
#4
|
Senior Member
Registered: Jun 2015
Location: Tucson, AZ USA
Distribution: Debian
Posts: 1,182
Original Poster
|
That did it. Thank you. i'll need to go over that man page again. missed the part on the colons.
The call would be something like this
Code:
aptsort -a amd64 -p pattern
For now those are the only 2 options I've got on this program. The colons though did it. Works a treat now.
Last edited by jmgibson1981; 05-14-2024 at 12:59 PM.
|
|
|
05-14-2024, 01:33 PM
|
#5
|
LQ Addict
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 22,639
|
Quote:
Originally Posted by jmgibson1981
That did it. Thank you. The colons though did it. Works a treat now.
|
In that case you might want to mark the thread as solved.
|
|
|
All times are GMT -5. The time now is 03:42 AM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|