cxxopts
支持将命令行选项进行分组,这样可以让帮助信息的展示更加清晰,便于用户理解不同类型选项的用途。以下是关于 cxxopts
分组用法的详细介绍和示例代码。
示例代码
#include <iostream>
#include "cxxopts.hpp"
int main(int argc, char* argv[]) {
try {
// 创建 Options 对象,指定程序名称和简要描述
cxxopts::Options options("MyApp", "A command - line application with option groups");
// 定义输入相关的选项组
auto inputGroup = options.add_options("Input Options");
inputGroup
// 输入文件路径选项,使用 -i 或 --input 触发,需要一个字符串参数
("i,input", "Path to the input file", cxxopts::value<std::string>())
// 输入格式选项,使用 -f 或 --format 触发,需要一个字符串参数
("f,format", "Input file format", cxxopts::value<std::string>());
// 定义输出相关的选项组
auto outputGroup = options.add_options("Output Options");
outputGroup
// 输出文件路径选项,使用 -o 或 --output 触发,需要一个字符串参数
("o,output", "Path to the output file", cxxopts::value<std::string>())
// 输出模式选项,使用 -m 或 --mode 触发,需要一个字符串参数
("m,mode", "Output mode", cxxopts::value<std::string>());
// 定义通用选项组
auto generalGroup = options.add_options("General Options");
generalGroup
// 帮助选项,使用 -h 或 --help 触发,无参数
("h,help", "Print this help message");
// 解析命令行参数
auto result = options.parse(argc, argv);
// 如果用户指定了 --help 或 -h 选项,打印帮助信息并退出程序
if (result.count("help")) {
std::cout << options.help({"Input Options", "Output Options", "General Options"}) << std::endl;
return 0;
}
// 处理输入选项
if (result.count("input")) {
std::cout << "Input file: " << result["input"].as<std::string>() << std::endl;
}
if (result.count("format")) {
std::cout << "Input format: " << result["format"].as<std::string>() << std::endl;
}
// 处理输出选项
if (result.count("output")) {
std::cout << "Output file: " << result["output"].as<std::string>() << std::endl;
}
if (result.count("mode")) {
std::cout << "Output mode: " << result["mode"].as<std::string>() << std::endl;
}
}
catch (const cxxopts::exceptions::exception& e) {
// 处理解析选项时可能出现的异常
std::cerr << "Error parsing options: " << e.what() << std::endl;
return 1;
}
return 0;
}
代码解释
-
创建
cxxopts::Options
对象:cxxopts::Options options("MyApp", "A command - line application with option groups");
这行代码创建了一个
Options
对象,用于管理命令行选项,并指定了程序的名称和简要描述。 -
定义选项组:
- 使用
add_options
方法创建不同的选项组,每个选项组有一个名称,用于在帮助信息中区分不同类型的选项。
auto inputGroup = options.add_options("Input Options"); auto outputGroup = options.add_options("Output Options"); auto generalGroup = options.add_options("General Options");
- 使用
-
向选项组中添加选项:
- 对于每个选项组,可以链式调用添加具体的选项。例如:
inputGroup ("i,input", "Path to the input file", cxxopts::value<std::string>()) ("f,format", "Input file format", cxxopts::value<std::string>());
这里向
inputGroup
中添加了两个选项,分别是输入文件路径和输入文件格式。 -
解析命令行参数:
auto result = options.parse(argc, argv);
调用
parse
方法对传入的命令行参数进行解析,解析结果存储在result
对象中。 -
打印帮助信息:
if (result.count("help")) { std::cout << options.help({"Input Options", "Output Options", "General Options"}) << std::endl; return 0; }
当用户指定了
--help
或-h
选项时,调用help
方法打印帮助信息。help
方法的参数是一个选项组名称的列表,用于指定要显示哪些选项组的帮助信息。 -
处理解析结果:
- 通过
result.count
方法检查某个选项是否被指定,然后使用result["option_name"].as<type>()
方法获取选项的值。例如:
if (result.count("input")) { std::cout << "Input file: " << result["input"].as<std::string>() << std::endl; }
- 通过
运行效果
当你运行程序并带上 --help
选项时,输出的帮助信息会按照选项组进行分组展示,让用户能更清晰地了解各个选项的用途。例如:
MyApp: A command - line application with option groups
Input Options:
-i,--input Path to the input file
-f,--format Input file format
Output Options:
-o,--output Path to the output file
-m,--mode Output mode
General Options:
-h,--help Print this help message
通过这种方式,使用 cxxopts
的分组功能可以使命令行选项的管理和展示更加清晰和有条理。