getopt函数用法
getopt被用来解析命令行选项参数。
#include <unistd.h>
extern char *optarg; //选项的参数指针
extern int optind, //下一次调用getopt的时,从optind存储的位置处重新开始检查选项。
extern int opterr, //当opterr=0时,getopt不向stderr输出错误信息。
extern int optopt; //当命令行选项字符不包括在optstring中或者选项缺少必要的参数时,该选项存储在optopt 中,getopt返回'?’、
int getopt(int argc, char * const argv[], const char *optstring);
调用一次,返回一个选项。在命令行选项参数再也检查不到optstring中包含的选项时,返回-1,同时optind储存第一个不包含选项的命令行参数。
首先说一下什么是选项,什么是参数。
1.单个字符,表示选项,
2.单个字符后接一个冒号:表示该选项后必须跟一个参数。参数紧跟在选项后或者以空格隔开。该参数的指针赋给optarg。
3 单个字符后跟两个冒号,表示该选项后必须跟一个参数。参数必须紧跟在选项后不能以空格隔开。该参数的指针赋给optarg。(这个特性是GNU的扩张)。
例如gcc -g -o test test.c ,其中g和o表示选项,test为选项o的参数。
上面是getopt()函数的基本含义,大家懂得了这些之后,我们一个例子加深一下理解。
例如我们这样调用getopt(argc, argv, "ab:c:de::");
从上面我们可以知道,选项a,d没有参数,选项b,c有一个参数,选项e有有一个参数且必须紧跟在选项后不能以空格隔开。getopt首先扫描argv[1]到argv[argc-1],并将选项及参数依次放到argv数组的最左边,非选项参数依次放到argv的最后边。
代码如下:
- #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:%d\n",optind,opterr);
- printf("--------------------------\n");
- while ((ch = getopt(argc, argv, "ab:c:de::")) != -1)
- {
- printf("optind: %d,argc:%d,argv[%d]:%s\n", optind,argc,optind,argv[optind]);
- switch (ch) {
- case 'a':
- printf("HAVE option: -a\n\n");
- break;
- case 'b':
- printf("HAVE option: -b\n");
- printf("The argument of -b is %s\n\n", optarg);
- break;
- case 'c':
- printf("HAVE option: -c\n");
- printf("The argument of -c is %s\n\n", optarg);
- break;
- case 'd':
- printf("HAVE option: -d\n");
- break;
- case 'e':
- printf("HAVE option: -e\n");
- printf("The argument of -e is %s\n\n", optarg);
- break;
- case '?':
- printf("Unknown option: %c\n",(char)optopt);
- break;
- }
- }
- printf("----------------------------\n");
- printf("optind=%d,argv[%d]=%s\n",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
getopt函数用法的更多相关文章
- getopt函数的使用——分析命令行参数
getopt(分析命令行参数) getopt(分析命令行参数) 短参数的定义 返回值 范例 getopt_long 相关函数表头文件#include<unistd.h> 函数声明int g ...
- Python中getopt()函数的使用
在运行程序时,可能需要根据不同的条件,输入不同的命令行选项来实现不同的功能.目前有短选项和长选项两种格式.短选项格式为"-"加上单个字母选项:长选项为"--"加 ...
- Linux编程里getopt_long_only函数用法详解
在程序中难免需要使用命令行选项,可以选择自己解析命令行选项,但是有现成的,何必再造轮子.下面介绍使用getopt_long_only和getopt_long(两者用法差不多)解析命令行选项. 程序中主 ...
- C++中的getopt的用法
getopt的用法 getopt被用来解析命令行选项参数.就不用自己写东东处理argv了. 点击(此处)折叠或打开 #include <unistd.h> extern char *opt ...
- Oracle 中 decode 函数用法
Oracle 中 decode 函数用法 含义解释:decode(条件,值1,返回值1,值2,返回值2,...值n,返回值n,缺省值) 该函数的含义如下:IF 条件=值1 THEN RETURN(翻译 ...
- memcpy函数用法
memcpy函数用法 .分类: VC++ VC++ mfc matlab 2011-12-01 19:17 14538人阅读 评论(0) 收藏 举报 null 原型:extern void *memc ...
- Python回调函数用法实例详解
本文实例讲述了Python回调函数用法.分享给大家供大家参考.具体分析如下: 一.百度百科上对回调函数的解释: 回调函数就是一个通过函数指针调用的函数.如果你把函数的指针(地址)作为参数传递给另一个函 ...
- php中opendir函数用法实例
这篇文章主要介绍了php中opendir函数用法,以实例形式详细讲述了opendir函数打开目录的用法及相关的注意事项,具有一定的参考借鉴价值,需要的朋友可以参考下 本文实例分析了php中opendi ...
- assert()函数用法总结
assert()函数用法总结 assert宏的原型定义在<assert.h>中,其作用是如果它的条件返回错误,则终止程序执行,原型定义: #include <assert.h> ...
随机推荐
- Android开发之常用必备工具类图片bitmap转成字符串string与String字符串转换为bitmap图片格式
作者:程序员小冰,CSDN博客:http://blog.csdn.net/qq_21376985 QQ986945193 博客园主页:http://www.cnblogs.com/mcxiaobing ...
- Android开发之清除缓存功能实现方法,可以集成在自己的app中,增加一个新功能。
作者:程序员小冰,CSDN博客:http://blog.csdn.net/qq_21376985 Android开发之清除缓存功能实现方法,可以集成在自己的app中,增加一个新功能. 下面是一个效果图 ...
- OpenJ_Bailian - 2995-登山(两遍最长上升子序列+枚举顶点)
五一到了,PKU-ACM队组织大家去登山观光,队员们发现山上一个有N个景点,并且决定按照顺序来浏览这些景点,即每次所浏览景点的编号都要大于前一个浏览景点的编号.同时队员们还有另一个登山习惯,就是不连续 ...
- es6使用箭头函数需要注意的问题
this问题 箭头函数没有它自己的this值,箭头函数内的this值继承自外围作用域,谁定义的函数,this指向谁 箭头函数要实现类似纯函数的效果,必须剔除外部状态.所以箭头函数不具备普通函数里常见的 ...
- 在一张id连续的表中找出缺失的id
有这样一张表: create table tb_lostid( id number(6,0) primary key not null, name nvarchar2(20) not null ) 可 ...
- fake_useragent.errors.FakeUserAgentError: Maximum amount of retries reached解决方法!
UserAgent 就是用户代理,又叫报头,是一串字符串,相当于浏览器的身份证号,在利用爬虫爬取网站数据时,频繁更换它可以避免触发相应的反爬机制. fake-useragent对频繁更换UserAge ...
- 鼠标移到图片上图片放大【css3实例】
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- 企业网站还是要考虑兼容至少IE10
中国国情,大部分企业还在使用win7,IE浏览器.为了兼容这些,还是少用比较VUE等一些高级的框架,改为使用jquery.用惯了VUE,jquey好多忘得差不多了,其中遇到的问题及解决方案 ajax, ...
- Linux:用tar解压文件出现错误Not found in archive
问题:用tar解压文件出现错误Not found in archive 解决办法:加上-C参数 tar -zxvf ZenTaoPMS.8.1.3.zbox_64.gz -C /usr 因为压缩文件 ...
- 使用 usbmon 抓取 usb 总线上的数据
使用 usbmon 抓取 usb 总线上的数据 usbmon 即 usb monitor,是 linux 内置的 usb 抓包工具.usbmon 本质是一个内核模块,在我的 ubuntu14.0 4中 ...