C getopt_long two required arguments for an option

is it possible to tell getopt_long I need two arguments if an option is given?

For example if -i is present it would require two arguments next and parsing would fails if they aren't present.

Upvotes: 1

Views: 617

Answers (1)

sabbahillel
sabbahillel

Reputation: 4425

According to the manual and getopt_long() -- proper way to use it? each argument passes in a single value which is passed via optarg.

It would appear that what you need to do is use optarg for the first argument and the pointer that is used by getopt(), as a pointer and strlen(optarg) to point to after the argument. Then (when you are in --i) test if the next string is an argument or the next option. If it is the next option, the kick out with the error. If it is the second argument, pick it up.

You may have to use the optind variable and point to argv[optind] to do the processing.

However, this may not work because

By default, getargs() permutes the contents of argv as it scans, so that eventually all the non-options are at the end.

get_opt() should skip that second argument, however, I am not sure (based on reading the manual) if it skips it or saves it for later. You will have to check with a debug output when you write your code. I do not have time to write a test for this.

Upvotes: 0

Related Questions