32#ifndef BOOST_PROGRAM_OPTIONS_YAML_PARSER_HPP
33#define BOOST_PROGRAM_OPTIONS_YAML_PARSER_HPP
40#include <yaml-cpp/yaml.h>
41#include <boost/program_options/parsers.hpp>
42#include <boost/program_options/options_description.hpp>
57 template<
typename char_T =
char>
58 static boost::program_options::basic_parsed_options<char_T>
60 const boost::program_options::options_description &desc)
62 YAML::Node config = YAML::Load(istrm);
63 return parse(config, desc);
76 template<
typename char_T =
char>
77 static boost::program_options::basic_parsed_options<char_T>
79 const boost::program_options::options_description &desc)
81 boost::program_options::basic_parsed_options<char_T> result(&desc);
88 const YAML::Node &node,
89 const std::string &key,
90 boost::program_options::basic_parsed_options<char_T> &result)
92 switch (node.Type()) {
93 case YAML::NodeType::Scalar:
94 add_option(key, node.as<std::basic_string<char_T>>(), result);
96 case YAML::NodeType::Sequence:
99 case YAML::NodeType::Map:
103 throw std::invalid_argument(
"Unsupported node type");
109 const YAML::Node &node,
110 const std::string &key,
111 boost::program_options::basic_parsed_options<char_T> &result)
113 for (
const auto &subnode: node) {
119 const YAML::Node &node,
120 const std::string &key,
121 boost::program_options::basic_parsed_options<char_T> &result)
123 for (
const auto &pair: node) {
124 std::string node_key = pair.first.as<std::string>();
126 std::stoul(node_key);
128 }
catch (std::invalid_argument &e) {
131 node_key = key +
'.' + node_key;
138 template<
typename char_T>
static void
140 const std::basic_string<char_T> &value,
141 boost::program_options::basic_parsed_options<char_T> &result)
144 throw std::logic_error(
"Empty key - malformed Config file");
146 if (result.description->find_nothrow(key,
false) ==
NULL) {
152 auto option_iter = std::find_if(
153 result.options.begin(),
154 result.options.end(),
155 [&key](
const boost::program_options::basic_option<char_T> &test) {
156 return test.string_key == key;
159 if (option_iter == result.options.end()) {
160 result.options.emplace_back();
161 option_iter = result.options.end() - 1;
162 option_iter->string_key = key;
163 option_iter->unregistered
164 = (result.description->find_nothrow(key,
false) ==
NULL);
167 option_iter->value.push_back(value);
Definition: yaml_parser.hpp:45
static void parse_subnode_map(const YAML::Node &node, const std::string &key, boost::program_options::basic_parsed_options< char_T > &result)
Definition: yaml_parser.hpp:118
static void add_option(const std::string &key, const std::basic_string< char_T > &value, boost::program_options::basic_parsed_options< char_T > &result)
Definition: yaml_parser.hpp:139
static boost::program_options::basic_parsed_options< char_T > parse(const YAML::Node &node, const boost::program_options::options_description &desc)
Pasre YAML configuration from YAML::Node.
Definition: yaml_parser.hpp:78
static boost::program_options::basic_parsed_options< char_T > parse(std::istream &istrm, const boost::program_options::options_description &desc)
Parse YAML configuration from istream.
Definition: yaml_parser.hpp:59
static void parse_subnode(const YAML::Node &node, const std::string &key, boost::program_options::basic_parsed_options< char_T > &result)
Definition: yaml_parser.hpp:87
static void parse_subnode_sequence(const YAML::Node &node, const std::string &key, boost::program_options::basic_parsed_options< char_T > &result)
Definition: yaml_parser.hpp:108
#define NULL
Definition: list.c:50