Linux 命令行参数处理函数探究

600阅读 0评论2015-07-02 等待的骑士
分类:LINUX

一、getopt函数



A.函数功能:处理命令行参数

B.参数:

argc代表命令行参数的个数,argv[i]存放命令行参数在内存存放的地址。这两个参数直接从mian(int argc,char *argv[])得到。

如果argv的元素是以 "-“开头称为一个选项元素,紧跟在"-"后面的一个字符称为选项字符。

eg:ls  -l

其中ls是可执行程序名称,-l称为选项元素,'l'称为选项字符

optstring:选项参数的集合

注意:如果getopt()函数被循环调用,每次调用都会返回一个对应选元素的选项字符,如果所有选项字符都放回后,再次调用getopt()函数返回-1。

./a.out   -a  -n  -l

while(1)
{
    ch = getopt(argc,argv,"anl");
}

第一次调用ch : 'a'
第二次调用ch : 'n'
第三次调用ch : 'l'

案例一、



运行结果:



C.变量optind

每次getopt函数调用的时候,optind总是当前处理argv[index]的下一个被处理选项元素的索引即optind = index + 1,如:当前处理的选项元素argv[1],则optind的值为2,即argv[2]的索引。

注意:其值默认初始化为1,如果。

案例二、

  1. #include <stdio.h>
  2. #include <unistd.h>

  3. int main(int argc,char *argv[])
  4. {
  5.     int ch;

  6.     while((ch = getopt(argc,argv,"cyg")) != -1)
  7.     {
  8.         switch(ch)
  9.         {
  10.         case 'c':
  11.             printf("optind = %d\n",optind);
  12.             printf("Option character \'c\'.\n");
  13.             break;

  14.         case 'y':
  15.             printf("optind = %d\n",optind);
  16.             printf("Option character \'y\'.\n");
  17.             break;

  18.         case 'g':
  19.             printf("optind = %d\n",optind);
  20.             printf("Option character \'g\'.\n");
  21.             break;
  22.         }
  23.     }

  24.     printf("Ending...\n");
  25.     printf("optind = %d\n",optind);

  26.     return 0;
  27. }
运行结果:



D.变量optarg

如果optstring选项字符后面紧跟一个":",则要求选项元素后面跟一个参数;
如果optstring选项字符后面紧跟两个"::",则选项元素后面可以跟一个可选的参数

案例三:

  1. #include <stdio.h>
  2. #include <unistd.h>

  3. int main(int argc,char *argv[])
  4. {
  5.     int ch;

  6.     while((ch = getopt(argc,argv,"c:yg::")) != -1)
  7.     {
  8.         switch(ch)
  9.         {
  10.         case 'c':
  11.             printf("optind = %d\n",optind);
  12.             printf("optarg = %s\n",optarg);
  13.             printf("Option character \'c\'.\n");
  14.             break;

  15.         case 'y':
  16.             printf("optind = %d\n",optind);
  17.             printf("Option character \'y\'.\n");
  18.             break;

  19.         case 'g':
  20.             printf("optind = %d\n",optind);
  21.             printf("optarg = %s\n",optarg);
  22.             printf("Option character \'g\'.\n");
  23.             break;
  24.         }
  25.     }

  26.     printf("Ending...\n");
  27.     printf("argv[%d] = %s\n",optind,argv[optind]);

  28.     return 0;
  29. }


注意:上面的最后一次运行结果,"xxx"在一开始在argv的的索引值为3,但是我们发现它实际的索引值是4。这是因为getopt函数,会把不拥有选项参数的选项字符后面的操作数,放在最后。

E.变量opterr和变量optopt

getopt()函数在搜索argv时,如果发现argv拥有optstring所没有的选项字符,则会提示立即返回'?',并提示错误信息,所没有的选项字符会放在opt里。如果不想系统提示默认的错误信息,将opterr设为0即可。

案例四:

  1. #include <stdio.h>
  2. #include <unistd.h>

  3. int main(int argc,char *argv[])
  4. {
  5.     int ch;

  6.     while((ch = getopt(argc,argv,"c:yg::")) != -1)
  7.     {
  8.         switch(ch)
  9.         {
  10.         case 'c':
  11.             printf("optind = %d\n",optind);
  12.             printf("optarg = %s\n",optarg);
  13.             printf("Option character \'c\'.\n");
  14.             break;

  15.         case 'y':
  16.             printf("optind = %d\n",optind);
  17.             printf("Option character \'y\'.\n");
  18.             break;

  19.         case 'g':
  20.             printf("optind = %d\n",optind);
  21.             printf("optarg = %s\n",optarg);
  22.             printf("Option character \'g\'.\n");
  23.             break;
  24.         case '?':
  25.             printf("Invalid Option character \'%c\'.\n",optopt);
  26.             break;
  27.         }
  28.     }

  29.     printf("Ending...\n");
  30.     printf("argv[%d] = %s\n",optind,argv[optind]);

  31.     return 0;
  32. }
运行结果:



注意:如果optstring字符串一开始就是":",则后面要求有选项参数的选项字符,在argv中没有选项参数时,此时调用getopt函数返回":",optopt存放的是没有跟选项参数的选项字符。

