如何检查选项是否是 CLI11 的标志?
How can I check if an option is a flag with CLI11?
我正在使用 CLI11 库 (link) 来解析我的程序的命令行参数。
现在我想将有关程序选项的信息打印到标准输出。
似乎通过 App::add_flag(...)
添加的标志也在内部存储为选项,但我需要在输出中区分它们。
如何确定哪个选项是标志?
这是一个简化的例子:
std::string file, bool myflag;
CLI::App *command = app.add_subcommand("start", "Start the program");
command->add_option("file", file, "This is a file option")->required();
command->add_flag("--myflag", myflag);
print_description(command);
...
std::string print_description(CLI::App* command) {
for (const auto &option : command->get_options()) {
result << R"(<option name=")" << option->get_name() << R"(" description=")" << option->get_description()
<< R"(" type=")";
if (/*option is a flag*/) {
result << "flag";
} else {
result << "option";
}
result << R"("/>)";
}
return result.str();
}
根据这个问题:https://github.com/CLIUtils/CLI11/issues/439,函数 Option::get_expected_min
将始终 return 0 作为标志。
所以可以这样检查:
if (option->get_expected_min() == 0) {
result << "flag";
} else {
result << "option";
}
我正在使用 CLI11 库 (link) 来解析我的程序的命令行参数。
现在我想将有关程序选项的信息打印到标准输出。
似乎通过 App::add_flag(...)
添加的标志也在内部存储为选项,但我需要在输出中区分它们。
如何确定哪个选项是标志?
这是一个简化的例子:
std::string file, bool myflag;
CLI::App *command = app.add_subcommand("start", "Start the program");
command->add_option("file", file, "This is a file option")->required();
command->add_flag("--myflag", myflag);
print_description(command);
...
std::string print_description(CLI::App* command) {
for (const auto &option : command->get_options()) {
result << R"(<option name=")" << option->get_name() << R"(" description=")" << option->get_description()
<< R"(" type=")";
if (/*option is a flag*/) {
result << "flag";
} else {
result << "option";
}
result << R"("/>)";
}
return result.str();
}
根据这个问题:https://github.com/CLIUtils/CLI11/issues/439,函数 Option::get_expected_min
将始终 return 0 作为标志。
所以可以这样检查:
if (option->get_expected_min() == 0) {
result << "flag";
} else {
result << "option";
}