码迷,mamicode.com
首页 > 其他好文 > 详细

getopt,getoptlong学习

时间:2016-08-20 23:10:45      阅读:196      评论:0      收藏:0      [点我收藏+]

标签:

     getopt和getoptlong被用来解析命令行参数。
 
一、getopt
#include <unistd.h>
extern char *optarg;  
extern int optind,   
extern int opterr,  
extern int optopt;  
int getopt(int argc, char * const argv[], const char *optstring);

     定义了四个全局变量:optarg是选项的参数指针,optind记录目前查找的位置,当opterr = 0时,getopt不向stderr输出错误信息。当命令选项字符不包括在optstring中或者缺少必要的参数时,该选项存储在optopt中,getopt返回 ‘?‘ 

     getopt调用一次,返回一次选项,当检查不到参数时,返回-1,同时,optind储存第一个不包含选项的命令行参数。
     optstring参数可以有以下元素。
     1.单个字符:表示选项
     2.单个字符后接一个冒号:表示该选项必须跟一个参数,参数紧跟在选项后,或以一个空格隔开,将参数的指针赋给optarg。
     3.单个字符后跟两个冒号:表示该选项必须跟一个参数,且不能用空格隔开。
例子:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
 
int main(int argc, char **argv)
{
    int opt = 0;
 
    while ((opt = getopt(argc, argv, "if:?lr::")) != -1)
    {
        switch(opt)
        {
            case i:
            case l:
                printf("option: %c\n", opt);
                break;
            case f:
                printf("filename: %s\n", optarg);
                break;
            case r:
                printf("arg:%s\n", optarg);
                break;
            case ::
                printf("option needs a value\n");
                break;
            case ?:
                printf("unknow option: %c\n", optopt);
                break;
        }
    }
 
 
    for (; optind < argc; optind++)
    {
        printf("argument: %s\n", argv[optind]);
 
    ar
 
    return 0;
}
     二、getoptlong
     getoptlong就是添加了对长选项支持的getopt,getoptlong比getopt多两个参数,struct option *longopts和int *longindex,后者用到的少,一般置为NULL。直接来看struct option 结构
struct option
{
     char *name;
     int has_arg;
     int *flag;
     int val;
};
     name表示的是长参数名,has_arg有三个值,当has_arg = no_argument(即0)时,表示该参数后面不跟参数值,当has_arg = required_argument(即1)时,表示参数后面一定跟参数值,当has_arg = optional_argument(即2)时,表示该参数后面可跟参数值,也可不跟。flag   当这个指针为空的时候,函数直接将val的数值从getopt_long的返回值返回出去,当它非空时,val的值会被赋到flag指向的整型数中,而函数返回值为0 。 val    用于指定函数找到该选项时的返回值,或者当flag非空时指定flag指向的数据的值。
定义option試例:
struct option longopts[] =
{
    {"initialize", 0, NULL, i},
    {"filename", 1, NULL, f},
    {"list", 0, NULL, l},
    {"restart", 0, NULL, r}
};
例子:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
 
#define _GNU_SOURCE
#include <getopt.h>
 
int main(int argc, char **argv)
{
    int opt;
 
    struct option longopts[] =
    {
        {"initialize", 0, NULL, i},
        {"filename", 1, NULL, f},
        {"list", 0, NULL, l},
        {"restart", 0, NULL, r}
    };
 
    while ((opt = getopt_long(argc, argv, ":if:lr", longopts, NULL)) != -1)
    {
        switch(opt)
        {
            case i:
            case l:
            case r:
                printf("option: %c\n", opt);
                break;
            case f:
                printf("filename: %s\n", optarg);
                break;
            case ::
                printf("option needs a value\n");
                break;
            case ?:
                printf("unknow option: %c\n", optopt);
                break;
        }
    }
 
    for (; optind < argc; optind++)
    {
        printf("argument: %s\n", argv[optind]);
 
    }
 
    return 0;
}

getopt,getoptlong学习

标签:

原文地址:http://www.cnblogs.com/shenlinken/p/5791428.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!