getopt,getoptlong学习
#include <unistd.h>
extern char *optarg;
extern int optind,
extern int opterr,
extern int optopt;
int getopt(int argc, char * const argv[], const char *optstring);
定义了四个全局变量:optarg是选项的参数指针,optind记录目前查找的位置,当opterr = 0时,getopt不向stderr输出错误信息。当命令选项字符不包括在optstring中或者缺少必要的参数时,该选项存储在optopt中,getopt返回 '?'
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h> int main(int argc, char **argv)
{
int opt = ; while ((opt = getopt(argc, argv, "if:?lr::")) != -)
{
switch(opt)
{
case 'i':
case 'l':
printf("option: %c\n", opt);
break;
case 'f':
printf("filename: %s\n", optarg);
break;
case 'r':
printf("arg:%s\n", optarg);
break;
case ':':
printf("option needs a value\n");
break;
case '?':
printf("unknow option: %c\n", optopt);
break;
}
} for (; optind < argc; optind++)
{
printf("argument: %s\n", argv[optind]); ar return ;
}
struct option
{
char *name;
int has_arg;
int *flag;
int val;
};
struct option longopts[] =
{
{"initialize", , NULL, 'i'},
{"filename", , NULL, 'f'},
{"list", , NULL, 'l'},
{"restart", , NULL, 'r'}
};
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h> #define _GNU_SOURCE
#include <getopt.h> int main(int argc, char **argv)
{
int opt; struct option longopts[] =
{
{"initialize", , NULL, 'i'},
{"filename", , NULL, 'f'},
{"list", , NULL, 'l'},
{"restart", , NULL, 'r'}
}; while ((opt = getopt_long(argc, argv, ":if:lr", longopts, NULL)) != -)
{
switch(opt)
{
case 'i':
case 'l':
case 'r':
printf("option: %c\n", opt);
break;
case 'f':
printf("filename: %s\n", optarg);
break;
case ':':
printf("option needs a value\n");
break;
case '?':
printf("unknow option: %c\n", optopt);
break;
}
} for (; optind < argc; optind++)
{
printf("argument: %s\n", argv[optind]); } return ;
}
getopt,getoptlong学习的更多相关文章
- windows下的getopt/getoptlong函数
windows下的getopt/getoptlong函数 getopt/getopt_long函数是GNU C中的函数,在linux编程中很常用到.这里就不介绍了. windows下没有找到类似的函数 ...
- windows下的getopt/getoptlong函数(拷贝GNU C的库函数)
http://www.cnblogs.com/oloroso/p/4856104.html
- webbench源码学习-->命令行选项解析函数getopt和getopt_long函数
对于webbench这个网站压力测试工具网上介绍的很多,有深度详解剖析的,对于背景就不在提了, 听说最多可以模拟3万个并发连接去测试网站的负载能力,这里主要是学习了一下它的源码,做点 笔记. 官方介绍 ...
- python getopt学习记录
有时候我们需要写一些脚本处理一些任务,这时候往往需要提供一些命令行参数,根据不同参数进行不同的处理,在Python里,命令行的参数和C语言很类似(因为标准Python是用C语言实现的).在C语言里,m ...
- getopt模块的学习
在运行程序时,可能需要根据不同的条件,输入不同的命令行选项来实现不同的功能.目前有短选项和长选项两种格式.短选项格式为"-"加上单个字母选项:长选项为"--"加 ...
- getopt 学习
https://www.cnblogs.com/qingergege/p/5914218.html
- 软件工程线上课程(C语言实践篇)学习心得总结
林牧 + 原创作品转载请注明出处 + <软件工程(C编码实践篇)>MOOC课程http://mooc.study.163.com/course/USTC-1000002006 软件工程的理 ...
- 软件工程(C编码实践)学习总结及心得
徐礼超 原创作品转载请注明出处:http://www.cnblogs.com/xulichao <软件工程(C编码实践篇)>MOOC课程http://mooc.study.163.com ...
- 命令行选项解析函数(C语言):getopt()和getopt_long()
命令行选项解析函数(C语言):getopt()和getopt_long() 上午在看源码项目webbench时,刚开始就被一个似乎挺陌生函数getopt_long()给卡住了,说实话这函数没怎么见过, ...
随机推荐
- Centos6.5中安装和配置vsftp详细总结
一.vsftp安装篇 #查看是否安装:rpm -qa|grep vsftpd#卸载vsftpdrpm -e vsftpd-2.2.2-11.el6_3.1x86_64 --nodeps# 安装vsft ...
- PHP Excel 下载数据,并分页下载
直接上代码: 调用下载Excel: $total=$duoduo->count(MOD.' as a',$where); $objExcel= SelfExcelObject(); //导出 i ...
- 【转】java.util.Arrays.asList 的用法
DK 1.4对java.util.Arrays.asList的定义,函数参数是Object[].所以,在1.4中asList()并不支持基本类型的数组作参数. JDK 1.5中,java.util.A ...
- Dom addEventlistener与id 绑定事件的区别(续)
addEventListener 第三个参数为 useCapture. 以一个例子说明. <div id="div1" style="background: blu ...
- PHP的学习--在Atom中使用XDebug(Mac)
之前写过一篇博客<PHP的学习--在sublime中使用XDebug(Ubuntu)>,讲了在Ubuntu系统 sublime 中配置 XDebug,其实配置好之后,我也很少用,原因有两点 ...
- 1Z0-053 争议题目解析
1Z0-053 争议题目解析 Summary 题目NO. 题目解析链接地址 题库答案 参考答案 考查知识点 24 http://www.cnblogs.com/jyzhao/p/5319220.ht ...
- css3 transition animation nick
时光转眼即逝,又到周六了,今天写点某部分人看不起的css玩玩! 转换 转换属性transform: 浏览器前缀: -webkit-transform;-o-transform;-moz-transfo ...
- 深入学习jQuery特性操作
× 目录 [1]获取特性 [2]设置特性 [3]删除特性 前面的话 每个元素都有一个或者多个特性,这些特性的用途就是给出相应元素或者其内容的附加信息.操作特性的DOM方法主要有3个:getAttrib ...
- ES6笔记(5)-- Generator生成器函数
系列文章 -- ES6笔记系列 接触过Ajax请求的会遇到过异步调用的问题,为了保证调用顺序的正确性,一般我们会在回调函数中调用,也有用到一些新的解决方案如Promise相关的技术. 在异步编程中,还 ...
- 如果你也会C#,那不妨了解下F#(6):面向对象编程之“类”
前言 面向对象的思想已经非常成熟,而使用C#的程序员对面向对象也是非常熟悉,所以我就不对面向对象进行介绍了,在这篇文章中将只会介绍面向对象在F#中的使用. F#是支持面向对象的函数式编程语言,所以你用 ...