hydra-eval-jobs: Modernize argument parser

This commit is contained in:
Eelco Dolstra 2019-05-10 22:05:49 +02:00
parent 8f3114960c
commit 2c60019910
No known key found for this signature in database
GPG key ID: 8170B4726D7198DE

View file

@ -180,25 +180,36 @@ int main(int argc, char * * argv)
initNix(); initNix();
initGC(); initGC();
Path releaseExpr; struct MyArgs : MixEvalArgs, MixCommonArgs
struct MyArgs : LegacyArgs, MixEvalArgs
{ {
using LegacyArgs::LegacyArgs; Path releaseExpr;
MyArgs() : MixCommonArgs("hydra-eval-jobs")
{
mkFlag()
.longName("help")
.description("show usage information")
.handler([&]() {
printHelp(programName, std::cout);
throw Exit();
});
mkFlag()
.longName("gc-roots-dir")
.description("garbage collector roots directory")
.labels({"path"})
.dest(&gcRootsDir);
mkFlag()
.longName("dry-run")
.description("don't create store derivations")
.set(&settings.readOnlyMode, true);
expectArg("expr", &releaseExpr);
}
}; };
MyArgs myArgs(baseNameOf(argv[0]), [&](Strings::iterator & arg, const Strings::iterator & end) { MyArgs myArgs;
if (*arg == "--gc-roots-dir")
gcRootsDir = getArg(*arg, arg, end);
else if (*arg == "--dry-run")
settings.readOnlyMode = true;
else if (*arg != "" && arg->at(0) == '-')
return false;
else
releaseExpr = *arg;
return true;
});
myArgs.parseCmdline(argvToStrings(argc, argv)); myArgs.parseCmdline(argvToStrings(argc, argv));
JSONObject json(std::cout, true); JSONObject json(std::cout, true);
@ -211,7 +222,7 @@ int main(int argc, char * * argv)
to the environment. */ to the environment. */
evalSettings.restrictEval = true; evalSettings.restrictEval = true;
if (releaseExpr == "") throw UsageError("no expression specified"); if (myArgs.releaseExpr == "") throw UsageError("no expression specified");
if (gcRootsDir == "") printMsg(lvlError, "warning: `--gc-roots-dir' not specified"); if (gcRootsDir == "") printMsg(lvlError, "warning: `--gc-roots-dir' not specified");
@ -220,7 +231,7 @@ int main(int argc, char * * argv)
Bindings & autoArgs = *myArgs.getAutoArgs(state); Bindings & autoArgs = *myArgs.getAutoArgs(state);
Value v; Value v;
state.evalFile(lookupFileArg(state, releaseExpr), v); state.evalFile(lookupFileArg(state, myArgs.releaseExpr), v);
findJobs(state, json, autoArgs, v, ""); findJobs(state, json, autoArgs, v, "");