函数getopt(),及其参数optind
来源:互联网 发布:c语言实训总结 编辑:程序博客网 时间:2024/06/05 03:28
原文地址:函数getopt(),及其参数optind作者:陌柏
getopt被用来解析命令行选项参数。
#include <unistd.h>
首先说一下什么是选项,什么是参数。
1.单个字符,表示选项,2.单个字符后接一个冒号:表示该选项后必须跟一个参数。参数紧跟在选项后或者以空格隔开。该参数的指针赋给optarg。
3 单个字符后跟两个冒号,表示该选项后必须跟一个参数。参数必须紧跟在选项后不能以空格隔开。该参数的指针赋给optarg。(这个特性是GNU的扩张)。
例如gcc -g -o test test.c ,其中g和o表示选项,test为选项o的参数。
上面是getopt()函数的基本含义,大家懂得了这些之后,我们一个例子加深一下理解。
从上面我们可以知道,选项a,d没有参数,选项b,c有一个参数,选项e有有一个参数且必须紧跟在选项后不能以空格隔开。getopt首先扫描argv[1]到argv[argc-1],并将选项及参数依次放到argv数组的最左边,非选项参数依次放到argv的最后边。
执行程序为:
$ ./test file1 -a -b -c code -d file2 -e file3
扫描结束后,getopt会将argv数组修改成下面的形式
$ ./test -a -b -c -d -e file1 code file2 file3
同时,optind会指向非选项的第一个参数,如上面,optind将指向file1
代码如下:
#include <unistd.h>
#include <stdio.h>
int main(int argc, char * argv[])
{
int aflag=0, bflag=0, cflag=0;
int ch;
printf("optind:%d,opterr:%dn",optind,opterr);
printf("--------------------------n");
while ((ch = getopt(argc, argv, "ab:c:de::")) != -1)
{
printf("optind: %d,argc:%d,argv[%d]:%sn", optind,argc,optind,argv[optind]);
switch (ch) {
case 'a':
printf("HAVE option: -ann");
break;
case 'b':
printf("HAVE option: -bn");
printf("The argument of -b is %snn", optarg);
break;
case 'c':
printf("HAVE option: -cn");
printf("The argument of -c is %snn", optarg);
break;
case 'd':
printf("HAVE option: -dn");
break;
case 'e':
printf("HAVE option: -en");
printf("The argument of -e is %snn", optarg);
break;
case '?':
printf("Unknown option: %cn",(char)optopt);
break;
}
}
printf("----------------------------n");
printf("optind=%d,argv[%d]=%sn",optind,optind,argv[optind]);
}
执行结果:
shiqi@wjl-desktop:~/code$ vim getopt.c
shiqi@wjl-desktop:~/code$ gcc getopt.c -o g
shiqi@wjl-desktop:~/code$ ./g file1 -a -b -c code -d file2 -e file3
optind:1,opterr:1
--------------------------
optind: 3,argc:10,argv[3]:-b
HAVE option: -a
optind: 5,argc:10,argv[5]:code
HAVE option: -b
The argument of -b is -c
optind: 7,argc:10,argv[7]:file2
HAVE option: -d
optind: 9,argc:10,argv[9]:file3
HAVE option: -e
The argument of -e is (null)
----------------------------
optind=6,argv[6]=file1 //while循环执行完后,optind=6
代码如下:
#include <unistd.h>
#include <stdio.h>
int main(int argc, char * argv[])
{
printf("optind:%d,opterr:%dn",optind,opterr);
printf("--------------------------n");
}
执行结果:
shiqi@wjl-desktop:~/code$ vim getopt.c
shiqi@wjl-desktop:~/code$ gcc getopt.c -o g
shiqi@wjl-desktop:~/code$ ./g file1 -a
optind:1,opterr:1
--------------------------
optind: 3,argc:10,argv[3]:-b
HAVE option: -a
optind: 5,argc:10,argv[5]:code
HAVE option: -b
The argument of -b is -c
optind: 7,argc:10,argv[7]:file2
HAVE option: -d
optind: 9,argc:10,argv[9]:file3
HAVE option: -e
The argument of -e is (null)
----------------------------
optind=6,argv[6]=file1
- 函数getopt(),及其参数optind
- 函数getopt(),及其参数optind
- 函数getopt(),及其参数optind
- 函数getopt(),及其参数optind
- 函数getopt(),及其参数optind
- 函数getopt(),及其参数optind
- 函数getopt(),及其参数optind
- 函数getopt(),及其参数optind
- 函数getopt(),及其参数optind
- 函数getopt(),及其参数optind
- 函数getopt(),及其参数optind
- 函数getopt(),及其参数optind
- getopt()函数optind
- 函数getopt()及其他参数
- Linux中getopt函数、optind等变量使用详解
- getopt, optarg, optind, opterr, optopt
- 参数处理函数getopt
- c语言中的getopt()、optind的使用
- 欧拉回路
- Android sensor架构
- 逆转一个整数的二进制表示问题[转]
- IO - 同步,异步,阻塞,非阻塞
- Introduction to Big Data
- 函数getopt(),及其参数optind
- HDU 1596 find the safest road 最短路求最大值
- C语言中的数据类型
- 腾讯历史话题
- Android - Binder机制 - 获得普通service
- visual studio 没有添加自定义控件到工具箱的解决方法
- 类的const和非const成员函数的重载
- 排序工作量
- C语言深入学习系列 - 字节对齐&内存管理