首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何将notifier()和composing()一起使用?

如何将notifier()和composing()一起使用?
EN

Stack Overflow用户
提问于 2019-01-21 17:15:59
回答 1查看 40关注 0票数 1

我有一个使用选项声明的C++代码,它的帮助是:

代码语言:javascript
复制
boost::program_options::option_descriptions::add_options()

我需要添加针对正则表达式的每个选项值的检查和额外的检查。

为此,我决定使用notifier()。例如:

代码语言:javascript
复制
add_options()
("myoption", bpo::value<string>()->notifier(param_validator()), "My option description")
;

其中param_validator是函数式对象,它验证选项值。

我有另一个已经使用composing()的选项,例如:

代码语言:javascript
复制
("myoption2", bpo::value<string>()->composing(), "My option 2 description")

为相同的选项调用notifier()的语法是什么?或者可以为这样的选项调用notifier()?

EN

回答 1

Stack Overflow用户

发布于 2019-01-22 03:38:49

composing成员有一个notifier成员。所以您只需从composing调用notifier即可。工作示例:

代码语言:javascript
复制
#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;
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54286644

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档