首页 > 其他 > 详细

getopt函数的用法

时间:2015-03-19 08:54:31      阅读:298      评论:0      收藏:0      [点我收藏+]

Linux提供了一个解析命令行参数的函数。

#include <unistd.h>

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

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

使用这个函数,我们可以这样运行命令

./a.out -n -t 100

n后面不需要参数,t需要一个数值作为参数。

代码如下:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#define ERR_EXIT(m)     do {         perror(m);        exit(EXIT_FAILURE);    }while(0)


int main(int argc, char *argv[])
{
    int opt;
    while(1)
    {
        opt = getopt(argc, argv, "nt:");
        if(opt == ?)
            exit(EXIT_FAILURE);
        if(opt == -1)
            break;

        switch(opt)
        {
            case n:
                printf("AAAAAAAA\n");
                break;
            case t:
                printf("BBBBBBBB\n");
                int n = atoi(optarg);
                printf("n = %d\n", n);
        }
    }
    return 0;
}

当输入非法参数时,getopt返回’?’,当解析完毕时,返回-1.

如果需要参数,那么使用optarg获取,这是一个全局变量。

注意getopt的第三个参数”nt:”,说明可用的参数有n和t,t后面有一个冒号,说明t需要额外的参数。

运行结果如下:

5:30:22 wing@ubuntu msg ./getopt_test -n
AAAAAAAA
5:30:26 wing@ubuntu msg ./getopt_test -t 
./getopt_test: option requires an argument -- t
5:30:31 wing@ubuntu msg ./getopt_test -t 100                                           1 ?
BBBBBBBB
n = 100

getopt函数的用法

原文:http://my.oschina.net/inevermore/blog/388714

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!