处理:
第二个参数有些东西: :后面须接参数 -bxx和:: 后面可不接参数,后面多个选项,-mnzxc
argv[optind]:可以得到后面 -axx -bxx 后面这个-bxx的值,不过是string类型的.
optarg:得到是-a后面的值.
#include <stdio.h> #include <unistd.h> #include "task.h" int main(int argc,char**argv) { int ch = -1; char *string = "v::a::b::cdezgnmcv"; int count = 0; while((ch=getopt(argc,argv,string))!=-1) { printf(" is %c\n",ch); printf("gain is %s\n",optarg); printf("optind is %d\n",optind); printf("string optind is %s\n",argv[optind]); printf("argc is %d\n",argc); printf("argv is %s\n",argv[1]); switch(ch) { case ‘a‘: printf("get opt ch is a\n"); break; case ‘b‘: printf("get opt ch is b\n"); break; case ‘c‘: printf("get opt ch is c\n"); break; case ‘d‘: printf("get opt ch is d\n"); break; case ‘n‘: printf("get opt is n\n"); break; case ‘v‘: printf(" string string gain is %s\n",optarg); break; default: printf("unknow\n"); break; } count++; printf("count is %d\n",count); } count = 0; //printf("argc backward is %d\n",argc); //task_create(); //printf("argc forward is %d\n",argc); while(1) { if( getchar() == ‘x‘) { printf("main phread\n "); break; } } return 0; }
./sunshine -v500 -b11
is v
gain is 500
optind is 2
string optind is -b11
argc is 3
argv is -v500
string string gain is 500
count is 1
is b
gain is 11
optind is 3
string optind is (null)
argc is 3
argv is -v500
get opt ch is b
count is 2