【整理】getopt和getopt_long函数

本文详细介绍了getopt和getopt_long函数的使用方法,包括函数声明、参数解释、全局变量说明、示例代码及运行结果等。getopt适用于处理短选项,getopt_long则扩展了getopt的功能,支持长选项。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

函数声明:

#include <unistd.h>  

int getopt(int argc, char * const argv[],  
           const char *optstring);  

extern char *optarg;  
extern int optind, opterr, optopt;  

#include <getopt.h>  

int getopt_long(int argc, char * const argv[],  
           const char *optstring,  
           const struct option *longopts, int *longindex); 

一、getopt()

argc、argv就是main函数的argc和argv,这两者直接传入即可
optstring是选项字母组成的字串。如果该字串里的任一字符后面有冒号,那么这个选项就要求有选项参数。
例如:

char *optstring = "abcd:";

上面这个optstring在传入之后,getopt函数将依次检查命令行是否指定了 -a, -b, -c及 -d(这需要多次调用getopt函数,直到其返回-1),当检查到上面某一个参数被指定时,函数会返回被指定的参数名称(即该字母)
最后一个参数d后面带有冒号,: 表示参数d是可以指定值的,如 -d 100 或 -d user。

getopt() 第一次调用时返回第一个选项,并设置一些全局变量。使用相同的参数再次调用该函数时,它将返回下一个选项,并设置相应的全局变量。如果不再有可识别的选项,将返回 -1,此任务就完成了。

getopt() 所设置的全局变量包括:

  1. char *optarg——当前选项参数字符串(如果有)。
  2. int optind——argv的当前索引值。当getopt()在while循环中使用时,循环结束后,剩下的字串视为操作数,在argv[optind]至argv[argc-1]中可以找到。
  3. int opterr——这个变量非零时,getopt()函数为“无效选项”和“缺少参数选项,并输出其错误信息。
  4. int optopt——当发现无效选项字符之时,getopt()函数或返回’?’字符,或返回’:’字符,并且optopt包含了所发现的无效选项字符。

示例:

#include <unistd.h>  
#include <stdlib.h>  
#include <stdio.h>  

int main(int argc, char *argv[])  
{  
    int opt;  
    char *optstring = "a:b:c:d";  

    while ((opt = getopt(argc, argv, optstring)) != -1)  
    {  
        printf("opt = %c\n", opt);  
        printf("optarg = %s\n", optarg);  
        printf("optind = %d\n", optind);  
        printf("argv[optind - 1] = %s\n\n",  argv[optind - 1]);  
    }  

    return 0;  
} 

运行结果

$ ./test_getopt -a 100 -b 200 -c admin -d  
opt = a  
optarg = 100  
optind = 3  
argv[optind - 1] = 100  

opt = b  
optarg = 200  
optind = 5  
argv[optind - 1] = 200  

opt = c  
optarg = admin  
optind = 7  
argv[optind - 1] = admin  

opt = d  
optarg = (null)  
optind = 8  
argv[optind - 1] = -d  

错误的调用程序,要么是命令行选项无效,要么是缺少选项参数,正常情况下,getopt()会为这两种情况输出自己的出错信息,并且不区分的均返回’?’。可以采用以下两种方法来更改getopt()函数的出错信息输出行为:

  1. 在调用getopt()之前,将opterr设置为0, getopt()函数发现错误的时候强制不输出任何消息。
  2. 如果optstring参数的第一个字符是冒号,例如”:ngl:”,那么getopt()会保持沉默,并根据错误情况返回不同字符,如下:
    • “无效选项” —— getopt()返回’?’,并且optopt包含了无效选项字符(这是正常的行为)。
    • “缺少选项参数” —— getopt()返回’:’

二、getopt_long()

getopt_long函数包含了getopt函数的功能,并且还可以指定“长参数”(或者说长选项),与getopt函数对比,getopt_long比其多了两个参数:

longopts指向的是一个由option结构体组成的数组,那个数组的每个元素,指明了一个“长参数”(即形如–name的参数)名称和性质:

           struct option {
               const char *name;
               int         has_arg;
               int        *flag;
               int         val;
           };
   name  是参数的名称

   has_arg 指明是否带参数值,其数值可选:
       - no_argument (即 0) 表明这个长参数不带参数(即不带数值,如:--name)
       - required_argument (即 1) 表明这个长参数必须带参数(即必须带数值,如:--name Bob)
       - optional_argument(即2)表明这个长参数后面带的参数是可选的,(即--name和--name Bob均可)

   flag   当这个指针为空的时候,函数直接将val的数值从getopt_long的返回值返回出去,当它非空时,val的值会被赋到flag指向的整型数中,而函数返回值为0

   val    用于指定函数找到该选项时的返回值,或者当flag非空时指定flag指向的数据的值。

另一个参数longindex,如果longindex非空,它指向的变量将记录当前找到参数符合longopts里的第几个元素的描述,即是longopts的下标值。

示例

#include <unistd.h>  
#include <stdlib.h>  
#include <stdio.h>  
#include <getopt.h>  

int  
main(int argc, char **argv)  
{  
   int opt;  
   int digit_optind = 0;  
   int option_index = 0;  
   char *optstring = "a:b:c:d";  
   static struct option long_options[] = {  
       {"reqarg", required_argument, NULL, 'r'},  
       {"noarg",  no_argument,       NULL, 'n'},  
       {"optarg", optional_argument, NULL, 'o'},  
       {0, 0, 0, 0}  
   };  

   while ( (opt = getopt_long(argc, argv, optstring, long_options, &option_index)) != -1)  
   {  
        printf("opt = %c\n", opt);  
        printf("optarg = %s\n", optarg);  
        printf("optind = %d\n", optind);  
        printf("argv[optind - 1] = %s\n",  argv[optind - 1]);  
        printf("option_index = %d\n", option_index);  
   }  

   return 0;  
}  

运行结果

$ ./test_getopt_long -a 100 --reqarg 100 --nonarg  
opt = a  
optarg = 100  
optind = 3  
argv[optind - 1] = 100  
option_index = 0  

opt = r  
optarg = 100  
optind = 5  
argv[optind - 1] = 100  
option_index = 0  

./test_getopt_long: unrecognized option '--nonarg'  
opt = ?  
optarg = (null)  
optind = 6  
argv[optind - 1] = --nonarg  
option_index = 0  

当所给的参数存在问题时,opt(即函数返回值是’?’),如:

$ ./test_getopt_long -a  
./test_getopt_long: option requires an argument -- 'a'  
opt = ?  
optarg = (null)  
optind = 2  
argv[optind - 1] = -a  
option_index = 0  

$ ./test_getopt_long --reqarg  
./test_getopt_long: option '--reqarg' requires an argument  
opt = ?  
optarg = (null)  
optind = 2  
argv[optind - 1] = --reqarg  

【参考:
http://blog.youkuaiyun.com/cashey1991/article/details/7942809
http://blog.youkuaiyun.com/mr_jj_lian/article/details/6835137

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值