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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
|
#include "config.hh"
#include "args.hh"
namespace nix {
void Config::set(const std::string & name, const std::string & value)
{
auto i = _settings.find(name);
if (i == _settings.end())
throw UsageError("unknown setting '%s'", name);
i->second.setting->set(value);
}
void Config::add(AbstractSetting * setting)
{
_settings.emplace(setting->name, Config::SettingData{false, setting});
for (auto & alias : setting->aliases)
_settings.emplace(alias, Config::SettingData{true, setting});
bool set = false;
auto i = initials.find(setting->name);
if (i != initials.end()) {
setting->set(i->second);
initials.erase(i);
set = true;
}
for (auto & alias : setting->aliases) {
auto i = initials.find(alias);
if (i != initials.end()) {
if (set)
warn("setting '%s' is set, but it's an alias of '%s' which is also set",
alias, setting->name);
else {
setting->set(i->second);
initials.erase(i);
set = true;
}
}
}
}
void Config::warnUnused()
{
for (auto & i : initials)
warn("unknown setting '%s'", i.first);
}
std::string Config::dump()
{
std::string res;
for (auto & opt : _settings)
if (!opt.second.isAlias)
res += opt.first + " = " + opt.second.setting->to_string() + "\n";
return res;
}
AbstractSetting::AbstractSetting(
const std::string & name,
const std::string & description,
const std::set<std::string> & aliases)
: name(name), description(description), aliases(aliases)
{
}
template<> void Setting<std::string>::set(const std::string & str)
{
value = str;
}
template<> std::string Setting<std::string>::to_string()
{
return value;
}
template<> void Setting<int>::set(const std::string & str)
{
try {
value = std::stoi(str);
} catch (...) {
throw UsageError("setting '%s' has invalid value '%s'", name, str);
}
}
template<> std::string Setting<int>::to_string()
{
return std::to_string(value);
}
template<> void Setting<bool>::set(const std::string & str)
{
if (str == "true" || str == "yes" || str == "1")
value = true;
else if (str == "false" || str == "no" || str == "0")
value = false;
else
throw UsageError("Boolean setting '%s' has invalid value '%s'", name, str);
}
template<> std::string Setting<bool>::to_string()
{
return value ? "true" : "false";
}
void PathSetting::set(const std::string & str)
{
if (str == "") {
if (allowEmpty)
value = "";
else
throw UsageError("setting '%s' cannot be empty", name);
} else
value = canonPath(str);
}
}
|