C++ 为什么在c中使用getopt时opstring不创建预期的响应?
我正在尝试为我正在编写的程序编写一个getopt()模块,但由于某些原因,我对getopt()的使用不起作用C++ 为什么在c中使用getopt时opstring不创建预期的响应?,c++,c,getopt,C++,C,Getopt,我正在尝试为我正在编写的程序编写一个getopt()模块,但由于某些原因,我对getopt()的使用不起作用 #include "setopt.h" #include <stdio.h> #include <getopt.h> #include <stdlib.h> #include <string.h> #include <unistd.h> struct flags_t getFlags(int argc, char** argv
#include "setopt.h"
#include <stdio.h>
#include <getopt.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
struct flags_t getFlags(int argc, char** argv) {
struct flags_t ourflags;
int opt;
ourflags.threadpool = 4;
while (1) {
opt = getopt(argc, argv, "N:l:");
if (opt == -1) {
//printf("getting bad read -1\n");
break;
}
switch (opt) {
case 'l' :
strcpy(ourflags.logfilename, optarg);
break;
case 'N' :
ourflags.threadpool = atoi(optarg);
break;
default :
printf("unkown flag %c\n", opt);
exit(-1);
}
}
const int requiredargs = 1;
if (argc - optind != requiredargs) {
printf("incorrect amount of flags, wanted : %d: got %d\n",
requiredargs, (argc - optind));
exit(-1);
}
ourflags.port = atoi(argv[optind]);
return ourflags;
}
我得到输出
incorrect amount of flags, wanted : 1: got 5
我认为问题出在我的optstring论证中,但我不确定。我不能让他换箱子。任何帮助都将不胜感激 您是否尝试将
8080
放在选项之后?这很有效,但当非选项参数位于前面时,为什么不这样做?您应该打印识别并使用了哪个参数。同时打印数字。您可能会发现根本没有识别任何选项。这不是getopt
的实现,而是对计算机/编译器附带的实现的使用。为您将其更改为“使用”。您是否尝试将8080
放在选项之后?可以,但是当非选项参数在前面时,为什么不打印呢?您应该打印识别并使用了哪个参数。同时打印数字。您可能会发现根本无法识别任何选项。这不是getopt
的实现,而是对计算机/编译器附带的实现的使用。将其更改为“使用”
incorrect amount of flags, wanted : 1: got 5