Linux getopt()函数 getopt_long()函数---转
http://hi.baidu.com/scoundrelgg/item/d4083f8412eea05d26ebd97f
Linux getopt()函数 getopt_long()函数
get_opt()函数:
函数原型::
#include <unistd.h>
int getopt(int argc, char * const argv[], const char *optstring);
extern char *optarg;
extern int optind, opterr, optopt;
用法见右边栏
1.参数说明:
optstring:选项字母组成的字串。如果该字串里的任一字符后面有冒号,那么这个选项就要求有选项参数。
char *optarg:当前选项参数字串(如果有)。
int optind:argv的当前索引值。当getopt()在while循环中使用时,循环结束后,剩下的字串视为操作数,在argv[optind]至argv[argc-1]中可以找到。
int opterr:这个变量非零时,getopt()函数为“无效选项”和“缺少参数选项,并输出其错误信息。
int optopt:当发现无效选项字符之时,getopt()函数或返回'?'字符,或返回':'字符,并且optopt包含了所发现的无效选项字符。
2.更改getopt()函数的出错信息输出与否:
a. 在调用getopt()之前,将opterr设置为0,这样就可以在getopt()函数发现错误的时候强制它不输出任何消息。
b. 在optstring参数的第一个字符前加冒号,那么getopt()函数就会保持沉默,并根据错误情况返回不同字符,如下:
“无效选项” —— getopt()返回'?',并且optopt包含了无效选项字符(这是正常的行为)。
“缺少选项参数” —— getopt()返回':',如果optstring的第一个字符不是冒号,那么getopt()返回'?',这会使得这种情况不能与无效选项的情况区分开。
/*get_opt function test
huasion 20090920
*/
#include <stdio.h>
#include <unistd.h>
int main( int argc, char ** argv)
{
int i,oc;
int res = 0;
char *b_opt_arg;
char ec;
/*commen print argument*/
printf("******************************************************\n");
printf("usage: get_opt \n ");
printf("argc=%d\n", argc);
for(i=0;i<argc;i++)
printf("argv[%d]:%s\n", i, argv[i]);
printf("******************************************************\n");
/*over*/
while((oc=getopt(argc,argv,":ab:cd:")) != -1){
switch(oc){
case 'a':
printf("case a optind = %d.\n",optind);
break;
case 'b':
printf("case b have a value,optarg = %s ,optind = %d.\n",optarg,optind);
break;
case 'c':
printf("case c optind = %d.\n",optind);
break;
case 'd':
printf("case d have a value optarg = %s ,optind = %d.\n",optarg,optind);
break;
case '?':
ec = (char)optopt;
printf("option \'%c\' invalid! optind = %d, .\n",ec,optarg,optind);
case ':':
printf("getopt() return value : , you need a option value \n");
default:
break;
}
}
printf("it is over, res = %d \n",res);
}
**********************************分割线**********************************
getopt_long()函数
函数原型:
int getopt_long(int argc, char * const argv[], const char *optstring, const struct option *longopts, int *longindex);
前三个参数的含义和getopt()函数一致,其余意义如下:
longopts: 长选项表地址 ,定义为:
struct option{
const char *name;
int has_arg;
int *flag;
int val;
};
longindex: 如果没有设置为NULL,那么它就指向一个变量,这个变量会被赋值为寻找到的长选项在longopts中的索引值,这可以用于错误诊断。
const char *name: 这是选项名,前面没有短横线。譬如"help"、"verbose"之类。
int has_arg:描述了选项是否有选项参数。如果有,是哪种类型的参数,此时,它的值一定是下表中的一个。符号常量 数值 含义
no_argument 0 选项没有参数
required_argument 1 选项需要参数
optional_argument 2 选项参数可选
int *flag
如 果这个指针为NULL,那么getopt_long()返回该结构val字段中的数值。如果该指针不为NULL,getopt_long()会使得它所指 向的变量中填入val字段中的数值,并且getopt_long()返回0。如果flag不是NULL,但未发现长选项,那么它所指向的变量的数值不变。
int val
这 个值是发现了长选项时的返回值,或者flag不是NULL时载入*flag中的值。典型情况下,若flag不是NULL,那么val是个真/假值,譬如1 或0;另一方面,如果flag是NULL,那么val通常是字符常量,若长选项与短选项一致,那么该字符常量应该与optstring中出现的这个选项的 参数相同。
每个长选项在长选项表中都有一个单独条目,该条目里需要填入正确的数值。数组中最后的元素的值应该全是0。数组不需要排序,getopt_long()会进行线性搜索。但是,根据长名字来排序会使程序员读起来更容易。
/*get_opt function test
huasion 20090920
*/
#include <stdio.h>
#include <unistd.h>
#include <getopt.h>
int main( int argc, char ** argv)
{
int i,oc;
int res = 0;
char *b_opt_arg;
char ec;
int do_name, do_gf_name;
char *l_opt_arg;
struct option longopts[] = {
{ "name", no_argument, &do_name, 1 },
{ "gf_name", no_argument, &do_gf_name, 1 },
{ "love", required_argument, NULL, 'l' },
{ 0, 0, 0, 0},
};
/*commen print argument*/
printf("******************************************************\n");
printf("usage: get_opt \n ");
printf("argc=%d\n", argc);
for(i=0;i<argc;i++)
printf("argv[%d]:%s\n", i, argv[i]);
printf("******************************************************\n");
/*over*/
while((oc = getopt_long(argc, argv, ":l:", longopts, NULL)) != -1){
switch (oc){
case 'l':
l_opt_arg = optarg;
printf("Our love is %s!\n", l_opt_arg);
break;
case 0:
printf("getopt_long()设置变量 : do_name = %d\n", do_name);
printf("getopt_long()设置变量 : do_gf_name = %d\n", do_gf_name);
break;
}
}
printf("it is over, res = %d \n",res);
}
#include <stdio.h>
#include <getopt.h>
int do_name, do_gf_name;
char *l_opt_arg;
struct option longopts[] = {
{ "name", no_argument, NULL, 'n' },
{ "gf_name", no_argument, NULL, 'g' },
{ "love", required_argument, NULL, 'l' },
{ 0, 0, 0, 0},
};
int main(int argc, char *argv[])
{
int c;
while((c = getopt_long(argc, argv, ":ngl:", longopts, NULL)) != -1){
//其中,选项以及其有无参数由":ngl:"决定 而不是在option longopts[] 表中的值决定
switch (c){
case 'n':
printf("My name is LYR.\n");
break;
case 'g':
printf("Her name is BX.\n");
break;
case 'l':
l_opt_arg = optarg;
printf("Our love is %s!\n", l_opt_arg);
break;
}
}
return 0;
}
Linux getopt()函数 getopt_long()函数---转的更多相关文章
- 命令行参数解析函数getopt和getopt_long函数【转】
原文地址:http://blog.csdn.net/cashey1991/article/details/7942809 getopt和getopt_long函数 平时在写程序时常常需要对命令行参 ...
- C语言中getopt()和getopt_long()函数的用法
一.参考文章 1.C语言中getopt()和getopt_long()函数的用法 2.linux 中解析命令行参数 (getopt_long用法) 二.调试经验
- 命令行參数选项处理:getopt()及getopt_long()函数使用
在执行某个程序的时候,我们通常使用命令行參数来进行配置其行为.命令行选项和參数控制 UNIX 程序,告知它们怎样动作. 当 gcc的程序启动代码调用我们的入口函数 main(int argc ...
- webbench源码学习-->命令行选项解析函数getopt和getopt_long函数
对于webbench这个网站压力测试工具网上介绍的很多,有深度详解剖析的,对于背景就不在提了, 听说最多可以模拟3万个并发连接去测试网站的负载能力,这里主要是学习了一下它的源码,做点 笔记. 官方介绍 ...
- getopt() getopt_long()函数手册[中文翻译]
getopt()函数 getopt_long函数 函数原型(function prototype) #include <unistd.h> int getopt(int argc, cha ...
- 命令行参数处理-getopt()和getopt_long()
在实际编程当中,自己编写代码处理命令行参数是比较麻烦且易出错的.一般我们会直接使用getopt()和getopt_long()函数,下文将介绍具体的使用方法. getopt() getopt()用于处 ...
- [置顶] getopt_long函数基本用法-linux
一.感性认识: [c-sharp] view plain copy #include <stdio.h> #include <getopt.h> char * l_opt ...
- getopt_long函数使用【转】
转自:https://blog.csdn.net/cashey1991/article/details/7942809 平时在写程序时常常需要对命令行参数进行处理,当命令行参数个数较多时,如果按照顺序 ...
- Linux编程里getopt_long_only函数用法详解
在程序中难免需要使用命令行选项,可以选择自己解析命令行选项,但是有现成的,何必再造轮子.下面介绍使用getopt_long_only和getopt_long(两者用法差不多)解析命令行选项. 程序中主 ...
随机推荐
- 十三、Node.js-fs模块(上)
Node.js内置的fs模块就是文件系统模块,负责读写文件以及对文件进行相关操作. 下面直接可参考下面的代码进行fs模块里面基本方法的学习: /** * Created by Administrato ...
- 【转】C# datagridview大小跟随窗口动态改变
源地址:https://blog.csdn.net/fengxing11/article/details/52527715
- 错误代码: 1142 REFERENCES command denied to user 'wuyong'@'localhost' for table 'orders'
错误代码: 1142 REFERENCES command denied to user 'wuyong'@'localhost' for table 'orders' 原因:在使用SQLyog操作数 ...
- promise思考
我写了三个单元块,分别对应三种业务场景 let query;query = (url) => { url=url||"传递的参数为空"; return new Promise ...
- C语言数据结构-链式队列的实现-初始化、销毁、清空、长度、队列头元素、插入、删除、显示操作
1.数据结构-链式队列的实现-C语言 typedef struct QNode { int data; struct QNode *next; }QNode,*QueuePtr; typedef st ...
- 重写成员“MySql.Data.Entity.MySqlConnectionFactory.CreateConnection(System.String)”时违反了继承安全性规则。重写方法的安全可访问性必须与所重写方法的安全可访问性匹配。
1,程序中使用加载反射出现下面的问题: 无法加载一个或多个请求的类型.有关更多信息,请检索 LoaderExceptions 属性. 然后把代码改了一下, try { types.AddRange ...
- 传智播客Springmvc_mybatis学习笔记
文件地址:https://download.csdn.net/download/qq_26078953/10614459
- Tensorflow可视化MNIST手写数字训练
简述] 我们在学习编程语言时,往往第一个程序就是打印“Hello World”,那么对于人工智能学习系统平台来说,他的“Hello World”小程序就是MNIST手写数字训练了.MNIST是一个手写 ...
- springcloud微服务总结五 服务熔断
一:雪崩效应 如下图所示:A作为服务提供者,B为A的服务消费者,C和D是B的服务消费者.A不可用引起了B的不可用,并将不可用像滚雪球一样放大到C和D时,导致整个系统瘫痪,雪崩效应就形成了. 雪崩过程: ...
- json、xml
json:(JavaScript Object Notation, JS 对象简谱) 是一种轻量级的数据交换格式.简单地说,JSON 可以将 JavaScript 对象中表示的一组数据转换为字符串,然 ...