参考:http://www.oschina.net/code/explore/axis2c-src-1.6.0/util/src/platforms/windows/getopt_windows.c
源码:
static char *optarg = NULL; //用于存储每次得到的参数
源码:
static char *optarg = NULL; //用于存储每次得到的参数
static int optind = 1;
int win_getopt(int __argc, char *const *__argv, const char *__shortopts)
{
int opterr = 1;
int optopt = 0;
static char *pos = "";
char *olstindex = NULL;
if (!*pos)
{
if (optind >= __argc || *(pos = __argv[optind]) != '-')
{
pos = "";
return -1;
}
if (pos[1] && *++pos == '-')
{
++optind;
pos = "";
return -1;
}
}
optopt = (int) *pos++;
olstindex = strchr((char *)__shortopts, optopt);
if (!olstindex)
{
if (optopt == (int) '-')
return -1;
if (!*pos)
++optind;
}
++olstindex;
if (*pos)
optarg = pos;
else if (__argc <= ++optind)
{
pos = "";
}
else
optarg = __argv[optind];
pos = "";
++optind;
return optopt;
}
测试命令: xxx.exe -c config.txt -d 0
int main(int argc, char *argv[])
{
char options[] = "Cc:d:v";
char cfg[1024];
int front = 0;
int ret = 0;
int debug = 0;
//读取配置文件
while( (ret = win_getopt(argc, argv, options)) >= 0){
if(ret == 'c'){
sprintf(cfg, optarg);
}else if(ret == 'd'){
debug = atoi(optarg);
front = 1;
}else if(ret == 'C'){
}else if(ret == 'v'){
return(0);
}else if(ret == '?'){
if(ret == 'd'){
debug = atoi(optarg);
front = 1;
}
}
}
}
测试命令: xxx.exe -c config.txt -d 0