您的位置:首页 > 运维架构 > Linux

Linux中getopt_long_only函数用法详解

2017-07-22 16:32 218 查看
[html] view
plain copy

  

在程序中难免需要使用命令行选项,可以选择自己解析命令行选项,但是有现成的,何必再造轮子。下面介绍使用getopt_long_only和getopt_long(两者用法差不多)解析命令行选项。

程序中主要使用:
短选项长选项是否需要参数
-n--username是(用户名)指定用户名
-d--debug是否已测试


1、函数出处 

[html] view
plain copy

#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);   


2、参数介绍

argc argv :直接从main函数传递而来

shortopts:短选项字符串。如”n:v",这里需要指出的是,短选项字符串不需要‘-’,而且但选项需要传递参数时,在短选项后面加上“:”。

longopts:struct option 数组,用于存放长选项参数。

longind:用于返回长选项在longopts结构体数组中的索引值,用于调试。一般置为NULL

下面介绍struct option

[html] view
plain copy

struct option    

{    

  const char *name;//长选项名    

  int has_arg;//是否需要参数    

  int *flag;    

  int val;    

};    

name:长选项名字 

has_arg:是否需要参数。值有三种情况

[html] view
plain copy

# define no_argument        0    //不需要参数    

# define required_argument  1    //必须指定参数    

# define optional_argument  2    //参数可选    

flag和val相互依赖,主要分两种情况:

(1)、flag为NULL,val值用于确定该长选项,所以需要为长选项指定唯一的val值。这里也为长选项和短选项建立了桥梁。

(2)、flag不为NULL,则将val值存放到flag所指向的存储空间,用于标识该长选项出现过。


3、返回值

程序中使用短选项,则返回短选项字符(如‘n'),当需要参数是,则在返回之前将参数存入到optarg中。

程序中使用长选项,返回值根据flag和val确定。当flag为NULL,则返回val值。所以根据val值做不同的处理,这也说明了val必须唯一。当val值等于短选项值,则可以使用短选项解析函数解析长选项;当flag不为NULL,则将val值存入flag所指向的存储空间,getopt_long返回0

出现未定义的长选项或者短选项,getopt_long返回?

解析完毕,getopt_long返回-1


4、实例

     理论要与实际相结合
实例一:

[html] view
plain copy

#include <stdio.h>    

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

}    

实例二:

[html] view
plain copy

#include <stdio.h>  

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

}  

至于运行结果,我只给出用例,大家有兴趣可以试试。

实例1:./test -n boy

实例2:./test -n boy

转自http://blog.csdn.net/pengrui18/article/details/8078813
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: