j10
j10

Reputation: 2261

option followed by a option in getopt [where the earlier option was expecting a value]

I have a doubt if we have two option -a and -c and option -a needs to have a value and no value for -c

now if i give ->testopt -a -c [testopt is program]

then my program takes -c as a value for -a . is there a way where i can make sure that a option with value does not have other options as its values ?

the thing is that the system accepts - one keyword's value as another keyword which i need to prevent . e.g. - testopt -a -c. in this case there was a mandatory argument (value ) for -a and no value for -c.. but i my getopt interprets this as "-c" is the value for "-a" how can i throw a error on this using getopt.

Upvotes: 1

Views: 141

Answers (1)

Some programmer dude
Some programmer dude

Reputation: 409166

You get -c as the value for the -a option because you specified -a to have a value. If you call your program with the options the other way around

$ testopt -c -a

then getopt will return with a '?' indicating that an option value is missing (as per the getopt manual page), and an error will be printed on the console.

Unfortunately there is no way of telling getopt that an argument has an optional value, then you have to implement your own option parser.

Upvotes: 1

Related Questions