A.函数功能:处理命令行参数
B.参数:
argc代表命令行参数的个数,argv[i]存放命令行参数在内存存放的地址。这两个参数直接从mian(int argc,char *argv[])得到。
如果argv的元素是以 "-“开头称为一个选项元素,紧跟在"-"后面的一个字符称为选项字符。
eg:ls -l
其中ls是可执行程序名称,-l称为选项元素,'l'称为选项字符
optstring:选项参数的集合
注意:如果getopt()函数被循环调用,每次调用都会返回一个对应选元素的选项字符,如果所有选项字符都放回后,再次调用getopt()函数返回-1。
./a.out -a -n -l
while(1)
{
ch = getopt(argc,argv,"anl");
}
第一次调用ch : 'a'
第二次调用ch : 'n'
第三次调用ch : 'l'
案例一、
运行结果:
C.变量optind
每次getopt函数调用的时候,optind总是当前处理argv[index]的下一个被处理选项元素的索引即optind = index + 1,如:当前处理的选项元素argv[1],则optind的值为2,即argv[2]的索引。
注意:其值默认初始化为1,如果。
案例二、
运行结果:
- #include <stdio.h>
- #include <unistd.h>
- int main(int argc,char *argv[])
- {
- int ch;
- while((ch = getopt(argc,argv,"cyg")) != -1)
- {
- switch(ch)
- {
- case 'c':
- printf("optind = %d\n",optind);
- printf("Option character \'c\'.\n");
- break;
- case 'y':
- printf("optind = %d\n",optind);
- printf("Option character \'y\'.\n");
- break;
- case 'g':
- printf("optind = %d\n",optind);
- printf("Option character \'g\'.\n");
- break;
- }
- }
- printf("Ending...\n");
- printf("optind = %d\n",optind);
- return 0;
- }
D.变量optarg
如果optstring选项字符后面紧跟一个":",则要求选项元素后面跟一个参数;
如果optstring选项字符后面紧跟两个"::",则选项元素后面可以跟一个可选的参数
案例三:
- #include <stdio.h>
- #include <unistd.h>
- int main(int argc,char *argv[])
- {
- int ch;
- while((ch = getopt(argc,argv,"c:yg::")) != -1)
- {
- switch(ch)
- {
- case 'c':
- printf("optind = %d\n",optind);
- printf("optarg = %s\n",optarg);
- printf("Option character \'c\'.\n");
- break;
- case 'y':
- printf("optind = %d\n",optind);
- printf("Option character \'y\'.\n");
- break;
- case 'g':
- printf("optind = %d\n",optind);
- printf("optarg = %s\n",optarg);
- printf("Option character \'g\'.\n");
- break;
- }
- }
- printf("Ending...\n");
- printf("argv[%d] = %s\n",optind,argv[optind]);
- return 0;
- }
注意:上面的最后一次运行结果,"xxx"在一开始在argv的的索引值为3,但是我们发现它实际的索引值是4。这是因为getopt函数,会把不拥有选项参数的选项字符后面的操作数,放在最后。
E.变量opterr和变量optopt
getopt()函数在搜索argv时,如果发现argv拥有optstring所没有的选项字符,则会提示立即返回'?',并提示错误信息,所没有的选项字符会放在opt里。如果不想系统提示默认的错误信息,将opterr设为0即可。
案例四:
运行结果:
- #include <stdio.h>
- #include <unistd.h>
- int main(int argc,char *argv[])
- {
- int ch;
- while((ch = getopt(argc,argv,"c:yg::")) != -1)
- {
- switch(ch)
- {
- case 'c':
- printf("optind = %d\n",optind);
- printf("optarg = %s\n",optarg);
- printf("Option character \'c\'.\n");
- break;
- case 'y':
- printf("optind = %d\n",optind);
- printf("Option character \'y\'.\n");
- break;
- case 'g':
- printf("optind = %d\n",optind);
- printf("optarg = %s\n",optarg);
- printf("Option character \'g\'.\n");
- break;
- case '?':
- printf("Invalid Option character \'%c\'.\n",optopt);
- break;
- }
- }
- printf("Ending...\n");
- printf("argv[%d] = %s\n",optind,argv[optind]);
- return 0;
- }
注意:如果optstring字符串一开始就是":",则后面要求有选项参数的选项字符,在argv中没有选项参数时,此时调用getopt函数返回":",optopt存放的是没有跟选项参数的选项字符。
案例五:
运行结果:
- #include <stdio.h>
- #include <unistd.h>
- int main(int argc,char *argv[])
- {
- int ch;
- while((ch = getopt(argc,argv,":c:yg::")) != -1)
- {
- switch(ch)
- {
- case 'c':
- printf("optind = %d\n",optind);
- printf("optarg = %s\n",optarg);
- printf("Option character \'c\'.\n");
- break;
- case 'y':
- printf("optind = %d\n",optind);
- printf("Option character \'y\'.\n");
- break;
- case 'g':
- printf("optarg = %s\n",optarg);
- printf("optind = %d\n",optind);
- printf("Option character \'g\'.\n");
- break;
- case ':':
- printf("Option character \'%c\',forget argument.\n",optopt);
- break;
- case '?':
- printf("optind = %d\n",optind);
- printf("Invalid Option character \'%c\'.\n",optopt);
- break;
- }
- }
- printf("Ending...\n");
- printf("argv[%d] = %s\n",optind,argv[optind]);
- return 0;
- }
二、getopt_long函数
getopt_long函数也是处理命令行参数,我们会发现它多了一个long,这个long的意思是它支持长选项。
前面我们讨论都是含有一个"-"的选项,常我们会看到"--help"这样的造型,其实这就是命令行参数中的长选项。一般短选项是选项名的缩写,长选项是选项名的全名。
这个函数前三个参数和getopt一样,不解释了。我们来看看后面两个参数。
这个函数前三个参数和getopt一样,不解释了。我们来看看后面两个参数。
getopt_long的长选项是一个结构体,定义如下:
参数longindex不为空时,存放的是getopt_long处理当前长选项的索引值。
案例六:
运行结果:
三、getopt_long_only函数
案例:
运行结果:
注意:getopt_long和getopt_long_only的返回值
- #include <stdio.h>
- #include <unistd.h>
- #include <getopt.h>
- int main(int argc,char *argv[])
- {
- int flag_value = 100;
- while(1)
- {
- int option_index = 0;
- int rvalue = 0;
- static struct option long_option[] = {
- {"help0",no_argument,0,0},
- {"help1",required_argument,0,0},
- {"help2",optional_argument,0,0},
- {"help3",no_argument,0,10},
- {0,0,0,0},
- };
-
- long_option[3].flag = &flag_value;
- rvalue = getopt_long(argc,argv,"a:bc::",long_option,&option_index);
-
- if(rvalue == -1)
- {
- printf("No more argument.\n");
- return -1;
- }
- switch(option_index)
- {
- case 0:
- printf("Long option is : %s\n",long_option[option_index].name);
- break;
- case 1:
- printf("Long option is : %s ",long_option[option_index].name);
- if(optarg)
- {
- printf("with parm '%s'",optarg);
- }
- printf("\n");
- break;
- case 2:
- printf("Long option is : %s ",long_option[option_index].name);
- if(optarg)
- {
- printf("with parm '%s'",optarg);
- }
- printf("\n");
- break;
- case 3:
- printf("Long option is : %s\n",long_option[option_index].name);
- break;
- }
- printf("flag_value = %d\n",flag_value);
- }
- return 0;
- }
三、getopt_long_only函数
案例:
- #include <stdio.h>
- #include <unistd.h>
- #include <getopt.h>
- int main(int argc,char *argv[])
- {
- int flag_value = 100;
- while(1)
- {
- int option_index = 0;
- int rvalue = 0;
- static struct option long_option[] = {
- {"help0",no_argument,0,0},
- {"help1",required_argument,0,0},
- {"help2",optional_argument,0,0},
- {"help3",no_argument,0,10},
- {0,0,0,0},
- };
-
- long_option[3].flag = &flag_value;
- rvalue = getopt_long_only(argc,argv,"a:bc::",long_option,&option_index);
-
- if(rvalue == -1)
- {
- printf("No more argument.\n");
- return -1;
- }
- switch(option_index)
- {
- case 0:
- printf("Long option is : %s\n",long_option[option_index].name);
- break;
- case 1:
- printf("Long option is : %s ",long_option[option_index].name);
- if(optarg)
- {
- printf("with parm '%s'",optarg);
- }
- printf("\n");
- break;
- case 2:
- printf("Long option is : %s ",long_option[option_index].name);
- if(optarg)
- {
- printf("with parm '%s'",optarg);
- }
- printf("\n");
- break;
- case 3:
- printf("Long option is : %s\n",long_option[option_index].name);
- break;
- }
- printf("flag_value = %d\n",flag_value);
- }
- return 0;
- }
注意:getopt_long和getopt_long_only的返回值