linux C编程-解析命令行选项参数

目录

1、optarg/optind/optopt/opterr

2、getopt()函数

3、getopt_long()函数

4、获取最后一个有效选项参数


1、optarg/optind/optopt/opterr

#include<getopt.h>        

extern char *optarg;   
extern int optind, opterr, optopt;
optarg指向当前选项参数(如果有)的指针
optind再次调用 getopt() 时的下一个 argv指针的索引
optopt最后一个未知选项
opterr如果不希望getopt()打印出错信息,则只要将全域变量opterr设为0即可。

2、getopt()函数

用于解析命令行短选项参数(不能解析长选项参数)。

#include<getopt.h>          

int getopt(intargc, char * const argv[], const char *optstring);

参数 argc:main()函数传递过来的参数的个数

参数 argv:main()函数传递过来的参数的字符串指针数组

参数 optstring:选项字符串,告知 getopt()可以处理哪个选项以及哪个选项需要参数

选项说明示例
optstring 中第一个字符为冒号不打印出错信息,等同于将全域变量opterr设为0。/
单个字符

表示选项没有参数

注:如果该字符后面还有内容,那么后面的内容将被视为新的选项字符

-a
单字符加冒号表示选项有且必须加参数-a1或-a 1
单字符加冒号

表示选项可以有,也可以无

-a或-a1

注:其它格式都是错误的,此时 optarg = null

返回值:

选项字母如果选项成功找到
-1所有命令行选项都解析完毕
'?'

1、未知选项字符(如果遇到选项字符不在 optstring 中)

2、丢失参数(optstring 中第一个字符不为冒号)

':'丢失参数(optstring 中第一个字符为冒号)

测试代码如下

#include<stdio.h>
#include<unistd.h>
#include<getopt.h>


int main(int argc, char *argv[])
{
    int opt;
    char *string = "a::b:c";
    //char *string = ":a::b:c";
    while ((opt = getopt(argc, argv, string))!= -1)
    {  
        printf("opt = %c\t\t", opt);
		printf("optopt = %c\t\t", optopt);
        printf("optarg = %s\t\t",optarg);
        printf("optind = %d\t\t",optind);
        printf("argv[optind] = %s\n",argv[optind]);
    }  
}

3、getopt_long()函数

包含 getopt 功能,增加了解析长选项的功能。

#include<getopt.h>         

int getopt_long(int argc, char * const argv[], const char *optstring,
                          const struct option *longopts, int*longindex);

参数 argc:main()函数传递过来的参数的个数

参数 argv:main()函数传递过来的参数的字符串指针数组

参数 optstring:选项字符串,告知 getopt()可以处理哪个选项以及哪个选项需要参数(用法同getopt).

参数 longopts:指明了长参数的名称和属性。

struct option {
const char  *name;       /* 参数名称 */
int          has_arg;    /* 指明是否带有参数 */
int          *flag;      /* flag=NULL时,返回value;不为空时,*flag=val,返回0 */
int          val;        /* 用于指定函数找到选项的返回值或flag非空时指定*flag的值 */
};
has_arg参数可选值格式示例
no_argument表明长选项不带参数--name, --help 
required_argument表明长选项必须带参数--prefix /root或 --prefix=/root 

optional_argument
表明长选项的参数是可选的

--help或 –prefix=/root,

注:其它格式都是错误的 

参数 longindex:如果longindex非空,它指向的变量将记录当前找到参数符合longopts里的第几个元素的描述,即是longopts的下标值

返回值:对于短选项,返回值同getopt函数对于长选项,如果flag是NULL,返回val,否则返回0对于错误情况返回值同getopt函数。

测试代码如下

#include<stdio.h>
#include<unistd.h>
#include<getopt.h>


int main(int argc, char *argv[])
{
    int opt;
    int digit_optind = 0;
    int option_index = 0;
    char *string = "a::b:c:d";
    static struct option long_options[] =
    {  
        {"reqarg", required_argument,NULL, 'r'},
        {"optarg", optional_argument,NULL, 'o'},
        {"noarg",  no_argument,         NULL,'n'},
        {NULL,     0,                      NULL, 0},
    }; 
    while((opt =getopt_long_only(argc,argv,string,long_options,&option_index))!= -1)
    {  
        printf("opt = %c\t\t", opt);
        printf("optarg = %s\t\t",optarg);
        printf("optind = %d\t\t",optind);
        printf("argv[optind] =%s\t\t", argv[optind]);
        printf("option_index = %d\n",option_index);
    }  
}

4、获取最后一个有效选项参数

当getopt()返回-1时,使用optind来确定。

optind=1无有效选项参数
optind>1最后一个选项参数为argv[optind-1]

5、获取剩余参数个数

argc-optind

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值