zoukankan      html  css  js  c++  java
  • linux 之 getopt_long() 分类: arm-linux-Ubuntu 2013-07-30 09:33 594人阅读 评论(0) 收藏

    文件
    #include <getopt.h>

    函数原型

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

    函数说明

    getopt被用来解析命令行选项参数。
    getopt_long支持长选项的命令行解析,使用man getopt_long,得到其声明如下:
    int getopt_long(int argc, char * const argv[],const char *optstring, const struct option *longopts,int *longindex);
    函数中的argc和argv通常直接从main()的两个参数传递而来。optsting是选项参数组成的字符串:
    字符串optstring可以下列元素:
    1.单个字符,表示选项,
    2.单个字符后接一个冒号:表示该选项后必须跟一个参数。参数紧跟在选项后或者以空格隔开。该参数的指针赋给optarg。
    3 单个字符后跟两个冒号,表示该选项后可以有参数也可以没有参数。如果有参数,参数必须紧跟在选项后不能以空格隔开。该参数的指针赋给optarg。(这个特性是GNU的扩张)。
    optstring是一个字符串,表示可以接受的参数。例如,"a:b:cd",表示可以接受的参数是a,b,c,d,其中,a和b参数后面跟有更多的参数值。(例如:-a host -b name)
    参数longopts,其实是一个结构的实例:
    struct option {
    const char *name; //name表示的是长参数名
    int has_arg; //has_arg有3个值,no_argument(或者是0),表示该参数后面不跟参数值
    // required_argument(或者是1),表示该参数后面一定要跟个参数值
    // optional_argument(或者是2),表示该参数后面可以跟,也可以不跟参数值
    int *flag;
    //用来决定,getopt_long()的返回值到底是什么。如果flag是null,则函数会返回与该项option匹配的val值
    int val; //和flag联合决定返回值
    }
    给个例子:
    struct option long_options[] = {
    {"a123", required_argument, 0, 'a'},
    {"c123", no_argument, 0, 'c'},
    }
    现在,如果命令行的参数是-a 123,那么调用getopt_long()将返回字符'a',并且将字符串123由optarg返回(注意注意!字符串123由optarg带回!optarg不需要定义,在getopt.h中已经有定义),那么,如果命令行参数是-c,那么调用getopt_long()将返回字符'c',而此时,optarg是null。最后,当getopt_long()将命令行所有参数全部解析完成后,返回-1。

    范例

    #include <stdio.h>
    #include <getopt.h>
    char *l_opt_arg;
    char* const short_options = "nbl:";
    struct option long_options[] = {
    { "name", 0, NULL, 'n' },
    { "bf_name", 0, NULL, 'b' },
    { "love", 1, NULL, 'l' },
    { 0, 0, 0, 0},
    };
    int main(int argc, char *argv[])
    {
    int c;
    while((c = getopt_long (argc, argv, short_options, long_options, NULL)) != -1)
    {
    switch (c)
    {
    case 'n':
    printf("My name is XL. ");
    break;
    case 'b':
    printf("His name is ST. ");
    break;
    case 'l':
    l_opt_arg = optarg;
    printf("Our love is %s! ", l_opt_arg);
    break;
    }
    }
    return 0;
    }
    [root@localhost wyp]# gcc -o getopt getopt.c
    [root@localhost wyp]# ./getopt -n -b -l forever
    My name is XL.
    His name is ST.
    Our love is forever!
    [root@localhost liuxltest]#
    [root@localhost liuxltest]# ./getopt -nb -l forever
    My name is XL.
    His name is ST.
    Our love is forever!
    [root@localhost liuxltest]# ./getopt -nbl forever
    My name is XL.
    His name is ST.
    Our love is forever!

    版权声明:本文为博主原创文章,未经博主允许不得转载。

  • 相关阅读:
    $(document).ready(function(){}) 与 window.onload = function(){} 区别
    [如何在Mac下使用gulp] 1.创建项目及安装gulp
    Mac 执行 gulp 报错 bash: gulp: command not found
    css 字体单位之间的区分以及字体响应式实现
    [nodejs]在mac环境下如何将node更新至最新?
    [angular 1.x] 使用select 实现下拉列表 ngoptions 与 ngrepeat的取舍
    事件冒泡、事件捕获、事件委托初探
    Android 随机铃声管理器
    git 强制恢复到某一版本
    混乱中生存
  • 原文地址:https://www.cnblogs.com/mao0504/p/4706866.html
Copyright © 2011-2022 走看看