Skip to content

Commit b828377

Browse files
committed
nix: Make all options available as flags
Thus, instead of ‘--option <name> <value>’, you can write ‘--<name> <value>’. So --option http-connections 100 becomes --http-connections 100 Apart from brevity, the difference is that it's not an error to set a non-existent option via --option, but unrecognized arguments are fatal. Boolean options have special treatment: they're mapped to the argument-less flags ‘--<name>’ and ‘--no-<name>’. E.g. --option auto-optimise-store false becomes --no-auto-optimise-store
1 parent c8cc50d commit b828377

File tree

4 files changed

+40
-0
lines changed

4 files changed

+40
-0
lines changed

src/libstore/globals.cc

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,13 @@ template<> void BaseSetting<SandboxMode>::toJSON(JSONPlaceholder & out)
9898
AbstractSetting::toJSON(out);
9999
}
100100

101+
template<> void BaseSetting<SandboxMode>::convertToArg(Args & args)
102+
{
103+
args.mkFlag(0, name, {}, "Enable sandboxing.", 0, [=](Strings ss) { value = smEnabled; });
104+
args.mkFlag(0, "no-" + name, {}, "Disable sandboxing.", 0, [=](Strings ss) { value = smDisabled; });
105+
args.mkFlag(0, "relaxed-" + name, {}, "Enable sandboxing, but allow builds to disable it.", 0, [=](Strings ss) { value = smRelaxed; });
106+
}
107+
101108
void MaxBuildJobsSetting::set(const std::string & str)
102109
{
103110
if (str == "auto") value = std::max(1U, std::thread::hardware_concurrency());

src/libutil/config.cc

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,13 @@ void Config::toJSON(JSONObject & out)
115115
}
116116
}
117117

118+
void Config::convertToArgs(Args & args)
119+
{
120+
for (auto & s : _settings)
121+
if (!s.second.isAlias)
122+
s.second.setting->convertToArg(args);
123+
}
124+
118125
AbstractSetting::AbstractSetting(
119126
const std::string & name,
120127
const std::string & description,
@@ -128,12 +135,22 @@ void AbstractSetting::toJSON(JSONPlaceholder & out)
128135
out.write(to_string());
129136
}
130137

138+
void AbstractSetting::convertToArg(Args & args)
139+
{
140+
}
141+
131142
template<typename T>
132143
void BaseSetting<T>::toJSON(JSONPlaceholder & out)
133144
{
134145
out.write(value);
135146
}
136147

148+
template<typename T>
149+
void BaseSetting<T>::convertToArg(Args & args)
150+
{
151+
args.mkFlag(0, name, {}, description, 1, [=](Strings ss) { set(*ss.begin()); });
152+
}
153+
137154
template<> void BaseSetting<std::string>::set(const std::string & str)
138155
{
139156
value = str;
@@ -174,6 +191,12 @@ template<> std::string BaseSetting<bool>::to_string()
174191
return value ? "true" : "false";
175192
}
176193

194+
template<> void BaseSetting<bool>::convertToArg(Args & args)
195+
{
196+
args.mkFlag(0, name, {}, description, 0, [=](Strings ss) { value = true; });
197+
args.mkFlag(0, "no-" + name, {}, description, 0, [=](Strings ss) { value = false; });
198+
}
199+
177200
template<> void BaseSetting<Strings>::set(const std::string & str)
178201
{
179202
value = tokenizeString<Strings>(str);
@@ -216,6 +239,8 @@ template class BaseSetting<long long>;
216239
template class BaseSetting<unsigned long long>;
217240
template class BaseSetting<bool>;
218241
template class BaseSetting<std::string>;
242+
template class BaseSetting<Strings>;
243+
template class BaseSetting<StringSet>;
219244

220245
void PathSetting::set(const std::string & str)
221246
{

src/libutil/config.hh

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@ public:
6363
void resetOverriden();
6464

6565
void toJSON(JSONObject & out);
66+
67+
void convertToArgs(Args & args);
6668
};
6769

6870
class AbstractSetting
@@ -99,6 +101,8 @@ protected:
99101

100102
virtual void toJSON(JSONPlaceholder & out);
101103

104+
virtual void convertToArg(Args & args);
105+
102106
bool isOverriden() { return overriden; }
103107
};
104108

@@ -132,6 +136,8 @@ public:
132136

133137
std::string to_string() override;
134138

139+
void convertToArg(Args & args) override;
140+
135141
void toJSON(JSONPlaceholder & out) override;
136142
};
137143

src/nix/main.cc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ struct NixArgs : virtual MultiCommand, virtual MixCommonArgs
2222
});
2323

2424
mkFlag(0, "version", "show version information", std::bind(printVersion, programName));
25+
26+
settings.convertToArgs(*this);
2527
}
2628
};
2729

0 commit comments

Comments
 (0)