我有一个使用选项声明的C++代码,它的帮助是:
boost::program_options::option_descriptions::add_options()我需要添加针对正则表达式的每个选项值的检查和额外的检查。
为此,我决定使用notifier()。例如:
add_options()
("myoption", bpo::value<string>()->notifier(param_validator()), "My option description")
;其中param_validator是函数式对象,它验证选项值。
我有另一个已经使用composing()的选项,例如:
("myoption2", bpo::value<string>()->composing(), "My option 2 description")为相同的选项调用notifier()的语法是什么?或者可以为这样的选项调用notifier()?
发布于 2019-01-22 03:38:49
composing成员有一个notifier成员。所以您只需从composing调用notifier即可。工作示例:
#include<string>
#include<iostream>
#include <boost/program_options.hpp>
namespace po = boost::program_options;
int main()
{
using str_vect_type = std::vector<std::string>;
size_t cmdcnt = 5;
const char* cmdline[] = { "dmy.exe", "--myoption", "this_that", "--myoption2", "testing" };
auto param_validator = [](const std::string& x) {std::cout << " " << x << "\nparam_validator\n"; };
auto other = [](const str_vect_type& x) {for (auto& s : x)std::cout << " " << s << '\n'; std::cout << "the_other\n"; };
po::variables_map vm;
try {
po::options_description desc("Allowed options");
desc.add_options()
("myoption", po::value<std::string>()->notifier(param_validator), "My option description")
("myoption2", po::value<std::vector<std::string> >()->composing()->notifier(other), "My option 2 description")
;
po::store(po::parse_command_line(cmdcnt, cmdline, desc), vm);
//as if from ini file hack...
const_cast<str_vect_type*>(&vm["myoption2"].as<str_vect_type>())->push_back("another");
po::notify(vm);
}
catch (std::exception& e) {
std::cerr << "error: " << e.what() << "\n";
return 1;
}
return 0;
}https://stackoverflow.com/questions/54286644
复制相似问题