c++boostboost-program-options

Boost program_options partial key match


I'm trying to read config files with Boost::program_options. The config files look like:

hier.arch.y.option_name = 0x5
another.hier.archy.setting_name = 0x1E

I want to be able to search by just "option_name" or "setting_name". I'm not worried too much about duplicates but if there's a way optionally to match 2 substrings (i.e. "another" + "setting_name") that'd be great too.

Is there a way to get program_options to match based on substring?

Code sample:

namespace po = boost::program_options;
po::options_description cmd_opts{"Options"};
po::options_description config_file_opts;
cmd_opts.add_options()
  ("help,h", "Help message")
  ("config_help", "List of configurations available in config file");

po::variables_map vm;
po::store(parse_command_line(argc, argv, combined_opts), vm);
auto parsed_opts = po::parse_config_file<char>(vm.["config_file"].as<std::string>);
po::store(parsed_opts, vm);

Config file:

foo.bar.setting0 = 0x5
foo.notbar.setting1 = 0x6
bar.notfoo.setting2 = 0x5E

I'd like to assign the following options:

int setting0;
int setting1;
int setting2;

Solution

  • Q. What does enumerate the parsed options mean?

    A. Live On Coliru

    for (auto& opt : parsed_opts.options) {
        for (auto& val : opt.value) {
            std::cout 
                << std::quoted(opt.string_key) << "\t-> "
                << std::quoted(val) << std::endl;
        }
    }
    

    Prints

    "foo.bar.setting0"  -> "0x5"
    "foo.notbar.setting1"   -> "0x6"
    "bar.notfoo.setting2"   -> "0x5E"
    

    Matching fuzzily, sounds like a regex job, maybe?

    Live On Coliru

    int setting0 = 0, setting1 = 0, setting2 = 0;
    
    struct { int& target; boost::regex re; }
        fuzzy_opt_desc[] = {
            { setting0, boost::regex{R"(bar\.setting0$)"} },
            { setting1, boost::regex{R"(^foo\..*?setting1$)"} },
            { setting2, boost::regex{R"(setting2$)"} },
        };
    
    for (auto& opt : parsed_opts.options) {
    for (auto const& desc : fuzzy_opt_desc) {
        if (boost::regex_search(opt.string_key, desc.re)) {
            for (auto& val : opt.value) {
                desc.target = std::stoul(val, nullptr, 16);
                std::cout 
                    << std::quoted(opt.string_key) << "\t-> "
                    << desc.target << std::endl;
            }
        }
    } }
    
    std::cout << "Finally: " << setting0 << ", " << setting1 << ", " << setting2 << "\n";
    

    Prints

    "foo.bar.setting0"  -> 5
    "foo.notbar.setting1"   -> 6
    "bar.notfoo.setting2"   -> 94
    Finally: 5, 6, 94