实例,程序文件名为m.c
#include <stdio.h>
#include <unistd.h>
extern int opterr;
extern char *optarg;
int main(int argc,char *argv[])
{
int ch;
opterr=0;
ch=getopt(argc,argv,"a:bcde");
while(ch!=-1)
{
switch(ch)
{
case 'a':printf("option a:%s\n",optarg);break;
case 'b':printf("option b:b\n");break;
default:printf("other option:%c\n",ch);
}
break;
}
return 0;
}
输出结果: