getopt_long, getopt_long_only -- 命令行解析函数,支持长选项解析
 
【说明】getopt_long/getopt_long_only是getopt的泛集,getopt是getopt_long的一个子集,getopt支持的所有特性,getopt_long都支持,包括错误打印、argv元素顺序调整等;getopt_long相比getopt增加了长选项的解析,具体如下:
 
1、形如:cmd [--create][--file] //对长选项的解析;
2、形如:cmd [--create a_argument][-file b_argument] //对长选项及长选项的参数解析;
3、形如:cmd [--create [a_argument]] //选项create的参数也是可选的情况解析
getopt_long_only与getopt_long的区别在于:getopt_long仅仅只能将"--"开始的选项视为长选项,但getopt_long_only将"-"开头选项也会视为长选项,当长选项列表均不满足时,且短选项满足时,"-"才会解析为短选项;
 
原型:

#define _GNU_SOURCE

#include <getopt.h>

extern char *optarg;

extern int optind, opterr, optopt;

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

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

描述:

 
1、注意相比getopt,使用getopt_long需要加头文件<getopt.h>;
2、getopt_long除了会接受长选项,其他概念和getopt是一样的;
3、如果使用getopt_long想只接受短选项,设置longopts为NULL即可;如果只想接受长选项,相应地设置optstring为NULL即可;
4、长选项名是可以使用缩写方式,比如:选项有--file\--create,那么输入--c/--cr/--cre等均会被正确识别为create选项;
5、对于带参数的长选项格式是:--arg=param或--arg param;
6、longopts是指向struct option数组的第一个元素的指针,struct option定义在<getopt.h>中;
7、longindex如果非NULL,则是返回识别到struct option数组中元素的位置指针;
 
struct option的说明:
 
/*
name: 长选项名
has_arg: 是否带参数或可选参数,这个值在getopt.h中有宏定义,如下:
     # define no_argument        0
     # define required_argument  1
     # define optional_argument  2
flag: 确定函数返回值的情况,如果flag==NULL,则识别选项后返回val(常用的如:设置val为长命令的短命令字符);否则,识别后getopt_long返回0,flag指向一个设置到val的变量;
val: 设置为返回值,或者是flag指向的变量;这里要注意不要写-1到val,否则其作用是getopt_long返回-1,然后停止解析选项;
 
[注意] longopts的最后一个元素必须是全0填充,否则会报段错误
*/
 
struct option {
    const char *name; 
    int has_arg;  
    int *flag;
    int val;
};
 
返回值:
 
1、如果识别短选项,同getopt一样返回短选项字符;
2、如果识别长选项,根据flag的设置返回不同的内容,一般flag都设置为NULL,返回val;
3、如果发生错误,如:未识别选项或者必须加参数的选项丢失参数,返回'?',如果在optstring中设置了第一个字符为':',丢失参数返回':',这个同getopt返回时一样的;
4、当缩写长选项引起歧义时或者不需要的选项强加了参数,都会返回'?';
5、返回-1表示选项处理全部结束;
6、如果在输入的argv[]中包含了独立的"--"字符串,同getopt一样,解析到这里返回-1,停止选项的解析;
 
测试实例:

#include <stdio.h>

#include <stdlib.h>

#include <getopt.h>

int main(int argc, char **argv)

