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 -

转载于:https://www.cnblogs.com/bigwalnut/archive/2011/07/07/2099609.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值