英文:
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
optstring
是 getopt_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);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论