Argtable学习(1)

Argtable是一款开源的ANSI C库,用于解析GNU样式的命令行选项。它提供声明式API,简化命令行分析,自动生成错误处理和帮助信息。此库适用于跨平台,具有良好的源代码可读性,且无外部依赖。了解如何使用argtable3,可以从教程和示例程序开始。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

什么是Argtable

Argtable是一款开源的ANSI C library,用来解析GNU样式的命令行选项。它通过定义可用于指定命令行语法的声明式API,从而简化了命令行的分析。argtable将自动生成一致的错误处理逻辑和命令行语法的文本描述,这对于一个健壮的cli程序来说,是非常必要的,但是很繁琐。例如要创建一个如下所示的cli程序:

$> util.exe --help
Usage: util.exe [-v] [--help] [--version] [--level=<n>] [-o myfile] <file> [<file>]...
Demonstrate command-line parsing in argtable3.

  --help                    display this help and exit
  --version                 display version information and exit
  --level=<n>               foo value
  -v, --verbose             verbose output
  -o myfile                 output file
  <file>                    input files

你可以使用以下代码段中的argtable实现命令行的分析逻辑:

#include "argtable3.h"

/* global arg_xxx structs */
struct arg_lit *verb, *help, *version;
struct arg_int *level;
struct arg_file *o, *file;
struct arg_end *end;

int main(int argc, char *argv[])
{
    /* the global arg_xxx structs are initialised within the argtable */
    void *argtable[] = {
        help    = arg_litn(NULL, "help", 0, 1, "display this help and exit"),
        version = arg_litn(NULL, "version", 0, 1, "display version info and exit"),
        level   = arg_intn(NULL, "level", "<n>", 0, 1, "foo value"),
        verb    = arg_litn("v", "verbose", 0, 1, "verbose output"),
        o       = arg_filen("o", NULL, "myfile", 0, 1, "output file"),
        file    = arg_filen(NULL, NULL, "<file>", 1, 100, "input files"),
        end     = arg_end(20),
    };
    
    int exitcode = 0;
    char progname[] = "util.exe";
    
    int nerrors;
    nerrors = arg_parse(argc,argv,argtable);

    /* special case: '--help' takes precedence over error reporting */
    if (help->count > 0)
    {
        printf("Usage: %s", progname);
        arg_print_syntax(stdout, argtable, "\n");
        printf("Demonstrate command-line parsing in argtable3.\n\n");
        arg_print_glossary(stdout, argtable, "  %-25s %s\n");
        exitcode = 0;
        goto exit;
    }

    /* If the parser returned any errors then display them and exit */
    if (nerrors > 0)
    {
        /* Display the error details contained in the arg_end struct.*/
        arg_print_errors(stdout, end, progname);
        printf("Try '%s --help' for more information.\n", progname);
        exitcode = 1;
        goto exit;
    }

exit:
    /* deallocate each non-null entry in argtable[] */
    arg_freetable(argtable, sizeof(argtable) / sizeof(argtable[0]));
    return exitcode;
}

一些特征

以下列出了一些为什么你应该在你的C/C++ 工具箱中包含Argtable的原因:

  • GNU-style command-line syntax: 使用标准和跨平台的方式来表达命令.
  • Declarative API: 通过指定具体的、非详细说明如何做的方式来排除复杂的解析逻辑.
  • Built-in error handling: 生成一致的错误处理逻辑.
  • Built-in help messages: 生成一致的命令行语法描述.
  • Written in ANSI C: 易于被其他语言创建绑定.
  • Readable source code: 源码注释良好,具有100%的分支测试覆盖率.
  • Single-file library: 没有繁琐的构建脚本。只需要将单个源文件放到你的项目中即可.
  • Self-contained: 没有外部依赖.
  • Cross-platform: 在大多数类Unix系统、Windows和嵌入式系统上都可以使用.
  • BSD-licensed:可以将此库用于任何目的,包括商业程序.

下一步

如果你想学习如何使用argtable3,可以从教程开始。如果你想从示例中学习,可以检查存储库中的示例程序列表。如果您发现文档或者代码有任何问题,可以将问题发送提交到Github项目页面

在云计算时代,cli程序变得越来越重要。我们希望argtable能够促进cli的复兴,有助于让开发人员和用户的生活变得更轻松。

注意:这个网站是针对最新的argtable v3 系列的,该系列源自Stewart Heitmann创建的argtable V2系列,argtable3不向后兼容。因此,如果要使用argtable2 api,必须转到argtable2网站并从其sourceforge.net 项目中获得源码。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值