【开源-常用C/C++命令行解析库对比】
- 游戏开发
- 2025-09-15 08:15:02

以下是几种常用的C/C++命令行解析库的对比表格,以及它们的GitHub开源库地址:
库名称语言特点是否支持子命令是否支持配置文件是否支持自动生成帮助信息GitHub地址ClaraC++11及以上单一头文件,轻量级,非异常错误处理,自动类型转换是否是Clara GitHubcxxoptsC++11及以上单一头文件,轻量级,支持多种数据类型,自动帮助信息是否是cxxopts GitHubCLI11C++11及以上功能强大,支持子命令、配置文件,自动生成帮助信息是是是CLI11 GitHubargparseC++17现代C++17头文件库,支持多种参数类型,自动帮助信息是否是argparse GitHubCmdlineC++轻量级,支持布尔标志和参数绑定,自动生成帮助信息否否是Cmdline GitHubcargsC/C++轻量级,支持多种参数类型,自动生成帮助信息否否是cargs GitHub 选择建议: 如果你需要轻量级且易于集成的库,可以选择 Clara 或 cxxopts,它们都是单一头文件库,易于集成到项目中。如果你需要功能强大且支持复杂命令行接口,CLI11 是一个不错的选择,它支持子命令、配置文件和自动生成帮助信息。如果你使用的是C语言或需要兼容C语言的库,可以考虑 cargs。如果你需要现代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 compression2. 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.
总结
以上是六种常用命令行解析库的简单示例。每种库都有其特点和适用场景,你可以根据项目需求选择合适的工具。希望这些示例对你有所帮助!
【开源-常用C/C++命令行解析库对比】由讯客互联游戏开发栏目发布,感谢您对讯客互联的认可,以及对我们原创作品以及文章的青睐,非常欢迎各位朋友分享到个人网站或者朋友圈,但转载请说明文章出处“【开源-常用C/C++命令行解析库对比】”
 
               
               
               
               
               
               
   
   
   
   
   
   
   
   
   
   
   
  