简单命令行处理函数:getopt() ---转

函数getopt()用来分析命令行参数(可怜的Sam啊……以前还傻愣愣的自己写命令行参数解析函数…I’m too simple…sometimes naive.),其函数原型和相关变量声明如下:

        #include <unistd.h>
        extern char *optarg;
        extern int optind,  // 初始化值为1,下一次调用getopt时,从optind存储的位置重新开始检查选项。
        extern int opterr,  // 初始化值为1,当opterr=0时,getopt不向stderr输出错误信息。
        extern int optopt;  // 当命令行选项字符不包括在optstring中或者选项缺少必要的参数时,
                                    // 该选项存储在optopt中, getopt返回'?’。
        int getopt(int argc, char * const argv[], const char *optstring);        optarg和optind是两个最重要的external 变量。optarg是指向参数的指针(当然这只针对有参数的选项);optind是argv[]数组的索引,众所周知,argv[0]是函数名称,所有参数从argv[1]开始,所以optind被初始化设置指为1。        每调用一次getopt()函数,返回一个选项,如果该选项有参数,则optarg指向该参数。 在命令行选项参数再也检查不到optstring中包含的选项时,返回-1。

        函数getopt()有三个参数,argc和argv[]应该不需要多说,下面说一下字符串optstring,它是作为选项的字符串的列表。
        函数getopt()认为optstring中,以'-’开头的字符(注意!不是字符串!!)就是命令行参数选项,有的参数选项后面可以跟参数值。optstring中的格式规范如下:
1) 单个字符,表示选项,
2) 单个字符后接一个冒号”:”,表示该选项后必须跟一个参数值。参数紧跟在选项后或者以空格隔开。该参数的指针赋给optarg。
3) 单个字符后跟两个冒号”::”,表示该选项后必须跟一个参数。参数必须紧跟在选项后不能以空格隔开。该参数的指针赋给optarg。(这个特性是GNU的扩张)。
        例如optstring="ab:c::d::",程序名称为test.ext,在命令行下运行该程序:


        test.exe -a -b host -ckeke -d haha
        在这个命令行参数中,-a和-h就是选项元素,去掉'-',a,b,c就是选项。host是b的参数,keke是c的参数。但haha并不是d的参数,因为它们中间有空格隔开。所以上面的命令行调用会出错。
        默认情况下getopt会重新排列命令行参数的顺序,所以到最后所有不包含选项的命令行参数都排到最后。例如:


        test.exe -a ima -b host -ckeke -d haha        最后命令行参数的顺序是: -a -b host -ckeke -d ima haha
        如果optstring中的字符串以'+'加号开头或者环境变量POSIXLY_CORRE被设置。那么一遇到不包含选项的命令行参数,getopt就会停止,返回-1。
    对getopt()函数的使用,通常用一个循环,不断的调用它,获得其参数选项以及参数值(如果有的话),直到取完最后一个命令行参数(getopt()函数返回值为-1)。并且,为了防止用户不按照要求进行命令行输入,会设计一个help选项,以告知用户如何使用命令行运行该程序)。

  
范例  #include<stdio.h>
#include<unistd.h>
int main(int argc,char **argv)
{
int ch;
opterr = 0;
while((ch = getopt(argc,argv,”a:bcde”))!= -1)
switch(ch)
{
case ‘a’:
printf(“option a:’%s’/n”,optarg);
break;
case ‘b’:
printf(“option b :b/n”);
break;
default:
printf(“other option :%c/n”,ch);
}
printf(“optopt +%c/n”,optopt);
}
 
执行  $./getopt –b
option b:b
$./getopt –c
other option:c
$./getopt –a
other option :?
$./getopt –a12345
option a:’12345’

 
#include<string.h>
#include<stdio.h>
#include<unistd.h>
static int opt_a=0;
static int opt_b=0;
static int opt_c=0;
static int opt_d=0;
static int opt_e=0;
static char * opt_a_arg=NULL;
static void usage()
{
        fprintf(stderr,"Usage:getopt [a arg] [b] [c] [d] [e]/n");
        exit(1);
}

int main(int argc,char **argv)
{
         int opt;
         char opts[]="a:bcde";
         opterr = 0;
         while((opt = getopt(argc,argv,opts)) != -1){
                 switch(opt)
                 {
                         case 'a':
                                 opt_a=1;
                                 opt_a_arg=strdup(optarg);
                                 break;
                         case 'b':
                                 opt_b=1;
                                 break;
                         case 'c':
                                 opt_c=1;
                                 break;
                         case 'd':
                                 opt_d=1;
                                 break;
                         case 'e':
                                 opt_e=1;
                                 break;
                         case '?':
                                 usage();
                                 break;
                 }
         }
         if(opt_a || opt_b || opt_c|| opt_d || opt_e)
         {
                 if(opt_a)
                         printf("opt a is set and arg is %s /n",opt_a_arg);
                 if(opt_b)
                         printf("opt b is set/n");
                 if(opt_c)
                         printf("opt c is set /n");
                 if(opt_d)
                         printf("opt d is set /n");
                 if(opt_e)
                         printf("opt e is set /n");
         }else
                 usage();
}
 
/* Glib C 的getopt源代码文件中 自带的测试的代码 */

#ifdef TEST
/* Compile with -DTEST to make an executable for use in testing
    the above definition of `getopt'.   */
int
main (int argc, char **argv)
{
   int c;
   int digit_optind = 0;
   while (1)
     {
       int this_option_optind = optind ? optind : 1;
       c = getopt (argc, argv, "abc:d:0123456789");
       if (c == -1)
         break;
       switch (c)
         {
         case '0':
         case '1':
         case '2':
         case '3':
         case '4':
         case '5':
         case '6':
         case '7':
         case '8':
         case '9':
           if (digit_optind != 0 && digit_optind != this_option_optind)
             printf ("digits occur in two different argv-elements./n");
           digit_optind = this_option_optind;
           printf ("option %c/n", c);
           break;
         case 'a':
           printf ("option a/n");
           break;
         case 'b':
           printf ("option b/n");
           break;
         case 'c':
           printf ("option c with value `%s'/n", optarg);
           break;
         case '?':
           break;
         default:
           printf ("?? getopt returned character code 0%o ??/n", c);
         }
     }
   if (optind < argc)
     {
       printf ("non-option ARGV-elements: ");
       while (optind < argc)
         printf ("%s ", argv[optind++]);
       printf ("/n");
     }
   exit (0);
}
#endif /* TEST */
参考

文章出处:http://www.diybl.com/course/3_program/c++/cppjs/2007930/75180.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值