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

本文共 3547 字,大约阅读时间需要 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/

    你可能感兴趣的文章
    Part 2异常和错误
    查看>>
    Pascal Script
    查看>>
    Spring Boot集成Redis实现keyspace监听 | Spring Cloud 34
    查看>>
    Spring Boot中的自定义事件详解与实战
    查看>>
    Passport 密码模式
    查看>>
    Spring Boot(七十六):集成Redisson实现布隆过滤器(Bloom Filter)
    查看>>
    passwd命令限制用户密码到期时间
    查看>>
    Spring @Async执行异步方法的简单使用
    查看>>
    PAT (Basic Level) Practice 乙级1021-1030
    查看>>
    PAT (Basic Level) Practice 乙级1031-1040
    查看>>
    PAT (Basic Level) Practice 乙级1041-1045
    查看>>
    SparkSql的元数据
    查看>>
    PAT (Basic Level) Practice 乙级1051-1055
    查看>>
    PAT (Basic Level) Practise - 写出这个数
    查看>>
    PAT 1027 Colors in Mars
    查看>>
    PAT 1127 ZigZagging on a Tree[难]
    查看>>
    PAT 2-07. 素因子分解(20)
    查看>>
    SparkSQL学习03-数据读取与存储
    查看>>
    PAT L2-012. 关于堆的判断
    查看>>
    PAT Spell It Right [非常简单]
    查看>>