开发者

getopt_long()/getopt() with duplicated option input

I just got to know both functions. Have been searching internet to learn their usage. Found one thing which is very important to parse the command开发者_如何学C line option input, but not discussed.

Is such a case, if duplicated options are typed in, both functions can not do anything to handle it. I was wondering if there is any lib function to use for this.

If I have to handle it myself. The way in my mind is to collection short option into an array and find identical ones in the array.

Any better way to do it?


If you want to do something special with duplicate options, you can manage state in the option handling code.

Something like -v|--verbose can be repeated for additional verboseness, and the vebosity handling code is

// initialize
int verbose_level=0

// in the getopt case for -v
  verbose_level++;

(for options that can be repeated with arguments which should all be used, load up a list or some such).

If you don't want repeats to do anything special just set the value every time

  // in the case
  verbose_level = 1;

and if you want to detect repeats

  // in the case
  if (verbose_level) {
   // handle this case as an error...
}
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