设置 getopt_long 将 optstring[0] 设为 ‘'+’

huangapple go评论73阅读模式
英文:

getopt_long setting optstring[0] to '+'

问题

根据getopt和getopt_long的手册页面,GNU版本会重新排列argv,使得类似于标志的内容首先出现,然后在遇到第一个不是标志的字符串时返回-1。它还指出,当optstring[0]被设置为'+'时,它不会重新排序参数。如何将optstring[0]设置为'+'呢?我尝试简单地插入一个optstring[0] = '+'的赋值语句,但我正确地收到了optstring未声明的错误。

英文:

According to the man page for getopt and getopt_long, the GNU version reorders argv so that anything resembling a flag will be first, then it will return -1 when it reaches the first string that is not a flag. It also says that when optstring[0] is set to '+', it will not reorder the arguments. How do I set optstring[0] to '+'? I tried simply tossing in a optstring[0] = '+'; assignment statement, and I rightfully got that optstring is undeclared.

答案1

得分: 2

optstringgetopt_long 的第三个参数,声明如下:

int getopt_long(int argc, char * const argv[],
           const char *optstring,
           const struct option *longopts, int *longindex);

使用以 + 开头的 optstring 调用该函数:

getopt_long(argc, argv, "+abc:d:f:", long_options, &option_index);
英文:

optstring is the third argument to getopt_long, declared as:

int getopt_long(int argc, char * const argv[],
           const char *optstring,
           const struct option *longopts, int *longindex);

Call the function with an optstring that begins with +:

getopt_long(argc, argv, "+abc:d:f:", long_options, &option_index);

huangapple
  • 本文由 发表于 2023年2月27日 09:24:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/75576092.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定