Search code examples
c++boostdelimiterfile-formatboost-program-options

boost::program_options config file format


I am using boost::program_options to load command line arguments. Now I want to additionally load a config file with the same arguments set. I use it in very standard way:

ifstream ifs(filename.c_str());
if (ifs) {
    po::store(po::parse_config_file(ifs, optionsDescription), vm);
    po::notify(vm);
}

The thing is that parse_config_file accepts ini files in following standard format:

key1=value
key2 = value

But my file does not use "equal sign" to separate key and value but only spaces and/or tabs like this:

key1 value
key2  value

I need to keep this format for compatibility reason. Is there any way to achieve this with boost program_options? I have found style option of command_line parses, but there seems to be nothing like that for parse_config_file.


Solution

  • As per source code, boost seems looking for = symbol explicitly.

    So there is no direct way to handle your file format. You might have to change boost source or load file to memory and handle values as command line input.

    else if ((n = s.find('=')) != string::npos) {
        string name = m_prefix + trim_ws(s.substr(0, n));
        string value = trim_ws(s.substr(n+1));
    
        bool registered = allowed_option(name);
        if (!registered && !m_allow_unregistered)
            boost::throw_exception(unknown_option(name));
    
        found = true;
        this->value().string_key = name;
        this->value().value.clear();
        this->value().value.push_back(value);
        this->value().unregistered = !registered;
        this->value().original_tokens.clear();
        this->value().original_tokens.push_back(name);
        this->value().original_tokens.push_back(value);
        break;
    
    }