Linux中getopt_long_only函数用法详解

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

程序中主要使用:

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

1、函数出处 

[html]  view plain  copy
  1. #include <getopt.h> //getopt_long()头文件位置    
  2. int getopt_long (int ___argc, char *const *___argv,    
  3.             const char *__shortopts,    
  4.                 const struct option *__longopts, int *__longind);    
  5. int getopt_long_only (int ___argc, char *const *___argv,    
  6.                  const char *__shortopts,    
  7.                      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
  1. struct option    
  2. {    
  3.   const char *name;//长选项名    
  4.   int has_arg;//是否需要参数    
  5.   int *flag;    
  6.   int val;    
  7. };    
name:长选项名字 
has_arg:是否需要参数。值有三种情况

[html]  view plain  copy
  1. # define no_argument        0    //不需要参数    
  2. # define required_argument  1    //必须指定参数    
  3. # 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
  1. #include <stdio.h>    
  2. #include <stdlib.h>    
  3. #include <getopt.h> //getopt_long()头文件位置    
  4.     
  5. int main(int argc, char** argv)    
  6. {    
  7.     const char *optstring="n:v";    
  8.     int c,deb,index;    
  9.     struct option opts[]={{"username",required_argument,NULL,'n'},    
  10.                           {"version",no_argument,NULL,'v'},    
  11.                           {"debug",no_argument,&deb,1},    
  12.                           {0,0,0,0}};    
  13.     while((c=getopt_long_only(argc,argv,optstring,opts,&index))!=-1)    
  14.     {    
  15.             
  16.         switch(c)    
  17.         {    
  18.         case 'n'://-n 或者 --username 指定用户名    
  19.             printf("username is %s\n",optarg);    
  20.             break;    
  21.         case 'v'://-v 或者--version,输出版本号    
  22.             printf("version is 0.0.1 \n");    
  23.             break;    
  24.         case 0://flag不为NULL    
  25.             printf("debug is %d\n",deb);    
  26.             break;    
  27.         case '?'://选项未定义    
  28.             printf("?\n");    
  29.             break;    
  30.         default:    
  31.             printf("c is %d\n",c);    
  32.             break;    
  33.         }    
  34.     }    
  35.     return 0;    
  36. }    

实例二:
[html]  view plain  copy
  1. #include <stdio.h>  
  2. #include <stdlib.h>  
  3. #include <getopt.h> //getopt_long()头文件位置  
  4.   
  5. int main(int argc, char** argv)  
  6. {  
  7.     const char *optstring="n:v";  
  8.     int c,deb,index;  
  9.     struct option opts[]={{"username",required_argument,0,0},  
  10.                           {"n",required_argument,0,0},  
  11.                           {"version",no_argument,0,0},  
  12.                           {"v",no_argument,0,0},  
  13.                           {"debug",no_argument,0,0},  
  14.                           {"d",no_argument,0,0},  
  15.                           {"help",no_argument,0,0},  
  16.                           {"h",no_argument,0,0}};  
  17.     while((c=getopt_long_only(argc,argv,optstring,opts,&index))!=-1)  
  18.     {  
  19.         switch(index){  
  20.             //-n或者--username  
  21.             case 0:  
  22.             case 1:  
  23.                 printf("username:%s\n",optarg);  
  24.                 break;  
  25.             //-v或者--version  
  26.             case 2:  
  27.             case 3:  
  28.                 printf("version:1.0.0\n");  
  29.                 break;  
  30.             //-d or --debug  
  31.             case 4:  
  32.             case 5:  
  33.                 printf("debug:yes\n");  
  34.                 break;  
  35.             //-h or --help  
  36.             case 6:  
  37.             case 7:  
  38.                 printf("Help:?\n");  
  39.                 break;  
  40.             default:  
  41.                 printf("other:%d\n",index);  
  42.                 break;  
  43.         }  
  44.     }  
  45.     return 0;  
  46. }  

至于运行结果,我只给出用例,大家有兴趣可以试试。
实例1:./test -n boy

实例2:./test -n boy


转自http://blog.csdn.net/pengrui18/article/details/8078813

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
`getopt_long` 是一个 C 语言的函数,用于解析命令行参数。与传统的 `getopt` 函数不同,`getopt_long` 支持长选项(如 --help)和短选项(如 -h)。 下面是 `getopt_long` 的基本用法: ```c #include <stdio.h> #include <stdlib.h> #include <getopt.h> int main(int argc, char *argv[]) { int c; int digit_optind = 0; while (1) { int option_index = 0; static struct option long_options[] = { {"add", required_argument, 0, 0 }, {"append", no_argument, 0, 0 }, {"delete", required_argument, 0, 0 }, {"verbose", no_argument, 0, 0 }, {"create", required_argument, 0, 'c'}, {"file", required_argument, 0, 0 }, {0, 0, 0, 0 } }; c = getopt_long(argc, argv, "abc:d:012", long_options, &option_index); if (c == -1) break; switch (c) { case 0: printf("option %s", long_options[option_index].name); if (optarg) printf(" with arg %s", optarg); printf("\n"); break; case '0': case '1': case '2': if (digit_optind != 0 && digit_optind != optind) printf("digits occur in two different argv-elements.\n"); digit_optind = optind; printf("option %c\n", c); break; case 'a': printf("option a\n"); break; case 'b': printf("option b\n"); break; case 'c': printf("option c with value '%s'\n", optarg); break; case 'd': printf("option d with value '%s'\n", optarg); break; case '?': break; default: printf("?? getopt returned character code 0%o ??\n", c); } } if (optind < argc) { printf("non-option ARGV-elements: "); while (optind < argc) printf("%s ", argv[optind++]); printf("\n"); } exit(EXIT_SUCCESS); } ``` 上面的代码演示了 `getopt_long` 的基本用法。其 `long_options` 数组定义了长选项的名称、参数类型以及对应的短选项(如果有的话)。在调用 `getopt_long` 函数时,第三个参数为短选项字符串,其冒号表示该选项需要参数。 函数返回值为下一个选项的字符编码,或者 -1 表示已经解析完所有选项。如果选项需要参数,则该参数存储在全局变量 `optarg` 。 更多关于 `getopt_long` 的用法,可以参考其官方文档:http://man7.org/linux/man-pages/man3/getopt_long.3.html
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值