getopt.docx
《getopt.docx》由会员分享,可在线阅读,更多相关《getopt.docx(18页珍藏版)》请在冰豆网上搜索。
getopt
函数名:
表头文件:
#include
函数原型:
intgetopt(intargc,char*constargv[],constchar*optstring);
所涉及的全局变量:
externchar*optarg;
externintoptind,opterr,optopt;
所涉及的静态的全局变量:
staticstruct_getopt_datagetopt_data
函数描述:
1,getopt被用来解析命令行选项参数(短选项)。
2,argc,argv是在main函数调用的时候传递进来的命令行参数的个数和数组。
3,argv在传递参数的时候每个选项字符以“-”(短选项)开始(比如是“*–a1-b2”),选项分为两种,一种是标志位,只需要选项本身,另一种是选项之后还要接受附加信息。
4,涉及到的全局变量:
4.1:
externchar*optarg:
选项的参数指针,在检索到合法的选项字符之后,指向该字符后面的参数
4.2:
externintoptind:
下一次调用getopt的时,从optind存储的位置处重新开始检查选项。
每次成功的检索之后都从新赋值。
一旦getopt找到了所有的选项且在参数中存在非选项字符的参数,则optind表示剩下的非选项参数的起点位置,如果在参数中没有非选项字符,则optind指向所有命令行参数的末尾,默认值为1。
4.3:
externintopterr:
当命令行选项字符不包括在optstring中或者选项缺少必要的参数时会向stderr打印错误信息(比如编译之后的可执行文件为a.out无效的选项是-u则在终端执行的时候会显示./a.out:
invalidoption--'u')当opterr=0时,getopt不向stderr输出错误信息。
默认值为1。
4.4:
externintoptopt; 当命令行选项字符不包括在optstring中或者命令行选项字符包括在optstring中但是缺少必要的参数时,该选项存储在optopt中,getopt返回‘?
’。
如果命令行参数包括在opstring中,但是缺少必要的参数,而且optstring的第一个字符是‘:
’,则此时返回的就是‘:
’(默认值为‘?
’),我们可以输出此值作为诊断信息。
5,静态的全局变量(getopt.c文件中)
staticstruct_getopt_datagetopt_data;
struct_getopt_data
{
和涉及到的全局变量有相同的名字和意义,在刚开始执行函数时候,全局变量会将数值付给静态的全局变量getopt_data中相应的成员变量,在函数执行结束之前该静态的全局变量的成员变量会将数据再付给相应的全局变量。
intoptind;
intopterr;
intoptopt;
char*optarg;
判断成员变量是否初始化,如果进行了初始化,该数字为TRUE,否则就是false
int__initialized;
用来记录最后一次函数找到的选项字符,如果该变量为0或者指向一个空字符串,意味着通过移动到下一个命令行中的元素,重新开始扫描
char*__nextchar;
描述在遇到非选项字符的时候怎么样去处理
REQUIRE_ORDER
如果定义了环境变量POSIXLY_CORRECT,或者optstring的第一个字符为“+”,则就为该选项。
如果设置为该值,则在遇到非字符选项的时候,函数会停止扫描。
PREMUTE
默认的选项,如果遇到非选项字符,会改变argv的顺序,以至于在最后,非选项字符都会在命令行参数的最后。
RETURN_IN_ORDER:
如果optstring的第一个字符为“-”,则设置为该值,在函数的处理过程中会将每一个非选项字符都处理为ASCII码为1的字符的参数。
特殊参数'--'强制选项扫描结束,并且忽略'__ordering'的数值设置。
在RETURN_IN_ORDER的情况下,只有'--'可以导致函数getopt在OPTIND!
=ARGC的情况下返回-1,也就是强制的结束扫描。
enum
REQUIRE_ORDER,PERMUTE,RETURN_IN_ORDER
}__ordering;
判断环境变量POSIXLY_CORRECT是否进行了设置
int__posixly_correct;
用来处理排列的参数,描述在argv中已经被跳过的非选项字符,__first_nonopt是他们其中的第一个,__last_nonopt是最后一个非选项字符后面的那个字符。
int__first_nonopt;
int__last_nonopt;
#ifdefined_LIBC&&definedUSE_NONOPTION_FLAGS
int__nonoption_flags_max_len;
int__nonoption_flags_len;
#endif
};
6,optstring为合法的选项字符(比如“ab”)以及‘+’,‘-’,‘:
’,等特殊字符组成的字符串,参数的设置顺序和输入的顺序可以不一致。
如果字符后面跟着一个冒号,比如“a:
”这样意味着你再输入-a的选项字符之后,还需要输入一个空格,然后添加一个参数,比如“*-a100”,同时getopt会返回检索到的选项字符‘a’,两个冒号意味着这是一个选项参数,在输入参数的时候不能在选项字符后面添加空格,而是直接添加参数,比如“*-a100”。
optstring也通常用来设置一些处理选项,比如,如果optstring的第一个参数为‘+’或者设置了环境变量POSIXLY_CORRECT设置了,则在检索参数的时候,如果遇到没有在optstring中的选项字符,则函数会终止检索。
如果opstring的第一个参数为‘-’则在遇到没有识别的选项字符时候,会将他们处理为ASCII码为1的字符的参数,特殊的参数”--”会强制结束扫描,无论是什么模式。
7,函数的返回值:
如果一个选项成功的找到,则函数返回的是该选项字符,如果所有的命令行参数都解析过了,则函数返回-1,如果遇到了没有在opstring中的选项或者找到了该选项字符,但是没有在该选项之后没有参数,返回“?
”。
如果在检索到合法的选项字符之后,但是后面没有参数的时候并且optstring的第一个字符为“:
”,则此时函数返回“:
”,如果opstring的第一个参数为‘-’则在遇到没有识别的选项字符时候,会将他们处理为ASCII码为1的字符的参数,也就是函数要返回1。
如果在检索的时候,遇到没有在opestring中的选项字符的时候,函数会默认的将该字符的参数交换到命令行参数的最后,但是该字符的位置不变,仅仅是他的参数后换了。
但是如果opstring的第一个参数为“+”或者POSIXLY_CORRECT设置了,这样在遇到不在optstring中的字符的时候会停止扫描,也就是说他不会将选项缺少的参数移动到argv的末尾。
如果此时你想利用这个空档耍滑的时候就太天真了,比如opstring为“a:
b:
c:
”你输入的命令行参数为“–a100–e300–c”这个时候你会说,不是在遇到-e的时候300会后换吗,那我不用给-c赋值,这样-c的时候我读到的数值就是300了,但是你错了,你忽略的那个静态的全局变量get_data中的成员变量__first_nonopt的作用,这个时候“-c”的参数你依然是什么都读不到。
实例程序:
1-1一个比较常规的用法:
intmain(intargc,char**argv)
intresult;
opterr=0;
printf("0***************************************************\n");
while((result=getopt(argc,argv,"c:
a:
b:
"))!
=-1)
switch(result)
case'a':
printf("option=a,optopt=%c%d,optarg=%s\n",optopt,optopt,optarg);
break;
case'b':
printf("option=b,optopt=%c%d,optarg=%s\n",optopt,optopt,optarg);
case'c':
printf("option=c,optopt=%c%d,optarg=%s\n",optopt,optopt,optarg);
case'?
':
printf("option=\?
,optopt=%c%d,optarg=%s\n",optopt,optopt,optarg);
case1:
printf("thecharacterisnotdefined!
\n");
default:
printf("default,result=%c\n",result);
}
printf("resultis%dargv[%d]=%soptopt==%d---%c\n",result,optind,argv[optind],optopt,optopt);
printf("1***************************************************\n");
printf("result=%doptind=%d\n",result,optind);
printf("2***************************************************\n");
printf("theparameterafteroptind:
for(result=optind;result{printf("-----argv[%d]=%s\n",result,argv[result]);}printf("3***************************************************\n");printf("theorderofcommandlineparameters!\n");for(result=1;result{printf("\nattheend-----argv[%d]=%s\n",result,argv[result]);}printf("optopt==%d\n",optopt);return0;}程序的执行结果1:zansx@ubuntu-linux:~/zanshixiang/myprogram$./a.out-a123-b456-c7890***************************************************option=a,optopt=0,optarg=123resultis97argv[3]=-boptopt==0---option=b,optopt=0,optarg=456resultis98argv[5]=-coptopt==0---option=c,optopt=0,optarg=789resultis99argv[7]=(null)optopt==0---1***************************************************result=-1optind=72***************************************************theparameterafteroptind:3***************************************************theorderofcommandlineparameters!attheend-----argv[1]=-aattheend-----argv[2]=123attheend-----argv[3]=-battheend-----argv[4]=456attheend-----argv[5]=-cattheend-----argv[6]=789optopt==0说明:程序的最后,optind的数值为7,也就是命令行参数的最有一个参数的后面。可以看出来,程序循环了三次,每次循环都正确的读取到了一个选项字符以及后面的参数。最后没有参数了,返回-1,循环正常结束,然后接着程序正常结束。程序的执行结果2:zansx@ubuntu-linux:~/zanshixiang/myprogram$./a.out-a123-e1000***************************************************option=a,optopt=0,optarg=123resultis97argv[3]=-eoptopt==0---option=?,optopt=e101,optarg=(null)resultis63argv[4]=100optopt==101---e1***************************************************result=-1optind=42***************************************************theparameterafteroptind:-----argv[4]=1003***************************************************theorderofcommandlineparameters!attheend-----argv[1]=-aattheend-----argv[2]=123attheend-----argv[3]=-eattheend-----argv[4]=100optopt==101说明:这个时候在输入参数的时候,有-e这个非选项字符,这个时候函数返回‘?’,optopt这个时候输出‘e’,optarg是NULL,这个时候optind指向了100。程序的执行结果3:zansx@ubuntu-linux:~/zanshixiang/myprogram$./a.out-a123-e100-b4560***************************************************option=a,optopt=0,optarg=123resultis97argv[3]=-eoptopt==0---option=?,optopt=e101,optarg=(null)resultis63argv[4]=100optopt==101---eoption=b,optopt=e101,optarg=456resultis98argv[7]=(null)optopt==101---e1***************************************************result=-1optind=62***************************************************theparameterafteroptind:-----argv[6]=1003***************************************************theorderofcommandlineparameters!attheend-----argv[1]=-aattheend-----argv[2]=123attheend-----argv[3]=-eattheend-----argv[4]=-battheend-----argv[5]=456attheend-----argv[6]=100optopt==101说明:这个时候在命令行参数的中间出现了不是选项的字符,这个时候“-a”,“–b”的参数都正确的读取,只有-e的时候出现了错误,而且optopt在读取-e之后就一直都是‘e’,而且我前面说过的交换也出现了,命令行参数中“-e”后面的100被换到了最后,“-e”没有移动。程序的执行结果4:zansx@ubuntu-linux:~/zanshixiang/myprogram$./a.out-a123-e100-b0***************************************************option=a,optopt=0,optarg=123resultis97argv[3]=-eoptopt==0---option=?,optopt=e101,optarg=(null)resultis63argv[4]=100optopt==101---eoption=?,optopt=b98,optarg=(null)resultis63argv[6]=(null)optopt==98---b1***************************************************result=-1optind=52***************************************************theparameterafteroptind:-----argv[5]=1003***************************************************theorderofcommandlineparameters!attheend-----argv[1]=-aattheend-----argv[2]=123attheend-----argv[3]=-eattheend-----argv[4]=-battheend-----argv[5]=100optopt==98说明:这个时候先看一下最后的结果,命令行参数的顺序变了,100跑到了“-e”的后面,没有仔细看前面的任这个时候就问了,既然数据交换到了“-e”的后面为什么在读-e参数的时候依然没有读到100,你不要像我当初忘了的那个静态的全局变get_data中成员__first_nonopt的作用。optopt先是指向了‘e’后来又换到了‘b’,optind指向了100,也就是最后争取读取选项字符的参数的后面。1-2opstring中‘+’,‘-’,‘:’的设置仅仅将上面的程序改动一条语句while((result=getopt(argc,argv,"+c:a:b:"))!=-1)程序的执行结果1:zansx@ubuntu-linux:~/zanshixiang/myprogram$./a.out-a123-b456-c5670***************************************************option=a,optopt=0,optarg=123resultis97argv[3]=-boptopt==0---option=b,optopt=0,optarg=456resultis98argv[5]=-coptopt==0---option=c,optopt=0,optarg=567resultis99argv[7]=(null)optopt==0---1***************************************************result=-1optind=72***************************************************theparameterafteroptind:3***************************************************theorderofcommandlineparameters!attheend-----argv[1]=-aattheend-----argv[2]=123attheend-----argv[3]=-battheend-----argv[4]=456attheend-----argv[5]=-cattheend-----argv[6]=567optopt==0说明:正确的输入没有任何的问题。程序的执行结果2:zansx@ubuntu-linux:~/zanshixiang/myprogram$./a.out-a123-e456-c5670***************************************************option=a,optopt=0,optarg=123resultis97argv[3]=-eoptopt==0---option=?,optopt=e101,optarg=(null)resultis63argv[4]=456optopt==101---e1***************************************************result=-1optind=42***************************************************theparameterafteroptind:-----argv[4]=456-----argv[5]=-c-----argv[6]=5673***************************************************theorderofcommandlinep
printf("-----argv[%d]=%s\n",result,argv[result]);
printf("3***************************************************\n");
printf("theorderofcommandlineparameters!
for(result=1;result{printf("\nattheend-----argv[%d]=%s\n",result,argv[result]);}printf("optopt==%d\n",optopt);return0;}程序的执行结果1:zansx@ubuntu-linux:~/zanshixiang/myprogram$./a.out-a123-b456-c7890***************************************************option=a,optopt=0,optarg=123resultis97argv[3]=-boptopt==0---option=b,optopt=0,optarg=456resultis98argv[5]=-coptopt==0---option=c,optopt=0,optarg=789resultis99argv[7]=(null)optopt==0---1***************************************************result=-1optind=72***************************************************theparameterafteroptind:3***************************************************theorderofcommandlineparameters!attheend-----argv[1]=-aattheend-----argv[2]=123attheend-----argv[3]=-battheend-----argv[4]=456attheend-----argv[5]=-cattheend-----argv[6]=789optopt==0说明:程序的最后,optind的数值为7,也就是命令行参数的最有一个参数的后面。可以看出来,程序循环了三次,每次循环都正确的读取到了一个选项字符以及后面的参数。最后没有参数了,返回-1,循环正常结束,然后接着程序正常结束。程序的执行结果2:zansx@ubuntu-linux:~/zanshixiang/myprogram$./a.out-a123-e1000***************************************************option=a,optopt=0,optarg=123resultis97argv[3]=-eoptopt==0---option=?,optopt=e101,optarg=(null)resultis63argv[4]=100optopt==101---e1***************************************************result=-1optind=42***************************************************theparameterafteroptind:-----argv[4]=1003***************************************************theorderofcommandlineparameters!attheend-----argv[1]=-aattheend-----argv[2]=123attheend-----argv[3]=-eattheend-----argv[4]=100optopt==101说明:这个时候在输入参数的时候,有-e这个非选项字符,这个时候函数返回‘?’,optopt这个时候输出‘e’,optarg是NULL,这个时候optind指向了100。程序的执行结果3:zansx@ubuntu-linux:~/zanshixiang/myprogram$./a.out-a123-e100-b4560***************************************************option=a,optopt=0,optarg=123resultis97argv[3]=-eoptopt==0---option=?,optopt=e101,optarg=(null)resultis63argv[4]=100optopt==101---eoption=b,optopt=e101,optarg=456resultis98argv[7]=(null)optopt==101---e1***************************************************result=-1optind=62***************************************************theparameterafteroptind:-----argv[6]=1003***************************************************theorderofcommandlineparameters!attheend-----argv[1]=-aattheend-----argv[2]=123attheend-----argv[3]=-eattheend-----argv[4]=-battheend-----argv[5]=456attheend-----argv[6]=100optopt==101说明:这个时候在命令行参数的中间出现了不是选项的字符,这个时候“-a”,“–b”的参数都正确的读取,只有-e的时候出现了错误,而且optopt在读取-e之后就一直都是‘e’,而且我前面说过的交换也出现了,命令行参数中“-e”后面的100被换到了最后,“-e”没有移动。程序的执行结果4:zansx@ubuntu-linux:~/zanshixiang/myprogram$./a.out-a123-e100-b0***************************************************option=a,optopt=0,optarg=123resultis97argv[3]=-eoptopt==0---option=?,optopt=e101,optarg=(null)resultis63argv[4]=100optopt==101---eoption=?,optopt=b98,optarg=(null)resultis63argv[6]=(null)optopt==98---b1***************************************************result=-1optind=52***************************************************theparameterafteroptind:-----argv[5]=1003***************************************************theorderofcommandlineparameters!attheend-----argv[1]=-aattheend-----argv[2]=123attheend-----argv[3]=-eattheend-----argv[4]=-battheend-----argv[5]=100optopt==98说明:这个时候先看一下最后的结果,命令行参数的顺序变了,100跑到了“-e”的后面,没有仔细看前面的任这个时候就问了,既然数据交换到了“-e”的后面为什么在读-e参数的时候依然没有读到100,你不要像我当初忘了的那个静态的全局变get_data中成员__first_nonopt的作用。optopt先是指向了‘e’后来又换到了‘b’,optind指向了100,也就是最后争取读取选项字符的参数的后面。1-2opstring中‘+’,‘-’,‘:’的设置仅仅将上面的程序改动一条语句while((result=getopt(argc,argv,"+c:a:b:"))!=-1)程序的执行结果1:zansx@ubuntu-linux:~/zanshixiang/myprogram$./a.out-a123-b456-c5670***************************************************option=a,optopt=0,optarg=123resultis97argv[3]=-boptopt==0---option=b,optopt=0,optarg=456resultis98argv[5]=-coptopt==0---option=c,optopt=0,optarg=567resultis99argv[7]=(null)optopt==0---1***************************************************result=-1optind=72***************************************************theparameterafteroptind:3***************************************************theorderofcommandlineparameters!attheend-----argv[1]=-aattheend-----argv[2]=123attheend-----argv[3]=-battheend-----argv[4]=456attheend-----argv[5]=-cattheend-----argv[6]=567optopt==0说明:正确的输入没有任何的问题。程序的执行结果2:zansx@ubuntu-linux:~/zanshixiang/myprogram$./a.out-a123-e456-c5670***************************************************option=a,optopt=0,optarg=123resultis97argv[3]=-eoptopt==0---option=?,optopt=e101,optarg=(null)resultis63argv[4]=456optopt==101---e1***************************************************result=-1optind=42***************************************************theparameterafteroptind:-----argv[4]=456-----argv[5]=-c-----argv[6]=5673***************************************************theorderofcommandlinep
printf("\nattheend-----argv[%d]=%s\n",result,argv[result]);
printf("optopt==%d\n",optopt);
return0;
程序的执行结果1:
zansx@ubuntu-linux:
~/zanshixiang/myprogram$./a.out-a123-b456-c789
0***************************************************
option=a,optopt=0,optarg=123
resultis97argv[3]=-boptopt==0---
option=b,optopt=0,optarg=456
resultis98argv[5]=-coptopt==0---
option=c,optopt=0,optarg=789
resultis99argv[7]=(null)optopt==0---
1***************************************************
result=-1optind=7
2***************************************************
theparameterafteroptind:
3***************************************************
theorderofcommandlineparameters!
attheend-----argv[1]=-a
attheend-----argv[2]=123
attheend-----argv[3]=-b
attheend-----argv[4]=456
attheend-----argv[5]=-c
attheend-----argv[6]=789
optopt==0
说明:
程序的最后,optind的数值为7,也就是命令行参数的最有一个参数的后面。
可以看出来,程序循环了三次,每次循环都正确的读取到了一个选项字符以及后面的参数。
最后没有参数了,返回-1,循环正常结束,然后接着程序正常结束。
程序的执行结果2:
~/zanshixiang/myprogram$./a.out-a123-e100
resultis97argv[3]=-eoptopt==0---
option=?
,optopt=e101,optarg=(null)
resultis63argv[4]=100optopt==101---e
result=-1optind=4
-----argv[4]=100
attheend-----argv[3]=-e
attheend-----argv[4]=100
optopt==101
这个时候在输入参数的时候,有-e这个非选项字符,这个时候函数返回‘?
’,optopt这个时候输出‘e’,optarg是NULL,这个时候optind指向了100。
程序的执行结果3:
~/zanshixiang/myprogram$./a.out-a123-e100-b456
option=b,optopt=e101,optarg=456
resultis98argv[7]=(null)optopt==101---e
result=-1optind=6
-----argv[6]=100
attheend-----argv[4]=-b
attheend-----argv[5]=456
attheend-----argv[6]=100
这个时候在命令行参数的中间出现了不是选项的字符,这个时候“-a”,“–b”的参数都正确的读取,只有-e的时候出现了错误,而且optopt在读取-e之后就一直都是‘e’,而且我前面说过的交换也出现了,命令行参数中“-e”后面的100被换到了最后,“-e”没有移动。
程序的执行结果4:
~/zanshixiang/myprogram$./a.out-a123-e100-b
,optopt=b98,optarg=(null)
resultis63argv[6]=(null)optopt==98---b
result=-1optind=5
-----argv[5]=100
attheend-----argv[5]=100
optopt==98
这个时候先看一下最后的结果,命令行参数的顺序变了,100跑到了“-e”的后面,没有仔细看前面的任这个时候就问了,既然数据交换到了“-e”的后面为什么在读-e参数的时候依然没有读到100,你不要像我当初忘了的那个静态的全局变get_data中成员__first_nonopt的作用。
optopt先是指向了‘e’后来又换到了‘b’,optind指向了100,也就是最后争取读取选项字符的参数的后面。
1-2opstring中‘+’,‘-’,‘:
’的设置
仅仅将上面的程序改动一条语句
while((result=getopt(argc,argv,"+c:
~/zanshixiang/myprogram$./a.out-a123-b456-c567
option=c,optopt=0,optarg=567
attheend-----argv[6]=567
正确的输入没有任何的问题。
~/zanshixiang/myprogram$./a.out-a123-e456-c567
resultis63argv[4]=456optopt==101---e
-----argv[4]=456
-----argv[5]=-c
-----argv[6]=567
theorderofcommandlinep
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1