getopt_long_only

时间:2023-03-09 01:49:27
getopt_long_only

st_i2c_parser.c中用到,遂学习一下:

参考:https://blog.csdn.net/pengrui18/article/details/8078813

https://blog.csdn.net/earbao/article/details/77785613

getopt_long_only和getopt_long(两者用法差不多)解析命令行选项

函数出处:

#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);
函数参数:

对于options类型参数可以有两种方式:
     1)短选项(short options):顾名思义,就是短小参数。它们通常包含一个连字号和一个字母(大写

或小写字母)。例如:-s,-h等。
     2)长选项(long options):长选项,包含了两个连字号和一些大小写字母组成的单词。例如,--

size,--help等。
     *注:一个程序通常会提供包括short options和long options两种参数形式的参数。

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

shortopts:短选项字符串。如”n:v",这里需要指出的是,短选项字符串不需要‘-’,但选项需要传递参数时,在短选项后面加上“:”。
longopts:struct option 数组,用于存放长选项参数。
longind:用于返回长选项在longopts结构体数组中的索引值,用于调试。一般置为NULL

struct option

{

const char *name;//长选项名

int has_arg;//是否需要参数

int *flag;

int val;

}

name:长选项名字
has_arg:是否需要参数。值有三种情况
# define no_argument        0    //不需要参数  
# define required_argument  1    //必须指定参数  
# define optional_argument  2    //参数可选   flag和val相互依赖,主要分两种情况:
(1)、flag为NULL,val值用于确定该长选项,所以需要为长选项指定唯一的val值。这里也为长选项和短选项建立了桥梁。
(2)、flag不为NULL,则将val值存放到flag所指向的存储空间,用于标识该长选项出现过。
程序中使用短选项,则返回短选项字符(如‘n'),当需要参数时,则在返回之前将参数存入到optarg中。
程序中使用长选项,返回值根据flag和val确定。当flag为NULL,则返回val值。所以根据val值做不同的处理,这也说明了val必须唯一。当val值等于短选项值,则可以使用短选项解析函数解析长选项;当flag不为NULL,则将val值存入flag所指向的存储空间,getopt_long返回0
出现未定义的长选项或者短选项,getopt_long返回?
解析完毕,getopt_long返回-1
#include <unistd.h>
      
extern char
*optarg;  //选项的参数指针
      
extern int optind,
  //下一次调用getopt的时,从optind存储的位置处重新开始检查选项。

extern int
opterr,  //当opterr=0时,getopt不向stderr输出错误信息。

extern int
optopt;  //当命令行选项字符不包括在optstring中或者选项缺少必要的参数时,该选项存储在optopt
中,getopt返回'?’

"a:b:cd::e",这就是一个选项字符串。对应到命令行就是-a ,-b ,-c ,-d, -e 。冒号又是什么呢? 冒号表示参数,一个冒号就表示这个选项后面必须带有参数(没有带参数会报错哦),但是这个参数可以和选项连在一起写,也可以用空格隔开,比如-a123 和-a   123(中间有空格) 都表示123是-a的参数;两个冒号的就表示这个选项的参数是可选的,即可以有参数,也可以没有参数,但要注意有参数时,参数与选项之间不能有空格(有空格会报错的哦),这一点和一个冒号时是有区别的。
char*optstring = “ab:c::”;
单个字符a 表示选项a没有参数 格式:-a即可,不加参数
单字符加冒号b: 表示选项b有且必须加参数 格式:-b 100或-b100,但-b=100错
单字符加2冒号c:: 表示选项c可以有,也可以无 格式:-c200,其它格式错误 getopt_long_only 函数与 getopt_long 函数使用相同的参数表,在功能上基本一致,只是 getopt_long 只将 --name 当作长参数,
但 getopt_long_only 会将 --name 和 -name 两种选项都当作长参数来匹配。getopt_long_only 如果选项 -name 不能在 longopts 中匹配,
但能匹配一个短选项,它就会解析为短选项。 实例一 以val区分参数
 #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;  
    }
实例二 以index区分参数
#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;
}