Clean up --arg processing

This commit is contained in:
Eelco Dolstra 2024-03-01 14:14:14 +01:00
parent bf48501194
commit d72ee91d07
2 changed files with 18 additions and 9 deletions

View file

@ -20,7 +20,7 @@ MixEvalArgs::MixEvalArgs()
.description = "Pass the value *expr* as the argument *name* to Nix functions.", .description = "Pass the value *expr* as the argument *name* to Nix functions.",
.category = category, .category = category,
.labels = {"name", "expr"}, .labels = {"name", "expr"},
.handler = {[&](std::string name, std::string expr) { autoArgs[name] = 'E' + expr; }} .handler = {[&](std::string name, std::string expr) { autoArgs.insert_or_assign(name, AutoArg{AutoArgExpr(expr)}); }}
}); });
addFlag({ addFlag({
@ -28,7 +28,7 @@ MixEvalArgs::MixEvalArgs()
.description = "Pass the string *string* as the argument *name* to Nix functions.", .description = "Pass the string *string* as the argument *name* to Nix functions.",
.category = category, .category = category,
.labels = {"name", "string"}, .labels = {"name", "string"},
.handler = {[&](std::string name, std::string s) { autoArgs[name] = 'S' + s; }}, .handler = {[&](std::string name, std::string s) { autoArgs.insert_or_assign(name, AutoArg{AutoArgString(s)}); }},
}); });
addFlag({ addFlag({
@ -154,13 +154,17 @@ MixEvalArgs::MixEvalArgs()
Bindings * MixEvalArgs::getAutoArgs(EvalState & state) Bindings * MixEvalArgs::getAutoArgs(EvalState & state)
{ {
auto res = state.buildBindings(autoArgs.size()); auto res = state.buildBindings(autoArgs.size());
for (auto & i : autoArgs) { for (auto & [name, arg] : autoArgs) {
auto v = state.allocValue(); auto v = state.allocValue();
if (i.second[0] == 'E') std::visit(overloaded {
state.mkThunk_(*v, state.parseExprFromString(i.second.substr(1), state.rootPath("."))); [&](const AutoArgExpr & arg) {
else state.mkThunk_(*v, state.parseExprFromString(arg.expr, state.rootPath(".")));
v->mkString(((std::string_view) i.second).substr(1)); },
res.insert(state.symbols.create(i.first), v); [&](const AutoArgString & arg) {
v->mkString(arg.s);
}
}, arg);
res.insert(state.symbols.create(name), v);
} }
return res.finish(); return res.finish();
} }

View file

@ -26,7 +26,12 @@ struct MixEvalArgs : virtual Args, virtual MixRepair
std::optional<std::string> evalStoreUrl; std::optional<std::string> evalStoreUrl;
private: private:
std::map<std::string, std::string> autoArgs; struct AutoArgExpr { std::string expr; };
struct AutoArgString { std::string s; };
using AutoArg = std::variant<AutoArgExpr, AutoArgString>;
std::map<std::string, AutoArg> autoArgs;
}; };
SourcePath lookupFileArg(EvalState & state, std::string_view s, const Path * baseDir = nullptr); SourcePath lookupFileArg(EvalState & state, std::string_view s, const Path * baseDir = nullptr);