Linux选项解释-getopt和getopt_long函数

来源:互联网 发布:初级会计培训网络课程 编辑:程序博客网 时间:2024/05/17 04:09

Linux选项解释-getopt和getopt_long函数

一、命令行简介

解释分析命令行通常是所以程序的第一个任务,C语言通过argc和argv参数来访问它的命令行参数。

最简单的命令行处理技术可以通过if判断来表示,如下例:

if(argc>1 &&argv[1][0] == - &&argv[1][1] == h)  //判断命令行参数是否为-n

{

    do_something();

}

这样处理简单有序的命令行还可以,对于复杂的命令行处理显得有心无力,于是GNU提供两个函数专门用来处理命令行参数:getopt和getopt_long。

二、getopt函数

getopt()函数声明如下:

C代码
  1. #include <unistd.h>  
  2.   
  3. Int getopt(int argc, char *const argv[], const char *optstring);  
  4.   
  5. extern char *optarg;  
  6.   
  7. extern int optind, opterr, optopt;  
 

 

说明:函数中的argc和argv通常直接从main()到两个参数传递而来。optsting是选项参数组成的字符串,如果该字符串里任一字母后有冒号,那么这个选项就要求有参数,optarg就是选项参数。optind是当前索引,optopt用于当发现无效选项字符的时候,getopt函数或者返回“?”或者返回“:”字符,并且optopt包含了所发现的无效选项字符。

如果optstring参数的第一个字符是冒号,那么getopt会根据错误情况返回不同的字符,当错误是无效选项,getopt返回“?”,当错误是缺少选项参数,getopt返回“:”。

注:GNU getopt()第三个特点是optstring中的选项字符后面接两个冒号,就允许该选项有可选的选项参数。在选项参数不存在的情况下,GNU getopt()返回选项字符并将optarg设置为NULL。

例子:

C代码
  1. #include <stdio.h>  
  2.   
  3. #include <unistd.h>  
  4.   
  5. #include <getopt.h>  
  6.   
  7. char *para = ":ab:c";  
  8.   
  9. int main(int argc, char *argv[])  
  10.   
  11. {  
  12.   
  13.      int oc = -1;  
  14.   
  15.      char *b_input = NULL;  
  16.   
  17.      while((oc = getopt(argc, argv, para)) != -1)  
  18.   
  19.      {  
  20.   
  21.          switch(oc)  
  22.   
  23.          {  
  24.   
  25.           case 'a':  
  26.   
  27.               printf("input para is a/n");  
  28.   
  29.               break;  
  30.   
  31.           case 'b':  
  32.   
  33.               b_input = optarg;  
  34.   
  35.               printf("input para is b,and optarg is %s/n", b_input);  
  36.   
  37.               break;  
  38.   
  39.           case 'c':  
  40.   
  41.               printf("input para is c/n");  
  42.   
  43.               break;  
  44.   
  45.           case ':':  
  46.   
  47.               printf("option %c requires an argument/n",optopt);  
  48.   
  49.               break;  
  50.   
  51.           case '?':  
  52.   
  53.           default:  
  54.   
  55.               printf("option %c is invalid:ignored/n",optopt);  
  56.   
  57.              break;  
  58.   
  59.          }  
  60.   
  61.      }  
  62.   
  63.      return 0;  
  64.   
  65. }  
 

 

编译:

[root@heguangwu projects]# gcc -o getopt_ex getopt_ex.c

运行:

[root@heguangwu projects]# ./getopt_ex -a

input para is a

[root@heguangwu projects]# ./getopt_ex -a -b

input para is a

option b requires an argument

[root@heguangwu projects]# ./getopt_ex -d

option d is invalid:ignored

 

三、getopt_long函数

getopt_long用来处理长选项,使用man 3 getopt_long,得到其声明如下:

C代码
  1. #include <getopt.h>  
  2.   
  3. int getopt_long(int argc, char * const argv[], const char *optstring,   
  4.   
  5. const struct option *longopts, int *longindex);  
  6.   
  7. int getopt_long_only(int argc, char * const argv[], const char *optstring,   
  8.   
  9. const struct option *longopts, int *longindex);  
 

 

前三个参数与getopt相同,下一个参数是指向数组的指针,这个数组是option结构数组,option结构称为长选项表,其声明如下:

