• getopt


    #include<stdio.h>
    #include<unistd.h>
    
    int main(int argc,char *argv[]) 
    { 
      int ch; 
      opterr=0; 
    
      while((ch=getopt(argc,argv,"a:b:c:d:e:"))!=-1) 
      { 
        printf("
    
    
    "); 
        printf("optind:%d
    ",optind); 
        printf("optarg:%s
    ",optarg); 
        printf("ch:%c
    ",ch); 
        switch(ch) 
        {   
          case 'a': 
            printf("option a='%s'
    ",optarg); 
            break; 
          case 'b': 
            printf("option b='%s'
    ",optarg); 
            break; 
          case 'c': 
            printf("option c='%s'
    ",optarg); 
            break; 
          case 'd': 
            printf("option d='%s'
    ",optarg); 
            break; 
          case 'e': 
            printf("option e='%s'
    ",optarg); 
            break; 
          default:
    
            printf("other option:%c
    ",ch); 
        } 
        printf("optopt+%c
    ",optopt); 
      } 
    }
    
    // ./a.out -b 2222 -a 1111 -a 3333 -c 4444 -d 5555 -e 6666
    
    
    
    /* optarg和optind是两个最重要的external变量。optarg是指向参数的指针(当然这只针对有参数的选项);
    optind是argv[]数组的索引,众所周知,argv[0]是函数名称,所有参数从argv[1]开始,所以optind被初始化设置指为1。
    每调用一次getopt()函数,返回一个选项,如果该选项有参数,则optarg指向该参数。                  
    在命令行选项参数再也检查不到optstring中包含的选项时,返回-1。
    
    optstring是getopt()中第3个参数,作为选项的字符串的列表。
    函数getopt()认为optstring中,以'-’开头的字符(注意!不是字符串!!)就是命令行参数选项,有的参数选项后面可以跟参数值。
    optstring中的格式规范如下: 
    1) 单个字符,表示选项, 
    2) 单个字符后接一个冒号”:”,表示该选项后必须跟一个参数值。参数紧跟在选项后或者以空格隔开。该参数的指针赋给optarg。 
    3) 单个字符后跟两个冒号”:”,表示该选项后必须跟一个参数。                                                                        
    参数必须紧跟在选项后不能以空格隔开。该参数的指针赋给optarg。
    */
    
    int main1(int argc, char **argv)
    {
        int result;
        opterr = 0;  //使getopt不行stderr输出错误信息
        while( (result = getopt(argc, argv, "ab:c::")) != -1 )
        {
               switch(result)
              {
                   case 'a':
                       printf("option=a, optopt=%c, optarg=%s
    ", optopt, optarg);
                       break;
                  case 'b':   
                       printf("option=b, optopt=%c, optarg=%s
    ", optopt, optarg);
                       break;
                  case 'c':
                       printf("option=c, optopt=%c, optarg=%s
    ", optopt, optarg);
                       break;
                  case '?':
                        printf("result=?, optopt=%c, optarg=%s
    ", optopt, optarg);
                        break;
                  default:
                       printf("default, result=%c
    ",result);
                       break;
               }
            printf("argv[%d]=%s
    ", optind, argv[optind]);
        }
        printf("result=-1, optind=%d
    ", optind);   //看看最后optind的位置
        for(result = optind; result < argc; result++)
             printf("-----argv[%d]=%s
    ", result, argv[result]);
     //看看最后的命令行参数,看顺序是否改变了哈。
        for(result = 1; result < argc; result++)
              printf("
    at the end-----argv[%d]=%s
    ", result, argv[result]);
        return 0;
    }


    #include<stdio.h>
    #include<unistd.h>
    int main(int argc,char **argv)
    {
    int ch;
    opterr = 0;
    while((ch = getopt(argc,argv,"a:b::cde"))!= -1)
    switch(ch)
    {
    case 'a': printf("option a:'%s' ",optarg); break;
    case 'b': printf("option b:'%s' ",optarg); break;
    default: printf("other option :%c ",ch);
    }
    printf("optopt +%c ",optopt);
    }

    
    
    
     
  • 相关阅读:
    2018-2019-1 20165313 20165212 20165222 实验二 固件程序设计
    2018-2019-1 20165313 20165212 2016522 实验一 开发环境的熟悉
    课程总结
    2017-2018-2 20165222 实验五《网络编程与安全》实验报告
    2017-2018-2 20165222实验四《Android程序设计》实验报告
    20165222 第十周课下补做
    20165222 实验三 敏捷开发与XP实践
    各种树
    【面试】MySQL
    Redis
  • 原文地址:https://www.cnblogs.com/timssd/p/4091030.html
Copyright © 2020-2023  润新知