一般是用while循环配合switch语句来使用getopt函数解析参数, 如下:
/*
* 得到参数信息
*/
int getinfo(int argc, char * const argv[], int *ps, int *pE, int *pb, char *trace_name, int *vflag)
{
int opt;
int count_arg = 0;
opterr = 0;
while ((opt = getopt(argc, argv, "vs:E:b:t:")) != -1) {
switch (opt) {
case 'v':
*vflag = 1;
break;
case 's':
++count_arg;
*ps = atoi(optarg);
break;
case 'E':
++count_arg;
*pE = atoi(optarg);
break;
case 'b':
++count_arg;
*pb = atoi(optarg);
break;
case 't':
++count_arg;
strcpy(trace_name, optarg);
break;
default: /* '?' */
Usage();
exit(-1);
break;
}
}
if (count_arg < 4) {
Usage();
exit(-1);
}
return 0;
}