zoukankan      html  css  js  c++  java
  • getopt()——命令行参数分析

    getopt(分析命令行参数)  
     
    相关函数表头文件
      #include<unistd.h>
      extern char *optarg; //选项的参数指针
      extern int optind, //下一次调用getopt的时,从optind存储的位置处重新开始检查选项。 
      extern int opterr, //当opterr=0时,getopt不向stderr输出错误信息。
      extern int optopt; //当命令行选项字符不包括在optstring中或者选项缺少必要的参数时,该选项存储在optopt 中,getopt返回'?’、
    定义函数
            int getopt(int argc,char * const argv[ ],const char * optstring);
    函数说明
            getopt()用来分析命令行参数。参数argc和argv是由main()传递的参数个数和内容。参数optstring 则代表欲处理的选项字符串。此函数会返回在argv 中下一个的选项字母,此字母会对应参数optstring 中的字母。如果选项字符串里的字母后接着冒号“:”,则表示还有相关的参数,全域变量optarg 即会指向此额外参数。如果getopt()找不到符合的参数则会印出错信息,并将全域变量optopt设为“?”字符,如果不希望getopt()印出错信息,则只要将全域变量opterr设为0即可。
     
      调用一次,返回一个选项。 在命令行选项参数再也检查不到optstring中包含的选项时,返回-1,同时optind储存第一个不包含选项的命令行参数。
    首先说一下什么是选项,什么是参数。
    1.单个字符,表示选项,
    2.单个字符后接一个冒号:表示该选项后必须跟一个参数。参数紧跟在选项后或者以空格隔开。该参数的指针赋给optarg。
    3.单个字符后跟两个冒号,表示该选项后必须跟一个参数。参数必须紧跟在选项后不能以空格隔开。该参数的指针赋给optarg。(这个特性是GNU的扩张)。
     
    短参数的定义
           getopt()使用optstring所指的字串作为短参数列表,象"1ac:d::"就是一个短参数列表。短参数的定义是一个'-'后面跟一个字母或数字,象-a, -b就是一个短参数。每个数字或字母定义一个参数。 
      其中短参数在getopt定义里分为三种:
      1. 不带值的参数,它的定义即是参数本身。
      2. 必须带值的参数,它的定义是在参数本身后面再加一个冒号。
      3. 可选值的参数,它的定义是在参数本身后面加两个冒号 。
      在这里拿上面的"1ac:d::"作为样例进行说明,其中的1,a就是不带值的参数,c是必须带值的参数,d是可选值的参数。
      在实际调用中,'-1 -a -c cvalue -d', '-1 -a -c cvalue -ddvalue', '-1a -ddvalue -c cvalue'都是合法的。这里需要注意三点:
      1. 不带值的参数可以连写,象1和a是不带值的参数,它们可以-1 -a分开写,也可以-1a或-a1连写。
      2. 参数不分先后顺序,'-1a -c cvalue -ddvalue'和'-d -c cvalue -a1'的解析结果是一样的。
      3. 要注意可选值的参数的值与参数之间不能有空格,必须写成-ddvalue这样的格式,如果写成-d dvalue这样的格式就会解析错误。

    返回值
       getopt()每次调用会逐次返回命令行传入的参数。
       当没有参数的最后的一次调用时,getopt()将返回-1。
        当解析到一个不在optstring里面的参数,或者一个必选值参数不带值时,返回'?',此时optopt的值为那个错误的参数。
       当optstring是以':'开头时,缺值参数的情况下会返回':',而不是'?' 。
     
    举例

    例如gcc -g -o test test.c ,其中g和o表示选项,test为选项o的参数。

    上面是getopt()函数的基本含义,大家懂得了这些之后,我们一个例子加深一下理解。
    例如我们这样调用getopt(argc, argv, "ab:c:de::");
    从上面我们可以知道,选项a,d没有参数,选项b,c有一个参数,选项e有有一个参数且必须紧跟在选项后不能以空格隔开。getopt首先扫描argv[1]到argv[argc-1],并将选项及参数依次放到argv数组的最左边,非选项参数依次放到argv的最后边。
    执行程序为:
           0     1    2  3  4    5    6    7   8   9
    $ ./test file1 -a -b -c code -d file2 -e file3
    扫描过程中,optind是下一个选项的索引, 非选项参数将跳过,同时optind增1。optind初始值为1。当扫描argv[1]时,为非选项参数,跳过,optind=2;扫描到-a选项时, 下一个将要扫描的选项是-b,则optind更改为3;扫描到-b选项时,后面有参数(会认为-c为选项b的参数),optind=5,扫描到code非选项跳过optind=6;扫描到-d选项,后面没有参数,optind=7;扫描到file2非选项跳过optind=8;扫描到-e后面本来应该有参数,optind=9但是有空格所以e的参数为空。

    扫描结束后,getopt会将argv数组修改成下面的形式
           0    1   2  3  4  5    6     7     8    9
    $ ./test -a -b -c -d -e file1 code file2 file3

    同时,optind会指向非选项的第一个参数,如上面,optind将指向file1

     1 #include <unistd.h>  
     2 #include <stdio.h>
     3   
     4 int main(int argc, char * argv[])  
     5 {  
     6     int aflag=0, bflag=0, cflag=0;  
     7     int ch; 
     8     
     9     printf("optind:%d,opterr:%d
    ",optind,opterr);  
    10     printf("--------------------------
    "); 
    11     
    12     while ((ch = getopt(argc, argv, "ab:c:de::")) != -1)  
    13     {  
    14         printf("optind: %d,argc:%d,argv[%d]:%s
    ", optind,argc,optind,argv[optind]);  
    15         
    16         switch (ch) 
    17         {  
    18             case 'a':  
    19                 printf("HAVE option: -a
    
    ");  
    20                 break; 
    21                 
    22             case 'b':  
    23                 printf("HAVE option: -b
    ");  
    24                 printf("The argument of -b is %s
    
    ", optarg);  
    25                 break; 
    26                 
    27             case 'c':  
    28                 printf("HAVE option: -c
    ");  
    29                 printf("The argument of -c is %s
    
    ", optarg);  
    30                 break; 
    31             
    32             case 'd':  
    33                 printf("HAVE option: -d
    ");  
    34                 break; 
    35                 
    36             case 'e':  
    37                 printf("HAVE option: -e
    ");  
    38                 printf("The argument of -e is %s
    
    ", optarg);  
    39                 break;  
    40                 
    41             case '?':  
    42                 printf("Unknown option: %c
    ",(char)optopt);  
    43                 break;  
    44                 
    45         }  
    46     }  
    47     printf("----------------------------
    ");  
    48     printf("optind=%d,argv[%d]=%s
    ",optind,optind,argv[optind]);  
    49 }
     运行结果:
     1 :$ ./getopt file1 -a -b -c code -d file2 -e file3
     2 optind:1,opterr:1
     3 --------------------------
     4 optind: 3,argc:10,argv[3]:-b
     5 HAVE option: -a
     6 
     7 optind: 5,argc:10,argv[5]:code
     8 HAVE option: -b
     9 The argument of -b is -c
    10 
    11 optind: 7,argc:10,argv[7]:file2
    12 HAVE option: -d
    13 
    14 optind: 9,argc:10,argv[9]:file3
    15 HAVE option: -e
    16 The argument of -e is (null)   
    17 
    18 ----------------------------
    19 optind=6,argv[6]=file1
  • 相关阅读:
    Vue如何下载文件?
    vue用template还是JSX?
    2020年的第一件事,我取关了97个公众号
    月经贴 | 2019.12
    CSS——盒子模型(含详解)
    CSS——字体、文本、背景属性设置
    CSS——选择器(三大特性)
    CSS——选择器(本篇介绍八类多种)
    CSS——简介
    WEB前端——body内常用标签(form标签)
  • 原文地址:https://www.cnblogs.com/cmembd/p/3585874.html
Copyright © 2011-2022 走看看