开发者

Is there a way to have multiple "name=value" lines in the INI file using boost::program_options::parse_config_file?

I want to be able to specify multiple name=value lines in the INI file using boost::program_options. Something like

[list.names]
name=value
name=value2
name=value3

Is there a way to achieve th开发者_StackOverflow中文版is with boost::program_options? I get a multiple occurrences error if I try it

If not, what other libraries are available?


Specify the value of the field as std::vector<value_type> in the options_description:

namespace po = boost::program_options;

po::options_description desc;
desc.add_options()
    ("list.names.name", po::value< std::vector<std::string> >(), "A collection of string values");

po::variables_map vm;
std::ifstream ini_file("config.ini");
po::store(po::parse_config_file(ini_file, desc), vm);
po::notify(variables);

if (vm.count("list.names.name"))
{
    const std::vector<std::string>& values = vm["list.names.name"].as< std::vector<std::string> >();
    std::copy(values.begin(), values.end(), std::ostream_iterator<std::string>(std::cout, "\n"));
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