diff options
author | Eelco Dolstra <eelco.dolstra@logicblox.com> | 2016-02-09T20·07+0100 |
---|---|---|
committer | Eelco Dolstra <eelco.dolstra@logicblox.com> | 2016-02-09T20·07+0100 |
commit | 0db9e6cd1af299f7d65e0b99019f0ccdbb1aaf3f (patch) | |
tree | 78e8c206bef0ac59146157c1b35b5c92af0bb819 /src/libmain | |
parent | c780c1124ec6711f09b9855c3b574b6655af6625 (diff) |
New command line parsing infrastructure
Diffstat (limited to 'src/libmain')
-rw-r--r-- | src/libmain/common-args.cc | 37 | ||||
-rw-r--r-- | src/libmain/common-args.hh | 22 | ||||
-rw-r--r-- | src/libmain/shared.cc | 148 | ||||
-rw-r--r-- | src/libmain/shared.hh | 3 |
4 files changed, 131 insertions, 79 deletions
diff --git a/src/libmain/common-args.cc b/src/libmain/common-args.cc new file mode 100644 index 000000000000..54dbb1da3ca8 --- /dev/null +++ b/src/libmain/common-args.cc @@ -0,0 +1,37 @@ +#include "common-args.hh" +#include "globals.hh" + +namespace nix { + +MixCommonArgs::MixCommonArgs(const string & programName) +{ + mkFlag('v', "verbose", "increase verbosity level", []() { + verbosity = (Verbosity) (verbosity + 1); + }); + + mkFlag(0, "quiet", "decrease verbosity level", []() { + verbosity = verbosity > lvlError ? (Verbosity) (verbosity - 1) : lvlError; + }); + + mkFlag(0, "debug", "enable debug output", []() { + verbosity = lvlDebug; + }); + + mkFlag1(0, "log-type", "type", "set logging format ('pretty', 'flat', 'systemd')", + [](std::string s) { + if (s == "pretty") logType = ltPretty; + else if (s == "escapes") logType = ltEscapes; + else if (s == "flat") logType = ltFlat; + else if (s == "systemd") logType = ltSystemd; + else throw UsageError("unknown log type"); + }); + + mkFlag(0, "option", {"name", "value"}, "set a Nix configuration option (overriding nix.conf)", 2, + [](Strings ss) { + auto name = ss.front(); ss.pop_front(); + auto value = ss.front(); + settings.set(name, value); + }); +} + +} diff --git a/src/libmain/common-args.hh b/src/libmain/common-args.hh new file mode 100644 index 000000000000..62657e0d0218 --- /dev/null +++ b/src/libmain/common-args.hh @@ -0,0 +1,22 @@ +#pragma once + +#include "args.hh" + +namespace nix { + +struct MixCommonArgs : virtual Args +{ + MixCommonArgs(const string & programName); +}; + +struct MixDryRun : virtual Args +{ + bool dryRun; + + MixDryRun() + { + mkFlag(0, "dry-run", "show what this command would do without doing it", &dryRun); + } +}; + +} diff --git a/src/libmain/shared.cc b/src/libmain/shared.cc index 88ed52497fb9..bf6bf5b712e7 100644 --- a/src/libmain/shared.cc +++ b/src/libmain/shared.cc @@ -1,7 +1,8 @@ #include "config.h" -#include "shared.hh" +#include "common-args.hh" #include "globals.hh" +#include "shared.hh" #include "store-api.hh" #include "util.hh" @@ -84,16 +85,6 @@ void printMissing(ref<Store> store, const PathSet & willBuild, } -static void setLogType(string lt) -{ - if (lt == "pretty") logType = ltPretty; - else if (lt == "escapes") logType = ltEscapes; - else if (lt == "flat") logType = ltFlat; - else if (lt == "systemd") logType = ltSystemd; - else throw UsageError("unknown log type"); -} - - string getArg(const string & opt, Strings::iterator & i, const Strings::iterator & end) { @@ -164,77 +155,80 @@ void initNix() } -void parseCmdLine(int argc, char * * argv, - std::function<bool(Strings::iterator & arg, const Strings::iterator & end)> parseArg) +struct LegacyArgs : public MixCommonArgs { - /* Put the arguments in a vector. */ - Strings args; - argc--; argv++; - while (argc--) args.push_back(*argv++); - - /* Process default options. */ - for (Strings::iterator i = args.begin(); i != args.end(); ++i) { - string arg = *i; - - /* Expand compound dash options (i.e., `-qlf' -> `-q -l -f'). */ - if (arg.length() > 2 && arg[0] == '-' && arg[1] != '-' && isalpha(arg[1])) { - *i = (string) "-" + arg[1]; - auto next = i; ++next; - for (unsigned int j = 2; j < arg.length(); j++) - if (isalpha(arg[j])) - args.insert(next, (string) "-" + arg[j]); - else { - args.insert(next, string(arg, j)); - break; - } - arg = *i; - } + std::function<bool(Strings::iterator & arg, const Strings::iterator & end)> parseArg; - if (arg == "--verbose" || arg == "-v") verbosity = (Verbosity) (verbosity + 1); - else if (arg == "--quiet") verbosity = verbosity > lvlError ? (Verbosity) (verbosity - 1) : lvlError; - else if (arg == "--log-type") { - string s = getArg(arg, i, args.end()); - setLogType(s); - } - else if (arg == "--no-build-output" || arg == "-Q") - settings.buildVerbosity = lvlVomit; - else if (arg == "--print-build-trace") - settings.printBuildTrace = true; - else if (arg == "--keep-failed" || arg == "-K") - settings.keepFailed = true; - else if (arg == "--keep-going" || arg == "-k") - settings.keepGoing = true; - else if (arg == "--fallback") + LegacyArgs(const std::string & programName, + std::function<bool(Strings::iterator & arg, const Strings::iterator & end)> parseArg) + : MixCommonArgs(programName), parseArg(parseArg) + { + mkFlag('Q', "no-build-output", "do not show build output", + &settings.buildVerbosity, lvlVomit); + + mkFlag(0, "print-build-trace", "emit special build trace message", + &settings.printBuildTrace); + + mkFlag('K', "keep-failed", "keep temporary directories of failed builds", + &settings.keepFailed); + + mkFlag('k', "keep-going", "keep going after a build fails", + &settings.keepGoing); + + mkFlag(0, "fallback", "build from source if substitution fails", []() { settings.set("build-fallback", "true"); - else if (arg == "--max-jobs" || arg == "-j") - settings.set("build-max-jobs", getArg(arg, i, args.end())); - else if (arg == "--cores") - settings.set("build-cores", getArg(arg, i, args.end())); - else if (arg == "--readonly-mode") - settings.readOnlyMode = true; - else if (arg == "--max-silent-time") - settings.set("build-max-silent-time", getArg(arg, i, args.end())); - else if (arg == "--timeout") - settings.set("build-timeout", getArg(arg, i, args.end())); - else if (arg == "--no-build-hook") - settings.useBuildHook = false; - else if (arg == "--show-trace") - settings.showTrace = true; - else if (arg == "--no-gc-warning") - gcWarning = false; - else if (arg == "--option") { - ++i; if (i == args.end()) throw UsageError("‘--option’ requires two arguments"); - string name = *i; - ++i; if (i == args.end()) throw UsageError("‘--option’ requires two arguments"); - string value = *i; - settings.set(name, value); - } - else { - if (!parseArg(i, args.end())) - throw UsageError(format("unrecognised option ‘%1%’") % *i); - } + }); + + auto intSettingAlias = [&](char shortName, const std::string & longName, + const std::string & description, const std::string & dest) { + mkFlag<unsigned int>(shortName, longName, description, [=](unsigned int n) { + settings.set(dest, std::to_string(n)); + }); + }; + + intSettingAlias('j', "max-jobs", "maximum number of parallel builds", "build-max-jobs"); + intSettingAlias(0, "cores", "maximum number of CPU cores to use inside a build", "build-cores"); + intSettingAlias(0, "max-silent-time", "number of seconds of silence before a build is killed", "build-max-silent-time"); + intSettingAlias(0, "timeout", "number of seconds before a build is killed", "build-timeout"); + + mkFlag(0, "readonly-mode", "do not write to the Nix store", + &settings.readOnlyMode); + + mkFlag(0, "no-build-hook", "disable use of the build hook mechanism", + &settings.useBuildHook, 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 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; + } +}; + + +void parseCmdLine(int argc, char * * argv, + std::function<bool(Strings::iterator & arg, const Strings::iterator & end)> parseArg) +{ + LegacyArgs(baseNameOf(argv[0]), parseArg).parseCmdline(argvToStrings(argc, argv)); settings.update(); } diff --git a/src/libmain/shared.hh b/src/libmain/shared.hh index e4b984f067e4..20744be0ab9b 100644 --- a/src/libmain/shared.hh +++ b/src/libmain/shared.hh @@ -1,6 +1,7 @@ #pragma once #include "util.hh" +#include "args.hh" #include <signal.h> @@ -9,8 +10,6 @@ namespace nix { -MakeError(UsageError, nix::Error); - class Exit : public std::exception { public: |