From 0d59f1ca49c9f7f3b2edaadcf590360ec66a6257 Mon Sep 17 00:00:00 2001 From: Eelco Dolstra Date: Tue, 24 Oct 2017 12:45:11 +0200 Subject: nix: Respect -I, --arg, --argstr Also, random cleanup to argument handling. --- src/libmain/common-args.cc | 28 ++++++----- src/libmain/shared.cc | 116 ++++++++++++++++++++++++--------------------- src/libmain/shared.hh | 14 ++++++ 3 files changed, 90 insertions(+), 68 deletions(-) (limited to 'src/libmain') diff --git a/src/libmain/common-args.cc b/src/libmain/common-args.cc index 3fa42c2aafa9..ea27aaa35e03 100644 --- a/src/libmain/common-args.cc +++ b/src/libmain/common-args.cc @@ -6,28 +6,30 @@ namespace nix { MixCommonArgs::MixCommonArgs(const string & programName) : programName(programName) { - mkFlag('v', "verbose", "increase verbosity level", []() { - verbosity = (Verbosity) (verbosity + 1); - }); + mkFlag() + .longName("verbose") + .shortName('v') + .description("increase verbosity level") + .handler([]() { verbosity = (Verbosity) (verbosity + 1); }); - mkFlag(0, "quiet", "decrease verbosity level", []() { - verbosity = verbosity > lvlError ? (Verbosity) (verbosity - 1) : lvlError; - }); + mkFlag() + .longName("quiet") + .description("decrease verbosity level") + .handler([]() { verbosity = verbosity > lvlError ? (Verbosity) (verbosity - 1) : lvlError; }); - mkFlag(0, "debug", "enable debug output", []() { - verbosity = lvlDebug; - }); + mkFlag() + .longName("debug") + .description("enable debug output") + .handler([]() { verbosity = lvlDebug; }); mkFlag() .longName("option") .labels({"name", "value"}) .description("set a Nix configuration option (overriding nix.conf)") .arity(2) - .handler([](Strings ss) { - auto name = ss.front(); ss.pop_front(); - auto value = ss.front(); + .handler([](std::vector ss) { try { - settings.set(name, value); + settings.set(ss[0], ss[1]); } catch (UsageError & e) { warn(e.what()); } diff --git a/src/libmain/shared.cc b/src/libmain/shared.cc index aef5d88bf46f..9123a3d1ae04 100644 --- a/src/libmain/shared.cc +++ b/src/libmain/shared.cc @@ -1,4 +1,3 @@ -#include "common-args.hh" #include "globals.hh" #include "shared.hh" #include "store-api.hh" @@ -149,71 +148,78 @@ void initNix() } -struct LegacyArgs : public MixCommonArgs +LegacyArgs::LegacyArgs(const std::string & programName, + std::function parseArg) + : MixCommonArgs(programName), parseArg(parseArg) { - std::function parseArg; - - LegacyArgs(const std::string & programName, - std::function parseArg) - : MixCommonArgs(programName), parseArg(parseArg) - { - mkFlag('Q', "no-build-output", "do not show build output", - &settings.verboseBuild, false); - - mkFlag('K', "keep-failed", "keep temporary directories of failed builds", - &(bool&) settings.keepFailed); - - mkFlag('k', "keep-going", "keep going after a build fails", - &(bool&) settings.keepGoing); + mkFlag() + .longName("no-build-output") + .shortName('Q') + .description("do not show build output") + .set(&settings.verboseBuild, false); + + mkFlag() + .longName("keep-failed") + .shortName('K') + .description("keep temporary directories of failed builds") + .set(&(bool&) settings.keepFailed, true); + + mkFlag() + .longName("keep-going") + .shortName('k') + .description("keep going after a build fails") + .set(&(bool&) settings.keepGoing, true); + + mkFlag() + .longName("fallback") + .description("build from source if substitution fails") + .set(&(bool&) settings.tryFallback, true); + + mkFlag1('j', "max-jobs", "jobs", "maximum number of parallel builds", [=](std::string s) { + settings.set("max-jobs", s); + }); - mkFlag(0, "fallback", "build from source if substitution fails", []() { - settings.tryFallback = true; + auto intSettingAlias = [&](char shortName, const std::string & longName, + const std::string & description, const std::string & dest) { + mkFlag(shortName, longName, description, [=](unsigned int n) { + settings.set(dest, std::to_string(n)); }); + }; - mkFlag1('j', "max-jobs", "jobs", "maximum number of parallel builds", [=](std::string s) { - settings.set("max-jobs", s); - }); + intSettingAlias(0, "cores", "maximum number of CPU cores to use inside a build", "cores"); + intSettingAlias(0, "max-silent-time", "number of seconds of silence before a build is killed", "max-silent-time"); + intSettingAlias(0, "timeout", "number of seconds before a build is killed", "timeout"); - auto intSettingAlias = [&](char shortName, const std::string & longName, - const std::string & description, const std::string & dest) { - mkFlag(shortName, longName, description, [=](unsigned int n) { - settings.set(dest, std::to_string(n)); - }); - }; + mkFlag(0, "readonly-mode", "do not write to the Nix store", + &settings.readOnlyMode); - intSettingAlias(0, "cores", "maximum number of CPU cores to use inside a build", "cores"); - intSettingAlias(0, "max-silent-time", "number of seconds of silence before a build is killed", "max-silent-time"); - intSettingAlias(0, "timeout", "number of seconds before a build is killed", "timeout"); + mkFlag(0, "show-trace", "show Nix expression stack trace in evaluation errors", + &settings.showTrace); - mkFlag(0, "readonly-mode", "do not write to the Nix store", - &settings.readOnlyMode); + mkFlag(0, "no-gc-warning", "disable warning about not using '--add-root'", + &gcWarning, false); +} - mkFlag(0, "show-trace", "show Nix expression stack trace in evaluation errors", - &settings.showTrace); - mkFlag(0, "no-gc-warning", "disable warning about not using '--add-root'", - &gcWarning, false); - } +bool LegacyArgs::processFlag(Strings::iterator & pos, Strings::iterator end) +{ + if (MixCommonArgs::processFlag(pos, end)) return true; + bool res = parseArg(pos, end); + if (res) ++pos; + return res; +} - bool processFlag(Strings::iterator & pos, Strings::iterator end) override - { - if (MixCommonArgs::processFlag(pos, end)) return true; - bool res = parseArg(pos, end); - if (res) ++pos; - return res; - } - bool processArgs(const Strings & args, bool finish) override - { - if (args.empty()) return true; - assert(args.size() == 1); - Strings ss(args); - auto pos = ss.begin(); - if (!parseArg(pos, ss.end())) - throw UsageError(format("unexpected argument '%1%'") % args.front()); - return true; - } -}; +bool LegacyArgs::processArgs(const Strings & args, bool finish) +{ + if (args.empty()) return true; + assert(args.size() == 1); + Strings ss(args); + auto pos = ss.begin(); + if (!parseArg(pos, ss.end())) + throw UsageError(format("unexpected argument '%1%'") % args.front()); + return true; +} void parseCmdLine(int argc, char * * argv, diff --git a/src/libmain/shared.hh b/src/libmain/shared.hh index 2a1e42dd9774..9219dbed8325 100644 --- a/src/libmain/shared.hh +++ b/src/libmain/shared.hh @@ -2,6 +2,7 @@ #include "util.hh" #include "args.hh" +#include "common-args.hh" #include @@ -69,6 +70,19 @@ template N getIntArg(const string & opt, } +struct LegacyArgs : public MixCommonArgs +{ + std::function parseArg; + + LegacyArgs(const std::string & programName, + std::function parseArg); + + bool processFlag(Strings::iterator & pos, Strings::iterator end) override; + + bool processArgs(const Strings & args, bool finish) override; +}; + + /* Show the manual page for the specified program. */ void showManPage(const string & name); -- cgit 1.4.1