zoukankan      html  css  js  c++  java
  • [c language] getopt 其参数optind 及其main(int argc, char **argv) 参数解释

    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返回'?’

     1 #include <stdio.h>  
     2 #include <unistd.h>  
     3 #include <stdlib.h>
     4 
     5 #ifdef RDP2
     6 #define VNCOPT "V:Q"
     7 #else
     8 #define VNCOPT "V:"
     9 #endif
    10  
    11 int main(int argc, char **argv)  
    12 {  
    13         int ch, i;  
    14         opterr = 0;
    15 
    16         while ((ch = getopt(argc,argv, VNCOPT ":a:bcde::f"))!=-1)  
    17         {  
    18             printf("optind:%d,opterr:%d
    ", optind, opterr);  
    19                 switch(ch)  
    20                 {         
    21                         case 'V':
    22                                 printf("option V:'%s'
    ",optarg);
    23                                 break; 
    24                         case 'a': 
    25                                 i = strtol(optarg, NULL, 16);
    26                                 printf("i = %x
    ", i); 
    27                                 printf("option a:'%s'
    ",optarg);  
    28                                 break;  
    29                         case 'b':  
    30                                 printf("option b :b
    ");  
    31                                 break; 
    32                         case 'e':
    33                                 printf("option e:'%s'
    ",optarg);
    34                                 break; 
    35                         default:  
    36                                 printf("other option :%c
    ",ch);  
    37                 }  
    38         }
    39         for(int i = 0; i < argc; i++)
    40             printf("argv[%d] = [%s]
    ", i, argv[i]);
    41         printf("argc = [%d]
    ", argc); 
    42         printf("optopt +%c
    ",optopt);  
    43 
    44     return 1;
    45 }

     执行结果:

    $ ./getopt -a 123 -bcd -e 456
    i = 123
    option a:'123'
    optind:3,opterr:0
    option b :b
    optind:3,opterr:0
    other option :c
    optind:3,opterr:0
    other option :d
    optind:4,opterr:0
    option e:'456'
    optind:6,opterr:0
    argv[0] = [./getopt]
    argv[1] = [-a]
    argv[2] = [123]
    argv[3] = [-bcd]
    argv[4] = [-e]
    argv[5] = [456]
    argc = [6]
    optopt +e
    optind = 6

     argv 数组存储命令行字符串(包含./getopt执行程序)

     argc 命令行字符串个数(./getopt -a 123 -bcd -e 456) 6个

     optind 下一次调用getopt的时,从optind存储的位置处重新开始检查选项。从0开始计算

     (对应下面位置)

     ./getopt -a 123 -bcd -e 456

          0       1   2     3     4   5   6

  • 相关阅读:
    内网Windows Server时间自动同步
    处理sqlserver数据
    virtualenv使用
    vue过渡动画效果
    vue视图
    vue组件
    Vue实例
    vue介绍
    Bootstrap布局
    Bootstrap组件
  • 原文地址:https://www.cnblogs.com/galoishelley/p/3820278.html
Copyright © 2011-2022 走看看