-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathyaml.cpp
More file actions
50 lines (37 loc) · 1.69 KB
/
Copy pathyaml.cpp
File metadata and controls
50 lines (37 loc) · 1.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
// Sample application using ConfigCpp to handle yaml-based configuration file
#include <iostream>
#include "config-cpp/config-cpp.h"
// User-provided type corresponding to yaml configuration data
#include "yaml-config.h"
int main(int argc, char**argv)
{
ConfigCpp::ConfigCpp config(argc,argv);
// Look for "config.{yaml|yml}" in the following directories
config.SetConfigName("config");
config.AddConfigPath("../inputs/yaml/");
config.AddConfigPath("../../inputs/yaml/");
config.AddBoolOption("b,top-bool","Bool option");
config.AddIntOption("t,top-int","Integer option");
config.AddStringOption("s,top-string","String option");
config.AddDoubleOption("d,top-double","Double option");
if (config.ReadInConfig()) {
std::cout << "Read in config successfully:\n" << config.GetConfigData() << "\n";
// Unmarshal all config data to user-provided type
YamlConfig configData;
if (config.UnmarshalYaml<YamlConfig>(configData)) {
std::cout << "Unmarshalled config successfully:\n" << configData;
} else {
std::cout << "Failed to ummarshal config\n";
}
std::cout << "IsSet(top-string) " << config.IsSet("top-string") << "\n";
// Retrieve specific config values read from yaml:
auto stringVal = config.GetString("top-string");
std::cout << "StringVal: " << stringVal << "\n";
auto intVal = config.GetInt("top-int");
std::cout << "IntVal: " << intVal << "\n";
auto dictVal = config.GetString("nested-dict.key2.key2-subkey2");
std::cout << "DictVal: " << dictVal << "\n";
} else {
std::cout << "Failed to read in config\n";
}
}