getopt_long 命令参数解析函数欣赏
来源:互联网 发布:sg wanna be 知乎 编辑:程序博客网 时间:2024/05/16 19:37
getopt_long 命令参数解析函数欣赏
函数
原型
#include <getopt.h>
int getopt_long(int argc, char * const argv[], const char *optstring, const struct option *longopts, int *longindex);
输入
参数
int argc: 输入参数个数,包括可执行项
char * const argv[]:输入参数字符串指针数组,没个元素对应各个参数
const char *optstring:指向短参数常量的指针(参看例子)
h,单字符,只表示一个选项
v:r:b:,单字符后接冒号,表示必须跟参数,且紧跟字符后或用空格隔开, 该参数指针赋值给optarg
const struct option *longopts:指向结构体长参数常量的指针(参看例子)
struct option{
const char *name; //长参数名
int has_arg; //有三类参数,如下所示
int *flag; //NULL,返回长参数对对应短参数名
int val; // getopt_long返回值(实际与flag综合决定)
};
no_argument(或者是0),表示该参数后面不跟参数值
required_argument(或者是1),表示该参数后面一定要跟个参数值
optional_argument(或者是2),表示该参数后面可以跟,也可以不跟参数值
int *longindex:通常为NULL
输出
参数
参数全部解析完成,返回-1
应用实例
下面是本人在DM6446项目中使用的代码,供大家学习.
/* ============================================================================ Name : getopt_long.c Author : sinonon Version : v0.0 Copyright : Your copyright notice Description : getopt_long in C, Ansi-style ============================================================================ */#include <stdio.h>#include <stdlib.h>#include <string.h>#include <getopt.h> #define __DEBUG/* Enables or disables debug output */#ifdef __DEBUG#define DBG(fmt, args...) fprintf(stderr, "XJTU_Encode: " fmt, ## args)#else#define DBG(fmt, args...)#endif#define ERR(fmt, args...) fprintf(stderr, "XJTU_EncodeError: " fmt, ## args) static void usage(void){ fprintf(stderr, "Usage: encode [options]\n\n" "Options:\n" "-v | --videofile Video file to record to\n" "-r | --resolution Video resolution ('width'x'height') [720x576]\n" "-b | --bitrate Bit rate to encode video at [variable]\n" "-t | --time Number of seconds to run the demo [infinite]\n" "-h | --help Print this message\n\n" "You must supply a video file or\n" "with appropriate extensions for the file formats.\n\n");} /****************************************************************************** * parseArgs ******************************************************************************/static void parseArgs(int argc, char *argv[]){const char shortOptions[] = "v:r:b:t:h";const struct option longOptions[]={{"videofile", required_argument, NULL, 'v'},{"resolution", required_argument, NULL, 'r'},{"videobitrate", required_argument, NULL, 'b'},{"time", required_argument, NULL, 't'},{"help", no_argument, NULL, 'h'},{0, 0, 0, 0}}; int c; char *extension; int imageWidth; int imageHeight; for (;;) {if(argc<2) {ERR("Input parameters error!\n"); break;} c = getopt_long(argc, argv, shortOptions, longOptions,NULL); if (c == -1) {break;} switch (c) { case 0: break; case 'v': extension = rindex(optarg, '.'); if (extension == NULL) { DBG("Video file without extension: %s\n", optarg); exit(EXIT_FAILURE); } if (strcmp(extension, ".264") == 0) { DBG("Init menu videoEncoder=H264_VIDEO_ENCODER\n "); } else if (strcmp(extension, ".mpeg4") == 0) { DBG("Init menu videoEncoder=MPEG4_VIDEO_ENCODER\n "); } else { ERR("Unknown video file extension: %s\n",extension); exit(EXIT_FAILURE); } break; case 'r': if (sscanf(optarg, "%dx%d", &imageWidth,&imageHeight) != 2) { ERR("Invalid resolution supplied (%s)\n",optarg); usage(); exit(EXIT_FAILURE); }DBG("Video resolution : %d x %d\n", imageWidth, imageHeight); break; case 'b': DBG("videoBitRate: %d.\n",atoi(optarg)); break; case 't': DBG("time: %d.\n",atoi(optarg)); break; case 'h': usage(); exit(EXIT_SUCCESS); default: usage(); exit(EXIT_FAILURE); } }} int main(int argc, char *argv[]){ parseArgs(argc,argv); return 0;}
代码测试
- getopt_long 命令参数解析函数欣赏
- 命令行参数解析函数getopt_long()
- 命令行参数解析函数:getopt和getopt_long
- 命令行参数解析函数getopt_long() 使用详解
- getopt_long()----命令行参数解析函数使用详解
- 命令解析函数getopt_long的用法
- getopt、getopt_long命令参数
- 程序参数解析函数使用getopt_long()函数,改编自webbench
- 参数控制---getopt_long函数
- getopt_long()解析命令行选项参数
- getopt_long 解析命令行参数(--help)
- 解析命令行的getopt_long()函数
- linux之getopt_long函数解析
- 命令处理、getopt_long函数说明
- 命令行参数解析: getopt()与getopt_long()
- 命令行参数解析: getopt()与getopt_long()
- 命令行参数解析: getopt()与getopt_long()
- linux 中解析命令行参数 (getopt_long用法)
- 微信js-sdk签名算法
- Codeforces Round #309 (Div. 1) A Kyoya and Colored Balls
- Android Studio 新建项目的R文件丢失的解决方法
- P2P内网穿透原理-tcp
- LeetCode题解——3Longest Substring Without Repeating Characters
- getopt_long 命令参数解析函数欣赏
- diff&&patch
- UNIX环境高级编程学习笔记(八)进程环境
- 网易游戏技术公开日分享
- Sqoop处理Clob与Blob字段
- Server.Transfer 页面传值.
- C#课程设计
- Linux下安装redis
- python验证码识别