当前位置: 首页 > news >正文

【开源-常用C/C++命令行解析库对比】

以下是几种常用的C/C++命令行解析库的对比表格,以及它们的GitHub开源库地址:

库名称语言特点是否支持子命令是否支持配置文件是否支持自动生成帮助信息GitHub地址
ClaraC++11及以上单一头文件,轻量级,非异常错误处理,自动类型转换Clara GitHub
cxxoptsC++11及以上单一头文件,轻量级,支持多种数据类型,自动帮助信息cxxopts GitHub
CLI11C++11及以上功能强大,支持子命令、配置文件,自动生成帮助信息CLI11 GitHub
argparseC++17现代C++17头文件库,支持多种参数类型,自动帮助信息argparse GitHub
CmdlineC++轻量级,支持布尔标志和参数绑定,自动生成帮助信息Cmdline GitHub
cargsC/C++轻量级,支持多种参数类型,自动生成帮助信息cargs GitHub

选择建议:

  1. 如果你需要轻量级且易于集成的库,可以选择 Claracxxopts,它们都是单一头文件库,易于集成到项目中。
  2. 如果你需要功能强大且支持复杂命令行接口CLI11 是一个不错的选择,它支持子命令、配置文件和自动生成帮助信息。
  3. 如果你使用的是C语言或需要兼容C语言的库,可以考虑 cargs
  4. 如果你需要现代C++17特性支持argparse 是一个简洁且功能丰富的选择。

以下是几种常用C/C++命令行解析库的简单使用示例

1. Clara

GitHub地址:Clara GitHub
示例代码:
#include <boost/program_options.hpp>
#include <iostream>

namespace po = boost::program_options;

int main(int argc, char* argv[]) {
    po::options_description desc("Allowed options");
    desc.add_options()
        ("help,h", "produce help message")
        ("compression,c", po::bool_switch(), "enable compression");

    po::variables_map vm;
    po::store(po::parse_command_line(argc, argv, desc), vm);
    po::notify(vm);

    if (vm.count("help")) {
        std::cout << desc << "\n";
        return 1;
    }

    bool compression = vm["compression"].as<bool>();
    std::cout << "Compression is " << (compression ? "on" : "off") << ".\n";

    return 0;
}
输出示例:
$ ./example --help
Allowed options:
  --help,h         produce help message
  --compression,c  enable compression

2. cxxopts

GitHub地址:cxxopts GitHub
示例代码:
#include <cxxopts.hpp>
#include <iostream>

int main(int argc, char* argv[]) {
    cxxopts::Options options("This is a test program.", "This goes after the options.");
    options.add_options()
        ("h,help", "Print usage")
        ("n,name", "Your name", cxxopts::value<std::string>())
        ("a,age", "Your age", cxxopts::value<int>());

    auto result = options.parse(argc, argv);

    if (result.count("help")) {
        std::cout << options.help() << std::endl;
        return 0;
    }

    std::string name = result["name"].as<std::string>();
    int age = result["age"].as<int>();

    std::cout << "Hello, " << name << "! You are " << age << " years old." << std::endl;

    return 0;
}
输出示例:
$ ./example --name John --age 30
Hello, John! You are 30 years old.

3. CLI11

GitHub地址:CLI11 GitHub
示例代码:
#include <CLI/CLI.hpp>
#include <iostream>

int main(int argc, char* argv[]) {
    CLI::App app{"This is a test application."};

    std::string name;
    int age;

    app.add_option("-n,--name", name, "Your name")->required();
    app.add_option("-a,--age", age, "Your age")->required();

    CLI11_PARSE(app, argc, argv);

    std::cout << "Hello, " << name << "! You are " << age << " years old." << std::endl;

    return 0;
}
输出示例:
$ ./example --name Alice --age 25
Hello, Alice! You are 25 years old.

4. argparse

GitHub地址:argparse GitHub
示例代码:
#include <argparse/argparse.hpp>
#include <iostream>

