I've modified the provided solutions parseChildren
template to support parsing of JSON array literal:
template <class charT>
void parseChildren(std::string const& prefix, boost::property_tree::ptree& tree,
boost::program_options::parsed_options& options) {
if (tree.empty()) {
// remove first dot
std::basic_string<charT> name = prefix.substr(1);
// remove last dot if present
if (name.back() == '.') {
name.pop_back();
}
std::basic_string<charT> value = tree.data();
boost::program_options::basic_option<charT> opt;
opt.string_key = name;
opt.value.push_back(value);
opt.unregistered =
(options.description->find_nothrow(name, false) == nullptr);
opt.position_key = -1;
// append value to existing option-key if it exists
for (auto& o : options.options) {
if (o.string_key == name) {
o.value.push_back(value);
return;
}
}
options.options.push_back(opt);
} else {
for (auto it = tree.begin(); it != tree.end(); ++it) {
parseChildren<charT>(prefix + "." + it->first, it->second, options);
}
}
}
Now parsing the following JSON should resolve in the desired CLI options output:
{
"opt-1": "value-1",
"opt-2": ["value-2-1", "value-2-2"]
}
when using:
po::options_description opts("CLI options");
opts.add_options() //
("opt-1", po::value<std::string>(), "opt-1 desc") //
("opt-2", po::value<std::vector<std::string>>()->multitoken(), "opt-2 desc")