Can I use getopt to process options in a certain order?

假如想象 提交于 2019-12-01 14:47:48

There are two main ways to process the options read by getopt(). One is to do the action associated with an option as getopt() identifies it before reading the rest of the options; the other is to read all the options before doing any processing. (Programs not infrequently implement hybrid solutions; some options are acted on while parsing the arguments but others are simply noted and the actions taken after the arguments are processed.)

In your situation, it seems likely that you need to parse all the options before doing any processing. If both -a and -b terminate the program after doing the action, then they're mutually exclusive options and you should probably diagnose that as an erroneous invocation of your program. You will need to decide whether it is OK to allow -c as well as either -a or -b.

Quite often, you will have pairs of options that are required to work together, or you need to know whether one, the other, or both have been specified before deciding what to do. Another issue to consider is what it means if an option is repeated on the command line.

Stephen Chambers

There is no way to make getopt itself handle this. The easiest way would be to create a flags bitmap, as @tfahl suggested.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!