开发者

Is it possible to move backwards through a getopt_long argument list?

开发者 https://www.devze.com 2023-01-07 05:27 出处:网络
I would like to delegate to one of several possible lists of arguments based on whether particular arguments are present, along the lines of:

I would like to delegate to one of several possible lists of arguments based on whether particular arguments are present, along the lines of:

./test --do-thing-1 --option-A=7 --common-option --option-B=2 # options C and D not valid
./test --do-thing-2 --option-C=9 --common-option --option-D=1 # options A and B not valid

And the best way I can think of to do this neatly is to have main() process all the common options, and decide which of several functions to call to process the remainder.

Note that I don't want to restrict the order so that common options can only occur first.

My problem is that if I reset optind to 1 before parsing the arguments for a second time, getopt_long segfaults by passi开发者_C百科ng an invalid string to strncmp - so I guess I shouldn't be messing with optind.

I've had a google, and can't find any reference on whether it is possible to jump around the getopt_long argument list (I know it is possible for getopt), if it is, how do I do it?

I would prefer not to use any non-standard libraries. (Language is plain c, no c++ please)


My problem was that I hadn't set the last row of my static struct option long_options[] array to zeros, setting this correctly fixes the error.

GNU getopt also requires optind to be reset to 0 not 1 in order to correctly reset its internal state.

0

精彩评论

暂无评论...
验证码 换一张
取 消