getopt和getopt_long参数处理
1:getopt函数
getopt主要用于解析程序运行时所带的参数,原型如下:
#include <unistd.h>
int getopt(int argc, char * const argv[],const char *optstring);
extern char *optarg;
extern int optind, opterr, optopt;
一般的调用方式:
while((c = getopt(argc, argv, "xy:z::")) != -){
switch(c){
case 'x': ... ...
case 'y': ... ...
case 'z': ... ...
case '?': ... ...
... ....
}
}
参数描述:
1:argc和argv就是main函数的两个参数
2:optstring参数是描述可支持选项的字符串,如果某个选项后面需要参数值,则选项后面有一个":"
3:optarg 正如2所述,它指向了当前选项的参数值
4:optind 初始值是1,是下一个要解析的argv数组的索引(因为argv[0]是程序本身的名字,所以从1开始)
5:optopt 初始值为0,当函数解析到不认识的选项时(optstring中未描述的),getopt将会返回字符'?'且将不认识的选项保存在optopt中,并向stderr打印出错信息(也可手动将opterr设置为0就不会打印出来了)
6:opterr 初始值为1,如果当opterr设置为0时,getopt函数不向stderr输出信息
2:getopt_long函数
getopt_long函数是getopt的一类扩展,用于处理长选项的情况(长选项就是选项值不是一个字符而是一个字符串),原型如下:
#include <getopt.h>
int getopt_long(int argc, char * const argv[],
const char *optstring,
const struct option *longopts, int *longindex);
一般调用方式:
static struct option arg_options[] = {
{"clientid", required_argument, , 'c'},
{"foreground", no_argument, , 'f'},
{"background", no_argument, , 'b'},
{"hostname", required_argument, , 'H'},
{"hostname", required_argument, , 'h'},
{"interface", required_argument, , 'i'},
{"now", no_argument, , 'n'},
{"pidfile", required_argument, , 'p'},
{"quit", no_argument, , 'q'},
{"request", required_argument, , 'r'},
{"script", required_argument, , 's'},
{"version", no_argument, , 'v'},
{"help", no_argument, , '?'},
{, , , }
};
/* get options */
while () {
int option_index = ;
c = getopt_long(argc, argv, "c:fbH:h:i:np:qr:s:v", arg_options, &option_index);
if (c == -) break;
switch(c){
case 'c': ... ...
... ...
}
}
参数描述:
1:argc、argv、optstring和getopt函数是一样的
2:longopts 是一个指向struct option的结构体指针,这个结构体在getopt.h头文件是这样定义的:
struct option {
const char *name;
int has_arg;
int *flag;
int val;
};
name: 长选项的名字
has_arg: 有0,1,2三个值分别对应三个宏no_argument,required_argument,optional_argument,分别表示不要参数和需要参数和参数可有可无
flag: 决定了getopt_long函数如何返回,如果flag是NULL,getopt_long将返回val的值,否则将返回0(一般设置为NULL)
val: val值一般是长选项的首字母
3:longindex 如果它不是NULL,它将是当前解析的longopts数组的索引值
注意事项:
1:其它的外部变量如optarg,optopt,opterr等在getopt_long函数中含义不变
2:getopt_long接收参数的格式是"--" 而不是 "-"
3:参数longopts的作用其实是关联短选项和长选项的,所以一个程序的 -c XXX 和 --clientid XXX是同样的效果,只不过长选项提供了更完整的信息给使用者
getopt和getopt_long参数处理的更多相关文章
- 命令行参数处理-getopt()和getopt_long()
在实际编程当中,自己编写代码处理命令行参数是比较麻烦且易出错的.一般我们会直接使用getopt()和getopt_long()函数,下文将介绍具体的使用方法. getopt() getopt()用于处 ...
- 命令行参数解析函数getopt和getopt_long函数【转】
原文地址:http://blog.csdn.net/cashey1991/article/details/7942809 getopt和getopt_long函数 平时在写程序时常常需要对命令行参 ...
- getopt、getopt_long和getopt_long_only
GNU/Linux的命令行选项有两种类型:短选项和长选项,前者以 '-' 作为前导符,后者以 '--' 作为前导符.比如有一个命令: $ myprog -a vv --add -b --file a. ...
- getopt与getopt_long
如何通过命令行,为程序传入参数,可以使用函数getopt与getopt_long. 函数的声明如下: #include <unistd.h> int getopt(int argc, ch ...
- webbench源码学习-->命令行选项解析函数getopt和getopt_long函数
对于webbench这个网站压力测试工具网上介绍的很多,有深度详解剖析的,对于背景就不在提了, 听说最多可以模拟3万个并发连接去测试网站的负载能力,这里主要是学习了一下它的源码,做点 笔记. 官方介绍 ...
- Linux getopt()函数 getopt_long()函数---转
http://hi.baidu.com/scoundrelgg/item/d4083f8412eea05d26ebd97f Linux getopt()函数 getopt_long()函数 get_o ...
- C语言中getopt()和getopt_long()函数的用法
一.参考文章 1.C语言中getopt()和getopt_long()函数的用法 2.linux 中解析命令行参数 (getopt_long用法) 二.调试经验
- python 使用getopt 获取配置参数
在工程中特别是稍微大一点的项目基本上都会用到配置,就会涉及到配置文件的读取,配置参数的读取. 常用的解析配置文件的是configParser,解析命令行参数的则为getopt. getopt的参数可以 ...
- getopt、getopt_long命令参数
参数 optstring为选项字符串.如果选项字符串里的字母后接着冒号":",则表示还有相关的参数 getopt int getopt(int argc, char * const ...
随机推荐
- 原生js常用方法
原生JavaScript设置cookie值 function setCookie(name, value, Hours) { var d = new Date(); var offset = 8; v ...
- sparksql读写hbase
//写入hbase(hfile方式) org.apache.hadoop.hbase.client.Connection conn = null; try { SparkLog.debug(" ...
- ubuntu ssh配置
Secure Shell (SSH) is a cryptographic network protocol for operating network services securely over ...
- WebApi中利用Razor模板引擎来生成html
在服务器端基于Razor来生成html的一个思路 using System.Web.Mvc; using System.IO; using System.Web.Routing; using Syst ...
- 转战Java~
记得16年5月份开始学的Java,当时就是为了学Hadoop才学的Java基础,之后Hadoop没学成,倒是学了Java Web的东西,当时就是玩玩,然后弄了个WeChat后台,就完事了.然后就又回到 ...
- POJ 1815 Friendship(最大流最小割の字典序割点集)
Description In modern society, each person has his own friends. Since all the people are very busy, ...
- BAT批处理(三)
1.set set命令:显示.设置或删除变量.显示变量:set 或 set s 前者显示批处理当前已定义的所有变量及其值,后者显示所有以s开头的变量及值.设置变量:set aa=abcd 此句命令便可 ...
- react项目开发入门
v16.2.0 在html头部引入react相关js文件 <!-- react核心库--><script src="../static/react/react.produc ...
- QT分析之网络编程
原文地址:http://blog.163.com/net_worm/blog/static/127702419201002842553382/ 首先对Windows下的网络编程总结一下: 如果是服务器 ...
- 【bzoj4903/uoj300】[CTSC2017]吉夫特 数论+状压dp
题目描述 给出一个长度为 $n$ 的序列,求所有长度大于等于2的子序列个数,满足:对于子序列中任意两个相邻的数 $a$ 和 $b$ ($a$ 在 $b$ 前面),${a\choose b}\mod 2 ...