2

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;
sehe
  • 374,641
  • 47
  • 450
  • 633
jkang
  • 483
  • 7
  • 19

2 Answers2

1

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
sehe
  • 374,641
  • 47
  • 450
  • 633
  • Quick question, why did you add a loop here? `for (auto& val : opt.value) {` Couldn't you have simply assigned: `desc.target = std::stoul(opt.value, nullptr, 16);` – jkang Jun 15 '20 at 22:27
  • 1
    As the docs state, [`value` is a vector of strings`](https://www.boost.org/doc/libs/1_73_0/doc/html/boost/program_options/basic_option.html#boost.program_options.basic_option.value). See e.g. this example: http://coliru.stacked-crooked.com/a/7e52d93003d89a53 – sehe Jun 15 '20 at 22:59
1

I answered the question as posed. However, this is really subverting the Boost Program Options library.

It seems much easier to just parse the same thing manually:

Live On Coliru

#include <boost/fusion/adapted/std_pair.hpp>
#include <boost/spirit/home/x3.hpp>
#include <boost/xpressive/xpressive_dynamic.hpp>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>

auto parse_cfg(std::string fname) {
    std::multimap<std::string, std::string> map;

    std::ifstream ifs(fname);
    std::string const cfg(std::istreambuf_iterator<char>(ifs), {});

    using namespace boost::spirit::x3;
    phrase_parse(begin(cfg), end(cfg),
         -(lexeme[+(graph - '=')] >> '=' >> lexeme[+~char_("\r\n")]) %
             eol,
         blank, map);

    return map;
}

int main() {
    auto map = parse_cfg("test.cfg");

    int setting0 = 0, setting1 = 0, setting2 = 0;

    using boost::xpressive::sregex;
    struct { int& target; sregex re; } fuzzy_opt_desc[] = {
        { setting0, sregex::compile(R"(bar\.setting0$)") },
        { setting1, sregex::compile(R"(^foo\..*?setting1$)") },
        { setting2, sregex::compile(R"(setting2$)") },
    };

    for (auto& [key, val] : parse_cfg("test.cfg")) {
        for (auto& [target,re] : fuzzy_opt_desc) {
            if (regex_search(key, re)) {
                target = std::stoul(val, nullptr, 16);
                std::cout << std::quoted(key) << "\t-> " << target << std::endl;
            }
        }
    }

    std::cout << "Finally: " << setting0 << ", " << setting1 << ", " << setting2 << "\n";
}

Prints

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

Note that there is no link dependency on any boost library any more.

sehe
  • 374,641
  • 47
  • 450
  • 633
  • Thanks, I was hoping for a pre-made solution but it appears boost::program_options isn't it. Are you aware of any lib that does the "fuzzy" matching by default for config file reading? – jkang Jun 15 '20 at 21:15
  • Libraries exist if they solve well-defined problems. This problem isn't well-defined, so I don't know why you expect a solution to exist. [For fun, a tweaked version with statically-compiled regexes https://godbolt.org/z/cYc9Jm] – sehe Jun 15 '20 at 21:32