博客
关于我
C getopt.h
阅读量:310 次
发布时间:2019-03-03

本文共 3595 字,大约阅读时间需要 11 分钟。

截取getopt.h的功能与应用

getopt.h是GnuLib库中一个重要的头文件,广泛应用于各种软件和套件中。它定义了处理命令行选项的基本功能,为程序的参数解析提供了强有力的支持。

getopt.h的功能概述

getopt.h文件主要定义了以下内容:

  • 结构体定义:定义了struct option结构体,用于描述长选项(即以--开头的选项)。
  • 全局变量:包括optargoptindopterroptopt等变量,用于存储解析过程中的参数信息。
  • 函数声明
    • getopt(int argc, char *const argv[], const char *optstring): 解析短选项(如-o-i等)。
    • getopt_long(int argc, char *const argv[], const char *optstring, const struct option *longopts, int *longindex): 解析长选项(如--help--datadir等)。
    • getopt_long_only(int argc, char *const argv[], const char *optstring, const struct option *longopts, int *longindex): 专门用于处理长选项的简化版本。
  • getopt函数的使用

    getopt函数是最常用的选项解析函数,其参数包括:

    • argc:主程序传入的命令行参数个数。
    • argv:存储命令行参数的数组。
    • optstring:一个字符串,包含所有有效的短选项字符。

    选项字符的定义方式

    optstring中,每个字符可以有以下两种表示方式:

    • 单独出现:表示该选项没有对应的参数。
    • 后接::表示该选项有对应的参数,参数会被存储在optarg变量中。

    getopt函数的执行过程:

  • 逐个分析argv数组中的每个参数。
  • 如果当前参数以-开头,检查其后是否有有效的选项字符。
  • 如果是短选项,直接处理;如果是长选项,转换为对应的短选项处理。
  • 如果选项参数有效,更新全局变量(如optargoptind等)。
  • 如果argv数组已遍历完毕,getopt返回-1,表示解析完成。
  • 代码示例

    #include 
    #include
    int main(int argc, char *const argv[]) {
    static struct option long_options[] = { {0}, {1}, {2}, ... };
    int option_index = 0;
    int opt = getopt(argc, argv, "abc");
    // 根据返回值进行相应的处理
    }

    getopt_long函数的使用

    getopt_long函数扩展了getopt的功能,能够处理长选项。其参数包括:

    • longopts:一个struct option数组,存储所有长选项的信息。
    • longindex:一个指针,用于记录当前解析的是longopts数组中的哪个选项。

    struct option结构体

    struct option结构体包含以下成员:

    • name:长选项的名称。
    • has_arg:选项是否有参数(可选值:no_argumentrequired_argumentoptional_argument)。
    • flag:如果为NULL,表示该选项是一个布尔标志;否则,表示该选项有一个值。
    • val:该选项的值。

    代码示例

    #include 
    static struct option long_options[] = {
    {"help", no_argument, 0, 'h'},
    {"datadir", required_argument, 0, 'd'},
    {"int8", no_argument, 0, 'i'},
    {"fp16", no_argument, 0, 'f'},
    {"useILoop", no_argument, 0, 'l'},
    {"useDLACore", required_argument, 0, 'u'},
    {"batch", required_argument, 0, 'b'},
    {nullptr, 0, nullptr, 0}
    };
    int option_index = 0;
    int arg = getopt_long(argc, argv, "hd:iu", long_options, &option_index);

    TensorRT中的参数解析

    TensorRT框架中常见一个参数解析的例子:

    #include 
    #include
    namespace samplesCommon {
    bool parseArgs(Args &args, int argc, char *argv[]) {
    static struct option long_options[] = {
    {"help", no_argument, 0, 'h'},
    {"datadir", required_argument, 0, 'd'},
    {"int8", no_argument, 0, 'i'},
    {"fp16", no_argument, 0, 'f'},
    {"useILoop", no_argument, 0, 'l'},
    {"useDLACore", required_argument, 0, 'u'},
    {"batch", required_argument, 0, 'b'},
    {nullptr, 0, nullptr, 0}
    };
    int option_index = 0;
    while (true) {
    int arg = getopt_long(argc, argv, "hd:iuflb", long_options, &option_index);
    if (arg == -1) {
    break;
    }
    switch (arg) {
    case 'h': args.help = true; return true;
    case 'd': if (optarg) { args.dataDirs.push_back(optarg); } else {
    std::cerr << "ERROR: --datadir requires option argument" << std::endl;
    return false;
    }
    case 'i': args.runInInt8 = true; break;
    case 'f': args.runInFp16 = true; break;
    case 'l': args.useILoop = true; break;
    case 'u': if (optarg) { args.useDLACore = std::stoi(optarg); } break;
    case 'b': if (optarg) { args.batch = std::stoi(optarg); } break;
    default: return false;
    }
    }
    return true;
    }
    }

    结论

    getopt.h头文件为程序员提供了强大的选项解析工具,支持处理短选项和长选项,并允许自定义参数解析逻辑。通过合理使用这些函数,开发者可以实现更加灵活和用户友好的命令行参数解析方案。

    转载地址:http://xatm.baihongyu.com/

    你可能感兴趣的文章
    Objective-C实现图片转化为 ASCII图(附完整源码)
    查看>>
    Objective-C实现图的拓扑序列(附完整源码)
    查看>>
    Objective-C实现图的邻接矩阵(附完整源码)
    查看>>
    Objective-C实现图结构(附完整源码)
    查看>>
    Objective-C实现圆球的表面积和体积(附完整源码)
    查看>>
    Objective-C实现在list中找到next greatest element下一个更大元素算法(附完整源码)
    查看>>
    Objective-C实现在Regex的帮助下检查字谜算法(附完整源码)
    查看>>
    Objective-C实现在指定区间 [a, b] 中找到函数的实根,其中 f(a)*f(b) < 0算法(附完整源码)
    查看>>
    Objective-C实现均值滤波(附完整源码)
    查看>>
    Objective-C实现埃拉托斯特尼筛法算法(附完整源码)
    查看>>
    Objective-C实现埃拉托色尼筛法(附完整源码)
    查看>>
    Objective-C实现域名解析(附完整源码)
    查看>>
    Objective-C实现域名转IP(附完整源码)
    查看>>
    Objective-C实现培根密码算法(附完整源码)
    查看>>
    Objective-C实现基于 LIFO的堆栈算法(附完整源码)
    查看>>
    Objective-C实现基于 LinkedList 的添加两个数字的解决方案算法(附完整源码)
    查看>>
    Objective-C实现基于opencv的抖动算法(附完整源码)
    查看>>
    Objective-C实现基于事件对象实现线程同步(附完整源码)
    查看>>
    Objective-C实现基于信号实现线程同步(附完整源码)
    查看>>
    Objective-C实现基于数据流拷贝文件(附完整源码)
    查看>>