#include <stdio.h>
#include <stdlib.h>
#include <getopt.h>
int main(int argc, char *argv[]) {
char* listen_arg = NULL;
const struct option long_options[] = {
{"listen", required_argument, NULL, 1000},
{NULL, 0, NULL, 0}
};
int opt;
int option_index;
while ((opt = getopt_long(argc, argv, "", long_options, &option_index)) != -1) {
switch (opt) {
case 1000:
listen_arg = optarg;
printf("listen_arg is %s\n",listen_arg);
break;
case '?':
fprintf(stderr, "Unknown option: %s\n", argv[optind - 1]);
exit(EXIT_FAILURE);
}
}
return 0;
}
./a.out --listen=1234和 ./a.out --lis=1234都会打印listen_arg is 1234。
为什么会这样? 为什么不是精确匹配listen参数的
--
FROM 222.128.2.*