可以提高Program_options单独的逗号分隔的参数值 [英] Can Boost Program_options separate comma separated argument values

查看:175
本文介绍了可以提高Program_options单独的逗号分隔的参数值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我的命令行是:

> prog --mylist=a,b,c

可以提升的program_options被设置看到三个不同的参数值的 MYLIST 参数?我已经配置program_options为:

Can Boost's program_options be setup to see three distinct argument values for the mylist argument? I have configured program_options as:

namespace po = boost::program_options;
po::options_description opts("blah")

opts.add_options()
    ("mylist", std::vector<std::string>>()->multitoken, "description");

po::variables_map vm;
po::store(po::parse_command_line(argc, argv, opts), vm);
po::notify(vm);

当我检查 MYLIST 参数的值,我看到一个值作为 A,B,C 。我想看到三个不同的值,劈在逗号。如果我在命令行指定这工作正常,如:

When I check the value of the mylist argument, I see one value as a,b,c. I'd like to see three distinct values, split on comma. This works fine if I specify the command line as:

> prog --mylist=a b c

> prog --mylist=a --mylist=b --mylist=c

有配置program_options使得它看到的方式 A,B,C 作为应该各自被插入载体的三个值,而不是一个?

Is there a way to configure program_options so that it sees a,b,c as three values that should each be inserted into the vector, rather than one?

我使用升压1.41,G ++ 4.5.0 20100520,并启用的C ++ 0x实验扩展。

I am using boost 1.41, g++ 4.5.0 20100520, and have enabled c++0x experimental extensions.

编辑:

接受的解决方案作品,但最终被更加复杂,国际海事组织,不仅仅是通过矢量迭代并手动分裂值。最后,我从詹姆斯McNellis的建议和实施这种方式。他的解决方案并没有提交然而,作为一个答案,所以我接受了hkaiser其他正确的解决方案。这两个工作,但手动标记化更清晰。

The accepted solution works but ends up being more complicated, IMO, than just iterating through a vector and splitting the values manually. In the end, I took the suggestion from James McNellis and implemented it that way. His solution wasn't submitted as an answer, however, so I accepted the other correct solution from hkaiser. Both worked, but the manual tokenization is clearer.

推荐答案

您可以为您的选择注册一个自定义的验证:

You could register a custom validator for your option:

namespace po = boost::program_options;

struct mylist_option 
{
    // values specified with --mylist will be stored here
    vector<std::string> values;

    // Function which validates additional tokens from command line.
    static void
    validate(boost::any &v, std::vector<std::string> const &tokens)
    {
        if (v.empty())
            v = boost::any(mylist_option());

        mylist_option *p = boost::any_cast<mylist_option>(&v);
        BOOST_ASSERT(p);

        boost::char_separator<char> sep(",");
        BOOST_FOREACH(std::string const& t, tokens)
        {
            if (t.find(",")) {
                // tokenize values and push them back onto p->values
                boost::tokenizer<boost::char_separator<char> > tok(t, sep);
                std::copy(tok.begin(), tok.end(), 
                    std::back_inserter(p->values));
            }
            else {
                // store value as is
                p->values.push_back(t);
            }
        }
    }
};

然后可以用作

opts.add_options()                 
    ("mylist", po::value<mylist_option>()->multitoken(), "description");

if (vm.count("mylist"))
{
    // vm["mylist"].as<mylist_option>().values will hold the value specified
    // using --mylist
}

这篇关于可以提高Program_options单独的逗号分隔的参数值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