getopt_long 命令参数解析函数欣赏

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;
}

代码测试

 




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值