getopt_long_only
st_i2c_parser.c中用到,遂学习一下:
参考:https://blog.csdn.net/pengrui18/article/details/8078813
https://blog.csdn.net/earbao/article/details/77785613
getopt_long_only和getopt_long(两者用法差不多)解析命令行选项
函数出处:
#include <getopt.h> //getopt_long()头文件位置
int getopt_long (int ___argc, char *const *___argv,
const char *__shortopts,
const struct option *__longopts, int *__longind);
int getopt_long_only (int ___argc, char *const *___argv,
const char *__shortopts,
const struct option *__longopts, int *__longind);
函数参数:
对于options类型参数可以有两种方式:
1)短选项(short options):顾名思义,就是短小参数。它们通常包含一个连字号和一个字母(大写
或小写字母)。例如:-s,-h等。
2)长选项(long options):长选项,包含了两个连字号和一些大小写字母组成的单词。例如,--
size,--help等。
*注:一个程序通常会提供包括short options和long options两种参数形式的参数。
argc argv :直接从main函数传递而来
shortopts:短选项字符串。如”n:v",这里需要指出的是,短选项字符串不需要‘-’,但选项需要传递参数时,在短选项后面加上“:”。
longopts:struct option 数组,用于存放长选项参数。
longind:用于返回长选项在longopts结构体数组中的索引值,用于调试。一般置为NULL
{
const char *name;//长选项名
int has_arg;//是否需要参数
int *flag;
int val;
}
name:长选项名字
has_arg:是否需要参数。值有三种情况
# define no_argument 0 //不需要参数
# define required_argument 1 //必须指定参数
# define optional_argument 2 //参数可选
flag和val相互依赖,主要分两种情况:
程序中使用长选项,返回值根据flag和val确定。当flag为NULL,则返回val值。所以根据val值做不同的处理,这也说明了val必须唯一。当val值等于短选项值,则可以使用短选项解析函数解析长选项;当flag不为NULL,则将val值存入flag所指向的存储空间,getopt_long返回0
出现未定义的长选项或者短选项,getopt_long返回?
解析完毕,getopt_long返回-1
extern char
*optarg; //选项的参数指针
extern int optind,
//下一次调用getopt的时,从optind存储的位置处重新开始检查选项。
extern int
opterr, //当opterr=0时,getopt不向stderr输出错误信息。
extern int
optopt; //当命令行选项字符不包括在optstring中或者选项缺少必要的参数时,该选项存储在optopt
中,getopt返回'?’
char*optstring = “ab:c::”;
单个字符a 表示选项a没有参数 格式:-a即可,不加参数
单字符加冒号b: 表示选项b有且必须加参数 格式:-b 100或-b100,但-b=100错
单字符加2冒号c:: 表示选项c可以有,也可以无 格式:-c200,其它格式错误 getopt_long_only 函数与 getopt_long 函数使用相同的参数表,在功能上基本一致,只是 getopt_long 只将 --name 当作长参数,
但 getopt_long_only 会将 --name 和 -name 两种选项都当作长参数来匹配。getopt_long_only 如果选项 -name 不能在 longopts 中匹配,
但能匹配一个短选项,它就会解析为短选项。 实例一 以val区分参数
#include <stdlib.h>
#include <getopt.h> //getopt_long()头文件位置
int main(int argc, char** argv)
{
const char *optstring="n:v";
int c,deb,index;
struct option opts[]={{"username",required_argument,NULL,'n'},
{"version",no_argument,NULL,'v'},
{"debug",no_argument,&deb,1},
{0,0,0,0}};
while((c=getopt_long_only(argc,argv,optstring,opts,&index))!=-1)
{
switch(c)
{
case 'n'://-n 或者 --username 指定用户名
printf("username is %s\n",optarg);
break;
case 'v'://-v 或者--version,输出版本号
printf("version is 0.0.1 \n");
break;
case 0://flag不为NULL
printf("debug is %d\n",deb);
break;
case '?'://选项未定义
printf("?\n");
break;
default:
printf("c is %d\n",c);
break;
}
}
return 0;
}
#include <stdlib.h>
#include <getopt.h> //getopt_long()头文件位置
int main(int argc, char** argv)
{
const char *optstring="n:v";
int c,deb,index;
struct option opts[]={{"username",required_argument,0,0},
{"n",required_argument,0,0},
{"version",no_argument,0,0},
{"v",no_argument,0,0},
{"debug",no_argument,0,0},
{"d",no_argument,0,0},
{"help",no_argument,0,0},
{"h",no_argument,0,0}};
while((c=getopt_long_only(argc,argv,optstring,opts,&index))!=-1)
{
switch(index){
//-n或者--username
case 0:
case 1:
printf("username:%s\n",optarg);
break;
//-v或者--version
case 2:
case 3:
printf("version:1.0.0\n");
break;
//-d or --debug
case 4:
case 5:
printf("debug:yes\n");
break;
//-h or --help
case 6:
case 7:
printf("Help:?\n");
break;
default:
printf("other:%d\n",index);
break;
}
}
return 0;
}
getopt_long_only的更多相关文章
- 函数参数选项的处理getopt getopt_long getopt_long_only
转载:http://blog.chinaunix.net/uid-20321537-id-1966849.html 在头文件中int getopt(int argc,char *argv[], c ...
- getopt、getopt_long和getopt_long_only
GNU/Linux的命令行选项有两种类型:短选项和长选项,前者以 '-' 作为前导符,后者以 '--' 作为前导符.比如有一个命令: $ myprog -a vv --add -b --file a. ...
- Linux编程里getopt_long_only函数用法详解
在程序中难免需要使用命令行选项,可以选择自己解析命令行选项,但是有现成的,何必再造轮子.下面介绍使用getopt_long_only和getopt_long(两者用法差不多)解析命令行选项. 程序中主 ...
- 【C】命令行参数解析——getopt、getopt_long及getopt_long_only
前言 在linux下学习开源代码Webbench,遇到get_long等函数的用法,一时有点懵,故想深入了解这类命令行解析函数,并记此博文. 1.getopt getopt主要用来处理短命令行选项,例 ...
- getopt、getopt_long和getopt_long_only解析命令行参数
一:posix约定: 下面是POSIX标准中关于程序名.参数的约定: 程序名不宜少于2个字符且不多于9个字符: 程序名应只包含小写字母和阿拉伯数字: 选项名应该是单字符或单数字,且以短横 '-' 为前 ...
- 使用getopt_long来解析参数的小函数模板
getopt_long原型 #define no_argument 0 #define required_argument 1 #define optional_argument 2 struct o ...
- 命令行选项解析函数(C语言):getopt()和getopt_long()
命令行选项解析函数(C语言):getopt()和getopt_long() 上午在看源码项目webbench时,刚开始就被一个似乎挺陌生函数getopt_long()给卡住了,说实话这函数没怎么见过, ...
- Use getopt() & getopt_long() to Parse Arguments
Today I came across a function [getopt] by accident. It is very useful to parse command-line argumen ...
- windows下的getopt/getoptlong函数
windows下的getopt/getoptlong函数 getopt/getopt_long函数是GNU C中的函数,在linux编程中很常用到.这里就不介绍了. windows下没有找到类似的函数 ...
随机推荐
- numpy的searchsorted细品
import numpy as np a= np.arange(20) pos_left = a.searchsorted(3) #也可以写成np.searchsorted(a, 3), 注 ...
- python的执行顺序
为了区分是主动执行(如python test.py)还是被动调用(如import test),python用__name__来进行标识. 当主动执行时,__name__为__main__,当被调用时, ...
- python的__new__方法
https://www.cnblogs.com/kex1n/p/5991249.html https://blog.csdn.net/wwx890208/article/details/8053445 ...
- 2018-2019-1 20189221《Linux内核原理与分析》第五周作业
2018-2019-1 20189221<Linux内核原理与分析>第五周作业 实验四 实验过程 当用户态进程调用一个系统调用时,cpu切换到内核态并开始执行一个内核函数. 在Linux中 ...
- [LeetCode] 860. Lemonade Change_Easy tag: Greedy
At a lemonade stand, each lemonade costs $5. Customers are standing in a queue to buy from you, and ...
- [LeetCode] 414. Third Maximum Number_Easy
Given a non-empty array of integers, return the third maximum number in this array. If it does not e ...
- R语言的scale函数
1.数据的中心化 所谓数据的中心化是指数据集中的各项数据减去数据集的均值. 例如有数据集1, 2, 3, 6, 3,其均值为3 那么中心化之后的数据集为1-3,2-3,3-3,6-3,3-3,即:-2 ...
- .net委托链
委托链可以增加方法,可以移除方法,如果是无返回值的方法,我们把它们都绑定到一个委托上面的话,直接调用,那么调用此委托就会依次调用其中的方法:但是如果是多个有返回值的委托链,如果我们不采用特殊手段,直接 ...
- node.js初识01
1.对于node.js的安装在这里就不做过多的介绍了,安装成功后,可以通过cmd 输入node -v查看node的版本号,如图所示 2.开始我们的hello world,通过cmd进入所属文件夹,输入 ...
- HDU 2254 奥运(矩阵+二分等比求和)
奥运 [题目链接]奥运 [题目类型]矩阵+二分等比求和 &题解: 首先离散化城市,之后就是矩阵快速幂了,但让求的是A^(t1)+A^(t1+1)+...+A^(t2),我先想的是打表,但时间真 ...