zoukankan      html  css  js  c++  java
  • getopt详解

    (本文翻译自:http://www.gnu.org/s/hello/manual/libc/Getopt.html#Getopt)

    一、使用getopt函数

    下面是getopt函数的使用细节,使用getopt函数必须包含头文件<unistd.h>。

    1、变量:int opterr

    如果opterr非零,当遇到未声明的选项字符或者选项字符后面缺失了参数,则打印错误消息到标准错误流。这是缺省的行为。如果设置opterr为零,则不会打印错误消息,但是会返回一个?号表示一个错误。

    2、变量:int optopt

    当getopt检测到一个未知选项字符或者某个选项没有带参数的时候,optopt将会存储该参数的值,我们可以输出此值作为诊断信息。

    3、变量:int optind 

    这个变量被getopt设置,表示下一个将被处理的argv数组中的元素。一旦getopt找到了所有的选项,则optind表示剩下的非选项参数的起点位置,该变量的初始值为1.

    4、变量:char * optarg

    这个变量表示接受参数的选项的参数。

     Function: int getopt (int argc, char **argv, const char *options)

    options的格式为“ab:c::”,选项字符后面可以跟参数,如果选项字符后面是:,表示该选项需要接受参数,如果是::,则表示参数可选。

    二、getopt解析参数的例子

    下面是一个getopt解析参数的典型例子,关键点如下:

    • 一般情况下,getopt作为一个循环调用,当getopt返回-1,表示没有选项可以检查, 循环中止。
    • switch语句用来区分getopt的返回值。
    • 第二个循环用来打印剩下的非选项参数。
         #include <ctype.h>
         #include <stdio.h>
         #include <stdlib.h>
         #include <unistd.h>
         
         int
         main (int argc, char **argv)
         {
           int aflag = 0;
           int bflag = 0;
           char *cvalue = NULL;
           int index;
           int c;
         
           opterr = 0;
         
           while ((c = getopt (argc, argv, "abc:")) != -1)
             switch (c)
               {
               case 'a':
                 aflag = 1;
                 break;
               case 'b':
                 bflag = 1;
                 break;
               case 'c':
                 cvalue = optarg;
                 break;
               case '?':
                 if (optopt == 'c')
                   fprintf (stderr, "Option -%c requires an argument.\n", optopt);
                 else if (isprint (optopt))
                   fprintf (stderr, "Unknown option `-%c'.\n", optopt);
                 else
                   fprintf (stderr,
                            "Unknown option character `\\x%x'.\n",
                            optopt);
                 return 1;
               default:
                 abort ();
               }
         
           printf ("aflag = %d, bflag = %d, cvalue = %s\n",
                   aflag, bflag, cvalue);
         
           for (index = optind; index < argc; index++)
             printf ("Non-option argument %s\n", argv[index]);
           return 0;
         }
    

    一些例子:

         % testopt
         aflag = 0, bflag = 0, cvalue = (null)
         
         % testopt -a -b
         aflag = 1, bflag = 1, cvalue = (null)
         
         % testopt -ab
         aflag = 1, bflag = 1, cvalue = (null)
         
         % testopt -c foo
         aflag = 0, bflag = 0, cvalue = foo
         
         % testopt -cfoo
         aflag = 0, bflag = 0, cvalue = foo
         
         % testopt arg1
         aflag = 0, bflag = 0, cvalue = (null)
         Non-option argument arg1
         
         % testopt -a arg1
         aflag = 1, bflag = 0, cvalue = (null)
         Non-option argument arg1
         
         % testopt -c foo arg1
         aflag = 0, bflag = 0, cvalue = foo
         Non-option argument arg1
         
         % testopt -a -- -b
         aflag = 1, bflag = 0, cvalue = (null)
         Non-option argument -b
         
         % testopt -a -
         aflag = 1, bflag = 0, cvalue = (null)
         Non-option argument -
  • 相关阅读:
    Rainmeter 雨滴桌面 主题分享
    行人检測之HOG特征(Histograms of Oriented Gradients)
    const和readonly差别
    ADB命令解析
    Java实现 蓝桥杯VIP 算法训练 接水问题
    Java实现 蓝桥杯VIP 算法训练 星际交流
    Java实现 蓝桥杯VIP 算法训练 星际交流
    Java实现 蓝桥杯VIP 算法训练 星际交流
    Java实现 蓝桥杯VIP 算法训练 星际交流
    Java实现 蓝桥杯VIP 算法训练 星际交流
  • 原文地址:https://www.cnblogs.com/bigwalnut/p/2099609.html
Copyright © 2011-2022 走看看