WINDOW写C++ 真的烦!!!
这个将 getopt.c整合在getopt.h了一起,就不用链接库,就不会出各种各样的BUG
将这个getopt.h放在VS环境中就可以使用,如下
C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.27.29110\include
示例:
#include <stdlib.h>
#include <stdio.h>
#include <getopt.h>
int
main(int argc, char** argv)
{
int opt;
int digit_optind = 0;
int option_index = 0;
char* optstring = "a:b:c:d";
static struct option long_options[] = {
{"reqarg", required_argument, NULL, 'r'},
{"noarg", no_argument, NULL, 'n'},
{"optarg", optional_argument, NULL, 'o'},
{0, 0, 0, 0}
};
while ((opt = getopt_long(argc, argv, optstring, long_options, &option_index)) != -1)
{
printf("opt = %c\n", opt);
printf("optarg = %s\n", optarg);
printf("optind = %d\n", optind);
printf("argv[optind - 1] = %s\n", argv[optind - 1]);
printf("option_index = %d\n", option_index);
}
return 0;
}