int main(int argc, char* argv[]) {
    argparse::ArgumentParser program("This is a test program.");

    program.add_argument("-n", "--name").help("Your name").required();
    program.add_argument("-a", "--age").help("Your age").required();

    try {
        program.parse_args(argc, argv);
    } catch (const std::runtime_error& err) {
        std::cerr << err.what() << std::endl;
        std::cerr << program;
        return 1;
    }

    std::string name = program.get<std::string>("--name");
    int age = program.get<int>("--age");

    std::cout << "Hello, " << name << "! You are " << age << " years old." << std::endl;

    return 0;
}
输出示例:
$ ./example --name Bob --age 40
Hello, Bob! You are 40 years old.

5. Cmdline

GitHub地址:Cmdline GitHub
示例代码:
#include <cmdline.h>
#include <iostream>

int main(int argc, char* argv[]) {
    cmdline::parser p;
    p.add<std::string>("name", 'n', "Your name", true);
    p.add<int>("age", 'a', "Your age", true);

    p.parse_check(argc, argv);

    std::string name = p.get<std::string>("name");
    int age = p.get<int>("age");

    std::cout << "Hello, " << name << "! You are " << age << " years old." << std::endl;

    return 0;
}
输出示例:
$ ./example -n Charlie -a 35
Hello, Charlie! You are 35 years old.

6. cargs

GitHub地址:cargs GitHub
示例代码:
#include <cargs.h>
#include <stdio.h>

int main(int argc, char* argv[]) {
    cargs_parser_t parser = cargs_parser_create("This is a test program.");
    cargs_add_option(parser, "name", 'n', "Your name", CARGS_STRING);
    cargs_add_option(parser, "age", 'a', "Your age", CARGS_INT);

    if (!cargs_parse(parser, argc, argv)) {
        cargs_print_help(parser);
        return 1;
    }

    char* name = cargs_get_string(parser, "name");
    int age = cargs_get_int(parser, "age");

    printf("Hello, %s! You are %d years old.\n", name, age);

    cargs_parser_destroy(parser);
    return 0;
}
输出示例:
$ ./example -n David -a 28
Hello, David! You are 28 years old.

总结

以上是六种常用命令行解析库的简单示例。每种库都有其特点和适用场景,你可以根据项目需求选择合适的工具。希望这些示例对你有所帮助!

相关文章:

  • jvm内存不够,怎么重新分配
  • 蓝桥杯4T平台(串口打印电压值)
  • 【Prometheus】prometheus如何监控k8s集群
  • 工程化与框架系列(16)--前端路由实现
  • Kotlin协程(二)协程的生命周期及管理
  • 在Vscode开发QT,完成QT环境的配置
  • 体育数据分析:竞技表现优化与商业价值挖掘的技术范式
  • Golang的数据库分库分表
  • AI 外呼产品架构解读:让智能外呼更精准高效
  • PDF万能水印删除工具
  • 利用Adobe Acrobat 实现PPT中图片分辨率的提升
  • 自己编译RustDesk,并将自建ID服务器和key信息写入客户端
  • java 项目中设计模式 之单例模式
  • 简述一下Spark中的hashShuffle和Sortshuffle两中shauffle的流程
  • 命名管道——进程间通信
  • 【JAVA面试题】设计模式之原型模式
  • 安装Linux操作系统
  • uni-app开发安卓和iOS 打包流程(云打包)
  • 代码随想录刷题学习日记
  • Oracle 11g的部署配置
  • 中美经贸高层会谈将在午餐后继续
  • 19个剧团15台演出,上海民营院团尝试文旅融合新探索
  • 上海消防全面推行“检查码”,会同相关部门推行“综合查一次”
  • 潘功胜:将下调个人住房公积金贷款利率0.25个百分点
  • 起底新型保健品电话销售诈骗:从快递信息中筛选对象,忽悠其高价买药
  • 白俄罗斯政府代表团将访问朝鲜