http://blog.chinaunix.net/uid-25909722-id-2887971.html
我们知道,Linux中在命令行下调用程序时,所使用的命令由三部分组成。比如:
gcc -O -o hello hello.c
其中
(1)gcc是
命令的名字,
(2)-O是
可选字符, -o也是可选字符,
(3)hello 和 hello.c就是命令行
参数。
getopt()函数就是用来解析命令行参数和可选字符的。所以getopt()函数应该是每一个命令行下运行的程序必须要使用的函数。基本上在这些程序的main函数中,调用的第一个函数就是getopt(),调用形式一般如下:
- while((c = getopt(argc, argv, "xy:z::")) != -1){
- switch(c){
- case 'x': ... ...
- case 'y': ... ...
- case 'z': ... ...
- ... ....
- }
- }
- ... ....
getopt函数的原型如下:
- #include <unistd.h>
- int getopt(int argc, char * const argv[], const char *optstring);
- extern char *optarg;
- extern int optind, opterr, optopt;
getopt()函数用于解析命令行参数。它的参数argc、argv就是main函数的参数,argc表示传给main函数的参数的个数(每一个参数都是字符串),argv则是一个char的指针数组,每一个指针数组的元素指向一个字符串参数。
在指针数组指向的字符串参数,
如果以'-'开头,就表示这个字符串参数是一个“可选部分”。
'-'后面的那个字符就是一个“可选字符”。如果不断的调用getopt()函数,那么
它每次从“可选部分”返回一个“可选字符”。
变量optind是getopt要处理的argv中的下一个字符串参数的索引。
optind被系统初始化为1. 我们可以将它重新赋值为1,从而可以重新开始扫描指针数组argv。
每当getopt()函数找到一个“可选字符”,它就更新外部变量optind和一个内部静态变量nextchar,所以对getopt()函数的下一次调用可以从静态变量nextchar保存的位置开始。
当分析完了所有的“可选字符”时,getopt()函数返回-1. 同时
optind的值正是数组argv中第一个非“可选部分”的索引,也就是说optind是第一个参数的索引。
getopt()函数的第三个参数optstring是一个有所有合法的“可选字符”所组成的字符串。
《1》在参数optstring的“可选字符”如果后面带有一个':',
则表示在该“可选字符”的后面必须有一个参数。比如“o:"表示: gcc -o arg 在-o的后面必须带有一个参数arg. 在getopt()函数解析完"o:"对应的命令行参数时,
char型的指针optarg则指向了参数"arg"。
《2》如果在“可选字符”的后面带有了两个':'时,则表示在该“可选字符”后面可能有也可能没有参数,有参数,则optarg指向参数,没有则为0。这是GNU的一个关于getopt()函数的扩展。
《3》如果optstring中含有一个大写的'W'字符,后面带有一个冒号,也就是形如"W:",则表示该“可选字符”是一个“长选项”,也就是说不是只有一个字符的“可选字符”。比如:gcc -Wall hello.c 要解析该命令行,getopt()函数中的第三个参数
optstring应该是:"W:all",而且当解析到“-Wall"时optarg = "all". 这一点也是GNU对getopt()函数的扩展。
《4》如果getopt()函数在argv中解析到一个没有包含在optstring中的“可选字符”,它会打印一个错误信息,并将该“可选字符”保存在optopt中,
并返回字符'?'。当然,
我们可以将变量opterr赋值为0,来阻止getopt()函数输出错误信息。
《5》当getopt()函数的第三个参数optstring的第一个字符是':'时,很显然,这是由于少写了一个"可选字符"的缘故。此时,getopt()函数不返回'?',
而是返回':'来暗示我们漏掉了一个”可选字符”.
例子1:
- /*
- * The following trivial example program uses getopt() to handle two
- * program options: -n, with no associated value;
- * and -t val, which expects an associated value.
- */
- #include <unistd.h>
- #include <stdlib.h>
- #include <stdio.h>
- int main(int argc, char *argv[])
- {
- int flags, opt;
- int nsecs, tfnd;
-
- nsecs = 0;
- tfnd = 0;
- flags = 0;
- while ((opt = getopt(argc, argv, "nt:")) != -1) {
- switch (opt) {
- case 'n':
- flags = 1;
- break;
- case 't':
- nsecs = atoi(optarg);
- tfnd = 1;
- break;
- default: /* '?' */
- fprintf(stderr, "Usage: %s [-t nsecs] [-n] name\n", argv[0]);
- exit(EXIT_FAILURE);
- }
- }
- printf("flags=%d; tfnd=%d; nsecs=%d; optind=%d\n", flags, tfnd, nsecs, optind);
- if (optind >= argc) {
- fprintf(stderr, "Expected argument after options\n");
- exit(EXIT_FAILURE);
- }
- printf("name argument = %s\n", argv[optind]);
- /* Other code omitted */
- exit(EXIT_SUCCESS);
- }
编译:gcc -Wall -o getopt getopt.c
运行:./getopt -n -t 120 nessecory_arg
结果:
flags=1; tfnd=1; nsecs=120; optind=4
name argument = nessecory_arg
注意:当我们将执行命令改为:./getopt -n -t 120
输出的结果为:
flags=1; tfnd=1; nsecs=120; optind=4
Expected argument after options
这是因为,当getopt()函数解析完了参数时,optind应该是argv指针数组中第一个非可选参数的索引。现在没有“非可选参数”,所以条件“
optind >= argc”成立了,所以输出了“Expected argument after options
”
例子2:
- #include <stdio.h>
- #include <stdlib.h>
- #include <unistd.h>
- #include <fcntl.h>
- int main(int argc, char *argv[])
- {
- int c;
-
- // opterr = 0;
- while((c = getopt(argc, argv, "iu:z:")) != -1){
- switch(c){
- case 'i':
- printf("current option index:(optind-1)=%d, argv[optind-1]=argv[%d]=%s\n",
- (optind-1), (optind-1), argv[optind-1]);
- printf("next option index:(optind)=%d, argv[optind]=argv[%d]=%s\n",
- (optind), (optind), argv[optind]);
- printf("optarg=%s, opterr=%d, optopt=%d\n\n", optarg, opterr, optopt);
- break;
-
- case 'u':
- printf("current option index:(optind-2)=%d, argv[optind-2]=argv[%d]=%s\n",
- (optind-2), (optind-2), argv[optind-2]);
- printf("next option index:(optind)=%d, argv[optind]=argv[%d]=%s\n",
- (optind), (optind), argv[optind]);
- printf("optarg=%s, opterr=%d, optopt=%d\n\n", optarg, opterr, optopt);
- break;
-
- case 'z':
- printf("current option index:(optind-1)=%d, argv[optind-1]=argv[%d]=%s\n",
- (optind-1), (optind-1), argv[optind-1]);
- printf("next option index:(optind)=%d, argv[optind]=argv[%d]=%s\n",
- (optind), (optind), argv[optind]);
- printf("optarg=%s, opterr=%d, optopt=%d\n\n", optarg, opterr, optopt);
- break;
-
- case '?':
- fprintf(stderr, "Usage: %s [-i] [-u username] [-z filename]\n", argv[0]);
- break;
- }
- }
- exit(0);
- }
编译:gcc -Wall -o getopt2 getopt2.c
运行:./getopt2 -i -u username -z filename
结果:
current option index:(optind-1)=1, argv[optind-1]=argv[1]=-i
next option index:(optind)=2, argv[optind]=argv[2]=-u
optarg=(null), opterr=1, optopt=0
current option index:(optind-2)=2, argv[optind-2]=argv[2]=-u
next option index:(optind)=4, argv[optind]=argv[4]=-z
optarg=username, opterr=1, optopt=0
current option index:(optind-1)=5, argv[optind-1]=argv[5]=filename
next option index:(optind)=6, argv[optind]=argv[6]=(null)
optarg=filename, opterr=1, optopt=0
例子3:
- #include <stdio.h>
- #include <stdlib.h>
- #include <unistd.h>
- #include <fcntl.h>
-
- int main(int argc, char *argv[])
- {
- int c;
-
- opterr = 0;
- while((c = getopt(argc, argv, "Oo:W:all")) != -1){
- printf("option char: %c\n", c);
- switch(c){
- case 'O':
- printf("optimization flag is open.\n\n");
- break;
- case 'o':
- printf("the obj is: %s\n\n", optarg);
- break;
- case 'W':
- printf("optarg: %s\n\n", optarg);
- break;
- case '?':
- fprintf(stderr, "Usage: %s [-Wall] [-O] [-o arg] arg\n", argv[0]);
- break;
- case ':':
- fprintf(stderr, "miss option char in optstring.\n");
- break;
- }
- }
- exit(0);
- }
编译:gcc -Wall -o mygcc gcc.c
运行:./mygcc -Wall -O -o mygcc
结果:
option char: W
optarg: all
option char: O
optimization flag is open.
option char: o
the obj is: mygcc