#include <stdio.h>
#include <getopt.h>
void comnParseOpt(int argc, char *argv[])
{
int c;
int cnt = 1;
const char optstr[] = {"a:b:c:h"};
printf("optind:%i\n", optind);
while (-1 != (c = getopt(argc, argv, optstr))){
printf("optind:%i\n", optind);
cnt++;
switch (c){
case 'a':
case 'b':
case 'c':
{
printf("%c %s\n", c, optarg);
}
break;
case 'h':
{
printf("opt:%s\n", optstr);
}
break;
default:
{
printf("%c\n", c);
exit(0);
}
break;
}
}
}
void specParseOpt(int argc, char *argv[])
{
int c;
int cnt = 1;
const char suboptstr[] = {"d:e:f:h"};
printf("optind:%i\n", optind);
printf("continue parse\n");
printf("optind:%i\n", optind);
while (-1 != (c = getopt(argc, argv, suboptstr))){
printf("optind:%i\n", optind);
cnt++;
switch (c){
case 'd':
case 'e':
case 'f':
{
printf("%c %s\n", c, optarg);
}
break;
case 'h':
{
printf("subopt:%s\n", suboptstr);
}
break;
default:
{
printf("%c\n", c);
}
break;
}
}
}
int main(int argc, char *argv[])
{
//主程序(共用)解析
comnParseOpt(argc, argv);
//子程序(专用)解析
specParseOpt(argc, argv);
}
“--” 起到分隔作用。