命令行程序选项解析函数—getopt_long
来源:互联网 发布:linux系统装在哪个分区 编辑:程序博客网 时间:2024/04/30 04:56
转载请注明出处:http://blog.csdn.net/zhangyang0402/archive/2010/06/15/5671554.aspx
getopt函数只能解析短选项,getopt_long除了接受短选项,还能解析长选项。因此,getopt_long函数在linux命令行程序中使用得最广泛。
1. 函数原型
#include <getopt.h>
int getopt_long(int argc, char * const argv[], const char *optstring, const struct option *longopts, int *longindex);
2. 参数
(1)argc
(2)argv
(3)optstring:短选项字符集合。若只有长选项,则置为空字符串(“”)
(4)longopts:指向struct option数组的第一个元素,结构体option定义如下
struct option
{
const char *name; //长选项名称
int has_arg; //是否有参数no_argument(0) required_argument(1) optional_argument(2)
int *flag; //指定长选项的返回结果:NULL 返回val;非空返回0
int val; //返回的值或装载flag指向的变量
};
注:struct option 数组最后一个元素用0填充
(5)longindex 非空的话,指向长选项的索引
3. 返回值
对短选项,返回对应的选项字符;
对长选项,若flag为NULL,返回val;否则返回0
4. 测试
这里只测试长选项,至于短选项的处理则和getopt函数相同
mygetoptlong.c
执行结果:
$ ./mygetoptlong --input infile --all --output outfile --version --help file
option "input": infile
option "all":
option "output": outfile
option "version":
option "help":
Usage: ./mygetoptlong [--input infile] [--all] [--output outfile] [--version] [--help] [file]
non-opotion arguments below:
file
- 命令行程序选项解析函数—getopt_long
- getopt_long()解析命令行选项参数
- 命令行选项解析函数(C语言):getopt()和getopt_long()
- 命令行选项解析函数(C语言):getopt()、getopt_long()和getopt_long_only
- 命令行程序选项解析函数—getopt
- 命令行程序选项解析函数—getopt
- 命令行程序选项解析函数—getopt
- 命令行参数解析函数getopt_long()
- 解析命令行的getopt_long()函数
- linux-解析命令行选项getopt_long用法
- GNU长选项命令行解析getopt_long()
- 使用getopt_long解析程序长选项参数
- 常用命令行解析函数——getopt/getopt_long
- getopt函数和getopt_long函数 解析命令行
- getopt函数和getopt_long函数 解析命令行
- 选项解析:getopt() 和 getopt_long()函数
- 选项解析函数getopt()和getopt_long()
- 命令行参数解析函数:getopt和getopt_long
- 个人股
- IntelliJ IDEA 快捷键
- 平衡二叉树 AVL树
- 郁闷,除了郁闷还是郁闷
- 消息中间件及WebSphere MQ入门
- 命令行程序选项解析函数—getopt_long
- [JAVASCRIPT][EXTJS]直接用JSON创建树形控件(Ext.tree.TreePanel )
- visual editor for eclipse
- Asynchronous JavaScript and XML (AJAX) Introduction
- 嵌入式开发学习笔记
- VC++数据库技术系列(三)DAO对象模型组成
- 魅族M8游戏框架开发的思路及方法,同样适用于WinCE设备的游戏开发。
- 从文件 I/O 看 Linux 的虚拟文件系统
- C# 遗传算法求函数最大值