案例五:

  1. #include <stdio.h>
  2. #include <unistd.h>

  3. int main(int argc,char *argv[])
  4. {
  5.     int ch;

  6.     while((ch = getopt(argc,argv,":c:yg::")) != -1)
  7.     {
  8.         switch(ch)
  9.         {
  10.         case 'c':
  11.             printf("optind = %d\n",optind);
  12.             printf("optarg = %s\n",optarg);
  13.             printf("Option character \'c\'.\n");
  14.             break;

  15.         case 'y':
  16.             printf("optind = %d\n",optind);
  17.             printf("Option character \'y\'.\n");
  18.             break;

  19.         case 'g':
  20.             printf("optarg = %s\n",optarg);
  21.             printf("optind = %d\n",optind);
  22.             printf("Option character \'g\'.\n");
  23.             break;

  24.         case ':':
  25.             printf("Option character \'%c\',forget argument.\n",optopt);
  26.             break;

  27.         case '?':
  28.             printf("optind = %d\n",optind);
  29.             printf("Invalid Option character \'%c\'.\n",optopt);
  30.             break;
  31.         }
  32.     }

  33.     printf("Ending...\n");
  34.     printf("argv[%d] = %s\n",optind,argv[optind]);

  35.     return 0;
  36. }
运行结果:



二、getopt_long函数

 getopt_long函数也是处理命令行参数,我们会发现它多了一个long,这个long的意思是它支持长选项。
前面我们讨论都是含有一个"-"的选项,常我们会看到"--help"这样的造型,其实这就是命令行参数中的长选项。一般短选项是选项名的缩写,长选项是选项名的全名。



这个函数前三个参数和getopt一样,不解释了。我们来看看后面两个参数。
getopt_long的长选项是一个结构体,定义如下:




参数longindex不为空时,存放的是getopt_long处理当前长选项的索引值。

案例六:

  1. #include <stdio.h>
  2. #include <unistd.h>
  3. #include <getopt.h>

  4. int main(int argc,char *argv[])
  5. {
  6.     int flag_value = 100;

  7.     while(1)
  8.     {
  9.         int option_index = 0;
  10.         int rvalue = 0;

  11.         static struct option long_option[] = {
  12.             {"help0",no_argument,0,0},
  13.             {"help1",required_argument,0,0},
  14.             {"help2",optional_argument,0,0},
  15.             {"help3",no_argument,0,10},
  16.             {0,0,0,0},
  17.         };
  18.         
  19.         long_option[3].flag = &flag_value;
  20.         rvalue = getopt_long(argc,argv,"a:bc::",long_option,&option_index);
  21.         
  22.         if(rvalue == -1)
  23.         {
  24.             printf("No more argument.\n");
  25.             return -1;
  26.         }

  27.         switch(option_index)
  28.         {
  29.         case 0:
  30.             printf("Long option is : %s\n",long_option[option_index].name);
  31.             break;

  32.         case 1:
  33.             printf("Long option is : %s ",long_option[option_index].name);
  34.             if(optarg)
  35.             {
  36.                 printf("with parm '%s'",optarg);
  37.             }
  38.             printf("\n");
  39.             break;

  40.         case 2:
  41.             printf("Long option is : %s ",long_option[option_index].name);
  42.             if(optarg)
  43.             {
  44.                 printf("with parm '%s'",optarg);
  45.             }
  46.             printf("\n");
  47.             break;

  48.         case 3:
  49.             printf("Long option is : %s\n",long_option[option_index].name);
  50.             break;
  51.         }

  52.         printf("flag_value = %d\n",flag_value);
  53.     }

  54.     return 0;
  55. }
运行结果:



三、getopt_long_only函数




案例:

  1. #include <stdio.h>
  2. #include <unistd.h>
  3. #include <getopt.h>

  4. int main(int argc,char *argv[])
  5. {
  6.     int flag_value = 100;

  7.     while(1)
  8.     {
  9.         int option_index = 0;
  10.         int rvalue = 0;

  11.         static struct option long_option[] = {
  12.             {"help0",no_argument,0,0},
  13.             {"help1",required_argument,0,0},
  14.             {"help2",optional_argument,0,0},
  15.             {"help3",no_argument,0,10},
  16.             {0,0,0,0},
  17.         };
  18.         
  19.         long_option[3].flag = &flag_value;
  20.         rvalue = getopt_long_only(argc,argv,"a:bc::",long_option,&option_index);
  21.         
  22.         if(rvalue == -1)
  23.         {
  24.             printf("No more argument.\n");
  25.             return -1;
  26.         }

  27.         switch(option_index)
  28.         {
  29.         case 0:
  30.             printf("Long option is : %s\n",long_option[option_index].name);
  31.             break;

  32.         case 1:
  33.             printf("Long option is : %s ",long_option[option_index].name);
  34.             if(optarg)
  35.             {
  36.                 printf("with parm '%s'",optarg);
  37.             }
  38.             printf("\n");
  39.             break;

  40.         case 2:
  41.             printf("Long option is : %s ",long_option[option_index].name);
  42.             if(optarg)
  43.             {
  44.                 printf("with parm '%s'",optarg);
  45.             }
  46.             printf("\n");
  47.             break;

  48.         case 3:
  49.             printf("Long option is : %s\n",long_option[option_index].name);
  50.             break;
  51.         }

  52.         printf("flag_value = %d\n",flag_value);
  53.     }

  54.     return 0;
  55. }
运行结果:



注意:getopt_long和getopt_long_only的返回值



上一篇:Linux应用编程一些简单常用函数
下一篇:文件I/O相关函数