c语言 getopt函数,C - getopt函数
c语言-getopt函数
作用:获取解析程序运行时用户输入的外部传参(例如:输入ls时的-a、-l的短参,还有--help、-help长参。)
函数原型
#include
int getopt(int argc, char * const argv[],const char *optstring);
extern char *optarg; //存储选项的参数
extern int optind //指向下一个扫描的位置
, opterr //是否显示错误信息
, optopt; //读取到的字符如果不在opstring(上面例子是"alRtS")中,则存入optopt中
#include
int getopt_long(int argc, char * const argv[],const char *optstring,const struct option *longopts, int *longindex);
int getopt_long_only(int argc, char * const argv[],const char *optstring,const struct option *longopts, int *longindex);
函数说明
getopt():是获取短参数的,它可以获取-a,-l类型的短参数,也可以-al合并的获取到a和l。
getopt_long():是在getopt()的基础上获取长参数,它还可以获取--help这种参数。
getopt_long_only():也是在上一个函数基础上有所增加,输入长参数可以不用输入两个--,而是这样:-help。
argc和argv即传给main()函数的参数,函数如果读取结束会返回-1,否则返回对应的字符。
optstring:"abc:d:012",这里的一个冒号代表这个冒号前面这个字符出现时需要有参数,例如这样ls -a5,那个这个5就是一个参数,"c::"那么就是说c可以带参数也可以不带
长选项表:
struct option
{
const char *name;//name长选项的名字
int has_arg;//has_arg如果选项没有参数这一项是no_argument者0;如果有参数那么是required_argument或者1;如果参数是可选的是optional_argument或者2
int *flag;//flag如果为NULL,getopt_long()返回该结构val字段中的数值;如果不为NULL,getopt_long()会使得flag所指向的变量中填入val字段中的数值,并且getopt_long()返回0;通常flag设置为NULL,val设置为与该长选项对应的短选项
int val;//val一个值,一般写成长选项所对应的短选项
};
getopt示例
//录入参数
char opt;
opterr = 0; //不显示参数错误信息
while ((opt = getopt(argc,argv,"alRtS")) != -1) {
if (opt == 'a') {
param |= PARAM_a;
} else if (opt == 'l') {
param |= PARAM_l;
} else if (opt == 'R') {
param |= PARAM_R;
} else if (opt == 'S') {
param |= PARAM_S;
} else if (opt == 't') {
param |= PARAM_t;
} else {
printf("对不起,目前只支持参数R,S,a,t和l.\n");
exit(0);
}
}
getopt_long_only示例
#include
#include
#include
int main(int argc, char **argv)
{
int c;
int digit_optind = 0;
int this_option_optind = optind ? optind : 1;
int option_index = 0;
static struct option long_options[] = {
{"add", required_argument, 0, 0 },
{"append", no_argument, 0, 0 },
{"delete", required_argument, 0, 0 },
{"verbose", no_argument, 0, 0 },
{"create", required_argument, 0, 'c'},
{"file", required_argument, 0, 0 },
{0, 0, 0, 0 }
};
while (1) {
c = getopt_long_only(argc, argv, "abc:d:012",
long_options, &option_index);
if (c == -1)
break;
switch (c) {
case 0:
printf("option %s", long_options[option_index].name);
if (optarg)
printf(" with arg %s", optarg);
printf("\n");
break;
case '0':
case '1':
case '2':
if (digit_optind != 0 && digit_optind != this_option_optind)
printf("digits occur in two different argv-elements.\n");
digit_optind = this_option_optind;
printf("option %c\n", c);
break;
case 'a':
printf("option a\n");
break;
case 'b':
printf("option b\n");
break;
case 'c':
printf("option c with value '%s'\n", optarg);
break;
case 'd':
printf("option d with value '%s'\n", optarg);
break;
case '?':
break;
default:
printf("?? getopt returned character code 0%o ??\n", c);
}
}
if (optind < argc) {
printf("optind:%d\n",optind);
printf("non-option ARGV-elements: ");
while (optind < argc)
printf("%s ", argv[optind++]);
printf("\n");
}
exit(EXIT_SUCCESS);
}
c语言 getopt函数,C - getopt函数相关推荐
- getopt.h和getopt(),getopt_long()等函数
下载了一个牛人的代码,里面包括了一个getopt.h的头文件,在vs2008下无法通过编译,没有这个头文件,上网搜索了一些信息,记录下来,以方便以后查阅. getopt.h和对应的链接库不是每个编译器 ...
- python getopt函数,python3 getopt介绍
目录 getopt函数简介 实例演示 入门级 小型脚本-读取文件 getopt函数简介 运行程序时,根据不同的条件,输入不同的命令行选项实现不同的功能.getopt函数支持短选项和长选项两种格式.短选 ...
- python中getopt函数_python getopt函数详解
getopt模块可以帮助脚本解析sys.argv的命令行参数,它遵守和Unix getopt()函数相同的约定,该模块提供两个函数和一个异常. 1. getopt.getopt(args, optio ...
- c语言中getenv的作用,C语言putenv()函数和getenv()函数的使用详解
C语言putenv()函数和getenv()函数的使用详解 C语言putenv()函数:改变或增加环境变量头文件: #include4 定义函数: int putenv(const char * st ...
- C语言小知识:typedef\函数模板\
(1)typedef用法: typedef为C语言的关键字,作用是为一种数据类型定义一个新名字.这里的数据类型包括内部数据类型(int,char等)和自定义的数据类型(struct等).在编程中使用t ...
- C语言中连续调用rand函数,返回值不变
C语言中连续调用rand函数,返回值不变 最近用C语言随机函数编程发现了一个奇怪的现象: 简易版代码: #include<stdio.h> #include<stdlib.h> ...
- c语言随机数生成0 99函数,C语言生成随机数的函数、延时函数
该楼层疑似违规已被系统折叠 隐藏此楼查看此楼 下面C语言代码使用了生成随机数的函数.延时函数.请大家仔细观察其显示效果. 从以下代码,我们可以得出一个重要的结论:当上述两类函数被放入循环时,应作出一定 ...
- c++语言函数大全,C++函数
本篇文章帮大家学习C++函数,包含了C++函数使用方法.操作技巧.实例演示和注意事项,有一定的学习价值,大家可以用来参考. C++语言中的函数在其他编程语言中也称为过程或子例程. 我们可以创建函数来执 ...
- R语言ggplot2可视化分面图使用facet_wrap函数和facet_grid函数实战
R语言ggplot2可视化分面图使用facet_wrap函数和facet_grid函数实战 目录 R语言ggplot2可视化分面图使用facet_wrap函数和facet_grid函数实战
最新文章
- Google 图片搜索的原理是什么?
- C++著名程序库的比较和学习经验
- 华为云部署html网页,手把手教你如何在华为云服务器上部署一个自己的弹幕网站!...
- Spring和JSF集成:MVC螺母和螺栓
- 工作404-判断浏览器函数
- java快捷键查看目录,java取得快捷方式指向的路径
- mysql自动获取时间列_mysql自动获取时间日期的方法
- 1024告诉身边的程序员,今天他过节日
- 虽然现在有可以去码的软件了,可视频是如何自动跟踪打码的?
- 电子档案管理系统java,电子政务档案管理系统 [Java/JSP] struts+hibernate+spring-DZZW - CodeBus...
- Linux 添加中文字体库
- 易编远航第七套大漠多线程多开入门高级篇
- 码出高效:Java开发手册 ——百度网盘下载链接
- 量化研究-恐慌和贪婪指数
- PHP民俗文化管理系统,中国民俗文化
- 微信小程序根据sourceMap 定位代码错误位置
- 看看小编都用啥!实用Win7小软件大推荐
- 9 迭代器与组合模式
- 如何把Dom对象转换成jQuery对象,如何把jQuery对象转换成Dom对象
- java 数据类型的基本类型
热门文章
- linux ext4文件系统超级块损坏修复
- python TCP 通信
- iPhone苹果手机微信如何双开 苹果微信分身多开方法
- cisco无线配置:AIR-AP1242G-E-K9修复与设置手册
- 地球-可视化-模型-全球图的画法python
- python简单单元测试示范卷,Python单元测试实例详解
- echarts将网格线设置成虚线
- c语言中aver是什么意思_嵌入式C语言基础编程—5年程序员给你讲函数,你真的懂函数吗?...
- linux命令touch意思,Linux touch命令的用法
- SVM 用于将数据分类为两分类或多分类(Matlab代码实现)