{

        extern char *optarg;

        extern int optind, opterr, optopt;

        int c;

        int digit_optind = ;

        while ()

        {

                int this_option_optind= optind ? optind : ;

                int option_index = ;

                static struct option long_options[] =

                {

                        {"add", required_argument, NULL, },

                        {"append", no_argument, NULL, },

                        {"delete", required_argument, NULL, },

                        {"verbose", no_argument, NULL, },

                        {"create", required_argument, NULL, 'c'},

                        {"file", required_argument, NULL, },

                        {, , , },

                };

                c = getopt_long(argc, argv, ":abc:d:012", long_options, &option_index);

                if (c == -)

                        break;

                switch (c)

                {

                        case :

                                printf ("option %s", long_options[option_index].name);

                                if (optarg)

                                        printf (" with arg %s", optarg);

                                printf ("\n");

                                break;

                        case '':

                        case '':

                        case '':

                                if (digit_optind !=  && 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 'd':

                                printf ("option d 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();

}

运行自行操作测试

getopt_long 函数的更多相关文章

  1. getopt_long函数使用【转】

    转自:https://blog.csdn.net/cashey1991/article/details/7942809 平时在写程序时常常需要对命令行参数进行处理,当命令行参数个数较多时,如果按照顺序 ...

  2. 命令行参数解析函数getopt和getopt_long函数【转】

    原文地址:http://blog.csdn.net/cashey1991/article/details/7942809 getopt和getopt_long函数   平时在写程序时常常需要对命令行参 ...

  3. getopt() getopt_long()函数手册[中文翻译]

    getopt()函数 getopt_long函数 函数原型(function prototype) #include <unistd.h> int getopt(int argc, cha ...

  4. [置顶] getopt_long函数基本用法-linux

    一.感性认识: [c-sharp]  view plain copy   #include <stdio.h> #include <getopt.h> char * l_opt ...

  5. Linux getopt()函数 getopt_long()函数---转

    http://hi.baidu.com/scoundrelgg/item/d4083f8412eea05d26ebd97f Linux getopt()函数 getopt_long()函数 get_o ...

  6. C语言中getopt()和getopt_long()函数的用法

    一.参考文章 1.C语言中getopt()和getopt_long()函数的用法 2.linux 中解析命令行参数 (getopt_long用法) 二.调试经验

  7. 命令行參数选项处理:getopt()及getopt_long()函数使用

         在执行某个程序的时候,我们通常使用命令行參数来进行配置其行为.命令行选项和參数控制 UNIX 程序,告知它们怎样动作. 当 gcc的程序启动代码调用我们的入口函数 main(int argc ...

  8. webbench源码学习-->命令行选项解析函数getopt和getopt_long函数

    对于webbench这个网站压力测试工具网上介绍的很多,有深度详解剖析的,对于背景就不在提了, 听说最多可以模拟3万个并发连接去测试网站的负载能力,这里主要是学习了一下它的源码,做点 笔记. 官方介绍 ...

  9. getopt_long函数解析命令行参数

    转载:http://blog.csdn.net/hcx25909/article/details/7388750 每一天你都在使用大量的命令行程序,是不是感觉那些命令行参数用起来比较方便,他们都是使用 ...

随机推荐

  1. Java8自定义条件让集合分组

    /** * 将一个指定类型对象的集合按照自定义的一个操作分组: 每组对应一个List.最终返回结果类型是:List<List<T>> * * @param <T> ...

  2. CentOS7 笔记 (一) .NETCore

    安装系统CentOS,虚拟机好麻烦,直接在阿里云开了一个6个月免费的ECS. 熟悉Linux 基本命令 登录,exit,vi ,vim,vi保存关闭,w,ls,mkdir,df,ip addr,修改系 ...

  3. 阿里云 全部端口port

  4. 【ZOJ 4067】Books

    [链接] 我是链接,点我呀:) [题意] [题解] 统计a中0的个数cnt0 然后m减去cnt0 因为这cnt0个0是一定会取到的. 如果m==0了 那么直接找到数组中的最小值mi 输出mi-1就好 ...

  5. elasticsearch实战 修改IK源码实现词组动态更新

    下载IK源码 https://github.com/medcl/elasticsearch-analysis-ik/tree/v5.2.0 选择你对应ik的版本(ps:版本最好一致) http://l ...

  6. 转载 - Pinyin4j的基本用法

    原文:http://blog.csdn.net/pathuang68/article/details/6692882 1.     简单介绍 有时候,需要将汉字编程对应的拼音,以方便数据的处理.比如在 ...

  7. BA-WG-术语

    ActiveX ActiveX 技术 使无论任何语言产生的软件在网络环境中能够实现互操作性的一组技术.ActiveX 建立在 Microsoft 的组件对象模型 (COM) 基础上.尽管 Active ...

  8. _DataStructure_C_Impl:求图G中从顶点u到顶点v的一条简单路径

    #pragma once #include<stdio.h> #include<stdlib.h> #define StackSize 100 typedef int Data ...

  9. webbench压力測试工具

    apache的測试工具ab 在并发100个以上后会出现错误.网上也有非常多改ab源代码来解禁的. 只是还是推荐一款比較好用的压力測试工具webbench wget http://blog.zyan.c ...

  10. eclipse导入myeclipse中的web项目

    场景:在myeclipse编写的一个简单的电信计费系统项目,后面公用到eclipse,想把它给导入到eclipse中 操作:eclipse中在packag explorer空白处右键>impor ...