Search code examples
c++fileboostboost-program-options

How to use sections in a boost program options config file


This is obviously a fairly simple question, as no one else has had this issue such as this with the library.

When I run my program however, boost returns the error "Unrecognised Option Settings.Directoy" However I have defined this in my code and the file I've asked it to read from. Firstly here's my code, short because I did it as a test.

std::string Directory;

try {
    ifstream Config_File("Config.ini");
    options_description Game("Settings");
    Game.add_options()
        ("Directory", value<std::string>(&Directory)->default_value("Example.exe"));

    variables_map vm;
    store(parse_config_file(Config_File, Game), vm);
    notify(vm);

    if (vm.count("Directory"))
    {
        cout << Directory;
    }
}
catch(std::exception& E)
{
    std::cout << E.what() << std::endl;
}

Here is the file it read from "Config.ini"

[Settings]
Directory = "Example.exe"

I have tried debugging this, by changing the file type, name... removing the spaces?

Adding and removing quotation marks to the entry in Directory? Multiple things, which have given me no solution.


Solution

  • Remove the line

    [Settings]

    from your config file.

    Alternatively, tell the configuration file parser that you are using sections, like this:

      Game.add_options()
            ("Settings.Directory", ...
    

    http://www.boost.org/doc/libs/1_55_0/doc/html/program_options/overview.html#idp163379208