C代码
  1. struct option  
  2.  {  
  3.   
  4.     const char *name;  
  5.   
  6.     int  has_arg;  
  7.   
  8.     int  *flag;  
  9.   
  10.     int  val;  
  11.   
  12. };  
 

 

结构中的元素解释如下:

const char *name:选项名,前面没有短横线

int has_arg:描述长选项是否有参数,其值见下表

符号常量

数值

含义

no_argument

required_argument

optional_argument

0

1

2

选项没有参数

选项需要参数

选项参数是可选的

int *flag

如果该指针为NULL,那么getopt_long返回val字段的值;

如果该指针不为NULL,那么会使得它所指向的结构填入val字段的值,同时getopt_long返回0

int val

如果flag是NULL,那么val通常是个字符常量,如果短选项和长选项一致,那么该字符就应该与optstring中出现的这个选项的参数相同;

 

 

C代码
  1. #include <stdio.h>  
  2.   
  3. #include <unistd.h>  
  4.   
  5. #include <getopt.h>  
  6.   
  7. char *para = ":ab:cf:v";  
  8.   
  9. int do_all = 0;  
  10.   
  11. int do_help = 0;   
  12.   
  13. int do_version = 0;  
  14.   
  15. char *file = NULL;  
  16.   
  17. struct option longopt[] =   
  18.   
  19. {  
  20.   
  21.      {"all", no_argument, &do_all, 1},  
  22.   
  23.      {"file", required_argument, NULL, 'f'},  
  24.   
  25.      {"help", no_argument, &do_help, 1},  
  26.   
  27.      {"version", no_argument, &do_version, 1},  
  28.   
  29.      {"bob", required_argument, NULL, 'b'},  
  30.   
  31.      {0, 0, 0, 0},  
  32.   
  33. };  
  34.   
  35. int main(int argc, char *argv[])  
  36.   
  37. {  
  38.   
  39.     int oc = -1;  
  40.   
  41.     char *b_input = NULL;  
  42.   
  43.     while((oc = getopt_long(argc, argv, para, longopt, NULL)) != -1)  
  44.   
  45.     {  
  46.   
  47.          switch(oc)  
  48.   
  49.          {  
  50.   
  51.          case 'a':  
  52.   
  53.                printf("input para is a/n");  
  54.   
  55.               break;  
  56.   
  57.          case 'b':  
  58.   
  59.               b_input = optarg;  
  60.   
  61.               printf("input para is b,and optarg is %s/n", b_input);  
  62.   
  63.              break;  
  64.   
  65.         case 'c':  
  66.   
  67.              printf("input para is c/n");  
  68.   
  69.             break;  
  70.   
  71.         case 'v':  
  72.   
  73.             printf("input para is v/n");  
  74.   
  75.             break;  
  76.   
  77.         case 'f':  
  78.   
  79.             printf("input para is f/n");  
  80.   
  81.             file = "hello world";  
  82.   
  83.             break;  
  84.   
  85.         case 0:  
  86.   
  87.            break;  
  88.   
  89.         case ':':  
  90.   
  91.              printf("option %c requires an argument/n",optopt);  
  92.   
  93.              break;  
  94.   
  95.          case '?':  
  96.   
  97.          default:  
  98.   
  99.             printf("option %c is invalid:ignored/n",optopt);  
  100.   
  101.             break;  
  102.   
  103.          }  
  104.   
  105.      }  
  106.   
  107.      printf("do_all is %d/n",do_all);  
  108.   
  109.      printf("do_help is %d/n",do_help);  
  110.   
  111.      printf("do_version is %d/n",do_version);  
  112.   
  113.      printf("do_file is %s/n",file);  
  114.   
  115.      printf("bob is %s/n", b_input);  
  116.   
  117.      return 0;  
  118.   
  119. }  
 

 

 

执行的结果:只显示关键结果

[root@heguangwu projects]# ./opt_ex2 -a

input para is a

 

[root@heguangwu projects]# ./opt_ex2 --all

do_all is 1

 

[root@heguangwu projects]# ./opt_ex2 -f h

input para is f

do_file is hello world

 

[root@heguangwu projects]# ./opt_ex2 --bob aa

input para is b,and optarg is aa

bob is aa

 

[root@heguangwu projects]# ./opt_ex2 -b aa

       input para is b,and optarg is aa

原创粉丝点击