0

I'm using boost::program_options to parse ini file. Simple ini file parsing is easy with program_options. I can add allowed options and it would automatically validate the options. Is there any way to validate the value? Suppose I want to allow only string value from allowed list. e.g For key "TransportType", allowed values are Socket or RPC.

TransportType = Socket ( Anything other than Socket or RPC should not allowed)

I'm using config_file_iterator to which I pass a list of options. But it validates only keys and not values.

Here is code snippet.

namespace pod = boost::program_options::detail;

std::ifstream config("settings.ini");
if(!config)
{
    std::cerr<<"error"<<std::endl;
    return 1;
}

//parameters
std::set<std::string> options;
std::map<std::string, std::string> parameters;
options.insert("TransportType");
options.insert("URL");
options.insert("Host");
options.insert("Port");

try
{      
    for (pod::config_file_iterator i(config, options), e ; i != e; ++i)
    {
        std::cout << i->string_key <<" "<<i->value[0] << std::endl;
        parameters[i->string_key] = i->value[0];
    }        
}
catch(std::exception& e)    
{
    std::cerr<<"Exception: "<<e.what()<<std::endl;
}
user832096
  • 373
  • 1
  • 6
  • 15
  • 1
    Show us your code for parsing of ini file. And we will help you to add filtering in it (as I remember, there is `add_options()` for it). – Ilya Sep 03 '14 at 11:35
  • Note that Boost Property Tree already ships with a [fully functional ini parser](http://www.boost.org/doc/libs/1_56_0/doc/html/property_tree/reference.html#header.boost.property_tree.ini_parser_hpp). – ComicSansMS Sep 03 '14 at 12:56
  • Code snippet added for your reference. – user832096 Sep 04 '14 at 03:58

0 Answers0