tags:

views:

76

answers:

1

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 this with boost::program_options? I get a multiple occurrences error if I try it

If not, what other libraries are available?

+2  A: 

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"));
}
dvide
+1 that works as I wanted. Cheers.
MeThinks
@dvide : is there a way to define multiple sections with a name like `[Section_1]` then `[Section_2]` and have it parsed, the problem I am facing is that , I don't know how many sections there will be up-front. do I have to insert this options `section_1.blablah' like wise for `sections_2` and so on. Because I don't know how many sections are there until I parse the file.
Gollum
@Gollum: You should have probably posted this as a separate question on here, but to answer your question there is an optional bool you can set in the 'parse_config_file' function to allow for unregistered entries. It's set to false by default. See here: http://www.boost.org/doc/libs/1_43_0/doc/html/boost/program_options/parse_config_file_id905267.html
dvide