Flag: Use designated initializers

This commit is contained in:
Eelco Dolstra 2020-05-04 22:40:19 +02:00
parent e9f10beed1
commit a721a0b114
22 changed files with 406 additions and 362 deletions

View file

@ -10,24 +10,27 @@ namespace nix {
MixEvalArgs::MixEvalArgs() MixEvalArgs::MixEvalArgs()
{ {
mkFlag() addFlag({
.longName("arg") .longName = "arg",
.description("argument to be passed to Nix functions") .description = "argument to be passed to Nix functions",
.labels({"name", "expr"}) .labels = {"name", "expr"},
.handler([&](std::vector<std::string> ss) { autoArgs[ss[0]] = 'E' + ss[1]; }); .handler = {[&](std::string name, std::string expr) { autoArgs[name] = 'E' + expr; }}
});
mkFlag() addFlag({
.longName("argstr") .longName = "argstr",
.description("string-valued argument to be passed to Nix functions") .description = "string-valued argument to be passed to Nix functions",
.labels({"name", "string"}) .labels = {"name", "string"},
.handler([&](std::vector<std::string> ss) { autoArgs[ss[0]] = 'S' + ss[1]; }); .handler = {[&](std::string name, std::string s) { autoArgs[name] = 'S' + s; }},
});
mkFlag() addFlag({
.shortName('I') .longName = "include",
.longName("include") .shortName = 'I',
.description("add a path to the list of locations used to look up <...> file names") .description = "add a path to the list of locations used to look up <...> file names",
.label("path") .labels = {"path"},
.handler([&](std::string s) { searchPath.push_back(s); }); .handler = {[&](std::string s) { searchPath.push_back(s); }}
});
} }
Bindings * MixEvalArgs::getAutoArgs(EvalState & state) Bindings * MixEvalArgs::getAutoArgs(EvalState & state)

View file

@ -6,43 +6,47 @@ namespace nix {
MixCommonArgs::MixCommonArgs(const string & programName) MixCommonArgs::MixCommonArgs(const string & programName)
: programName(programName) : programName(programName)
{ {
mkFlag() addFlag({
.longName("verbose") .longName = "verbose",
.shortName('v') .shortName = 'v',
.description("increase verbosity level") .description = "increase verbosity level",
.handler([]() { verbosity = (Verbosity) (verbosity + 1); }); .handler = {[]() { verbosity = (Verbosity) (verbosity + 1); }},
});
mkFlag() addFlag({
.longName("quiet") .longName = "quiet",
.description("decrease verbosity level") .description = "decrease verbosity level",
.handler([]() { verbosity = verbosity > lvlError ? (Verbosity) (verbosity - 1) : lvlError; }); .handler = {[]() { verbosity = verbosity > lvlError ? (Verbosity) (verbosity - 1) : lvlError; }},
});
mkFlag() addFlag({
.longName("debug") .longName = "debug",
.description("enable debug output") .description = "enable debug output",
.handler([]() { verbosity = lvlDebug; }); .handler = {[]() { verbosity = lvlDebug; }},
});
mkFlag() addFlag({
.longName("option") .longName = "option",
.labels({"name", "value"}) .description = "set a Nix configuration option (overriding nix.conf)",
.description("set a Nix configuration option (overriding nix.conf)") .labels = {"name", "value"},
.arity(2) .handler = {[](std::string name, std::string value) {
.handler([](std::vector<std::string> ss) {
try { try {
globalConfig.set(ss[0], ss[1]); globalConfig.set(name, value);
} catch (UsageError & e) { } catch (UsageError & e) {
warn(e.what()); warn(e.what());
} }
}); }},
});
mkFlag() addFlag({
.longName("max-jobs") .longName = "max-jobs",
.shortName('j') .shortName = 'j',
.label("jobs") .description = "maximum number of parallel builds",
.description("maximum number of parallel builds") .labels = Strings{"jobs"},
.handler([=](std::string s) { .handler = {[=](std::string s) {
settings.set("max-jobs", s); settings.set("max-jobs", s);
}); }}
});
std::string cat = "config"; std::string cat = "config";
globalConfig.convertToArgs(*this, cat); globalConfig.convertToArgs(*this, cat);

View file

@ -165,28 +165,32 @@ LegacyArgs::LegacyArgs(const std::string & programName,
std::function<bool(Strings::iterator & arg, const Strings::iterator & end)> parseArg) std::function<bool(Strings::iterator & arg, const Strings::iterator & end)> parseArg)
: MixCommonArgs(programName), parseArg(parseArg) : MixCommonArgs(programName), parseArg(parseArg)
{ {
mkFlag() addFlag({
.longName("no-build-output") .longName = "no-build-output",
.shortName('Q') .shortName = 'Q',
.description("do not show build output") .description = "do not show build output",
.set(&settings.verboseBuild, false); .handler = {&settings.verboseBuild, false},
});
mkFlag() addFlag({
.longName("keep-failed") .longName = "keep-failed",
.shortName('K') .shortName ='K',
.description("keep temporary directories of failed builds") .description = "keep temporary directories of failed builds",
.set(&(bool&) settings.keepFailed, true); .handler = {&(bool&) settings.keepFailed, true},
});
mkFlag() addFlag({
.longName("keep-going") .longName = "keep-going",
.shortName('k') .shortName ='k',
.description("keep going after a build fails") .description = "keep going after a build fails",
.set(&(bool&) settings.keepGoing, true); .handler = {&(bool&) settings.keepGoing, true},
});
mkFlag() addFlag({
.longName("fallback") .longName = "fallback",
.description("build from source if substitution fails") .description = "build from source if substitution fails",
.set(&(bool&) settings.tryFallback, true); .handler = {&(bool&) settings.tryFallback, true},
});
auto intSettingAlias = [&](char shortName, const std::string & longName, auto intSettingAlias = [&](char shortName, const std::string & longName,
const std::string & description, const std::string & dest) { const std::string & description, const std::string & dest) {
@ -205,11 +209,12 @@ LegacyArgs::LegacyArgs(const std::string & programName,
mkFlag(0, "no-gc-warning", "disable warning about not using '--add-root'", mkFlag(0, "no-gc-warning", "disable warning about not using '--add-root'",
&gcWarning, false); &gcWarning, false);
mkFlag() addFlag({
.longName("store") .longName = "store",
.label("store-uri") .description = "URI of the Nix store to use",
.description("URI of the Nix store to use") .labels = {"store-uri"},
.dest(&(std::string&) settings.storeUri); .handler = {&(std::string&) settings.storeUri},
});
} }

View file

@ -167,21 +167,24 @@ template<> void BaseSetting<SandboxMode>::toJSON(JSONPlaceholder & out)
template<> void BaseSetting<SandboxMode>::convertToArg(Args & args, const std::string & category) template<> void BaseSetting<SandboxMode>::convertToArg(Args & args, const std::string & category)
{ {
args.mkFlag() args.addFlag({
.longName(name) .longName = name,
.description("Enable sandboxing.") .description = "Enable sandboxing.",
.handler([=](std::vector<std::string> ss) { override(smEnabled); }) .category = category,
.category(category); .handler = {[=]() { override(smEnabled); }}
args.mkFlag() });
.longName("no-" + name) args.addFlag({
.description("Disable sandboxing.") .longName = "no-" + name,
.handler([=](std::vector<std::string> ss) { override(smDisabled); }) .description = "Disable sandboxing.",
.category(category); .category = category,
args.mkFlag() .handler = {[=]() { override(smDisabled); }}
.longName("relaxed-" + name) });
.description("Enable sandboxing, but allow builds to disable it.") args.addFlag({
.handler([=](std::vector<std::string> ss) { override(smRelaxed); }) .longName = "relaxed-" + name,
.category(category); .description = "Enable sandboxing, but allow builds to disable it.",
.category = category,
.handler = {[=]() { override(smRelaxed); }}
});
} }
void MaxBuildJobsSetting::set(const std::string & str) void MaxBuildJobsSetting::set(const std::string & str)

View file

@ -3,16 +3,14 @@
namespace nix { namespace nix {
Args::FlagMaker Args::mkFlag() void Args::addFlag(Flag && flag_)
{
return FlagMaker(*this);
}
Args::FlagMaker::~FlagMaker()
{ {
auto flag = std::make_shared<Flag>(std::move(flag_));
if (flag->handler.arity != ArityAny)
assert(flag->handler.arity == flag->labels.size());
assert(flag->longName != ""); assert(flag->longName != "");
args.longFlags[flag->longName] = flag; longFlags[flag->longName] = flag;
if (flag->shortName) args.shortFlags[flag->shortName] = flag; if (flag->shortName) shortFlags[flag->shortName] = flag;
} }
void Args::parseCmdline(const Strings & _cmdline) void Args::parseCmdline(const Strings & _cmdline)
@ -101,15 +99,14 @@ bool Args::processFlag(Strings::iterator & pos, Strings::iterator end)
auto process = [&](const std::string & name, const Flag & flag) -> bool { auto process = [&](const std::string & name, const Flag & flag) -> bool {
++pos; ++pos;
std::vector<std::string> args; std::vector<std::string> args;
for (size_t n = 0 ; n < flag.arity; ++n) { for (size_t n = 0 ; n < flag.handler.arity; ++n) {
if (pos == end) { if (pos == end) {
if (flag.arity == ArityAny) break; if (flag.handler.arity == ArityAny) break;
throw UsageError(format("flag '%1%' requires %2% argument(s)") throw UsageError("flag '%s' requires %d argument(s)", name, flag.handler.arity);
% name % flag.arity);
} }
args.push_back(*pos++); args.push_back(*pos++);
} }
flag.handler(std::move(args)); flag.handler.fun(std::move(args));
return true; return true;
}; };
@ -157,17 +154,18 @@ bool Args::processArgs(const Strings & args, bool finish)
return res; return res;
} }
Args::FlagMaker & Args::FlagMaker::mkHashTypeFlag(HashType * ht) Args::Flag Args::Flag::mkHashTypeFlag(std::string && longName, HashType * ht)
{ {
arity(1); return Flag {
label("type"); .longName = std::move(longName),
description("hash algorithm ('md5', 'sha1', 'sha256', or 'sha512')"); .description = "hash algorithm ('md5', 'sha1', 'sha256', or 'sha512')",
handler([ht](std::string s) { .labels = {"hash-algo"},
*ht = parseHashType(s); .handler = {[ht](std::string s) {
if (*ht == htUnknown) *ht = parseHashType(s);
throw UsageError("unknown hash type '%1%'", s); if (*ht == htUnknown)
}); throw UsageError("unknown hash type '%1%'", s);
return *this; }}
};
} }
Strings argvToStrings(int argc, char * * argv) Strings argvToStrings(int argc, char * * argv)

View file

@ -32,13 +32,59 @@ protected:
struct Flag struct Flag
{ {
typedef std::shared_ptr<Flag> ptr; typedef std::shared_ptr<Flag> ptr;
struct Handler
{
std::function<void(std::vector<std::string>)> fun;
size_t arity;
Handler() {}
Handler(std::function<void(std::vector<std::string>)> && fun)
: fun(std::move(fun))
, arity(ArityAny)
{ }
Handler(std::function<void()> && handler)
: fun([handler{std::move(handler)}](std::vector<std::string>) { handler(); })
, arity(0)
{ }
Handler(std::function<void(std::string)> && handler)
: fun([handler{std::move(handler)}](std::vector<std::string> ss) {
handler(std::move(ss[0]));
})
, arity(1)
{ }
Handler(std::function<void(std::string, std::string)> && handler)
: fun([handler{std::move(handler)}](std::vector<std::string> ss) {
handler(std::move(ss[0]), std::move(ss[1]));
})
, arity(2)
{ }
template<class T>
Handler(T * dest)
: fun([=](std::vector<std::string> ss) { *dest = ss[0]; })
, arity(1)
{ }
template<class T>
Handler(T * dest, const T & val)
: fun([=](std::vector<std::string> ss) { *dest = val; })
, arity(0)
{ }
};
std::string longName; std::string longName;
char shortName = 0; char shortName = 0;
std::string description; std::string description;
Strings labels;
size_t arity = 0;
std::function<void(std::vector<std::string>)> handler;
std::string category; std::string category;
Strings labels;
Handler handler;
static Flag mkHashTypeFlag(std::string && longName, HashType * ht);
}; };
std::map<std::string, Flag::ptr> longFlags; std::map<std::string, Flag::ptr> longFlags;
@ -65,49 +111,7 @@ protected:
public: public:
class FlagMaker void addFlag(Flag && flag);
{
Args & args;
Flag::ptr flag;
friend class Args;
FlagMaker(Args & args) : args(args), flag(std::make_shared<Flag>()) { }
public:
~FlagMaker();
FlagMaker & longName(const std::string & s) { flag->longName = s; return *this; }
FlagMaker & shortName(char s) { flag->shortName = s; return *this; }
FlagMaker & description(const std::string & s) { flag->description = s; return *this; }
FlagMaker & label(const std::string & l) { flag->arity = 1; flag->labels = {l}; return *this; }
FlagMaker & labels(const Strings & ls) { flag->arity = ls.size(); flag->labels = ls; return *this; }
FlagMaker & arity(size_t arity) { flag->arity = arity; return *this; }
FlagMaker & handler(std::function<void(std::vector<std::string>)> handler) { flag->handler = handler; return *this; }
FlagMaker & handler(std::function<void()> handler) { flag->handler = [handler](std::vector<std::string>) { handler(); }; return *this; }
FlagMaker & handler(std::function<void(std::string)> handler) {
flag->arity = 1;
flag->handler = [handler](std::vector<std::string> ss) { handler(std::move(ss[0])); };
return *this;
}
FlagMaker & category(const std::string & s) { flag->category = s; return *this; }
template<class T>
FlagMaker & dest(T * dest)
{
flag->arity = 1;
flag->handler = [=](std::vector<std::string> ss) { *dest = ss[0]; };
return *this;
}
template<class T>
FlagMaker & set(T * dest, const T & val)
{
flag->arity = 0;
flag->handler = [=](std::vector<std::string> ss) { *dest = val; };
return *this;
}
FlagMaker & mkHashTypeFlag(HashType * ht);
};
FlagMaker mkFlag();
/* Helper functions for constructing flags / positional /* Helper functions for constructing flags / positional
arguments. */ arguments. */
@ -116,13 +120,13 @@ public:
const std::string & label, const std::string & description, const std::string & label, const std::string & description,
std::function<void(std::string)> fun) std::function<void(std::string)> fun)
{ {
mkFlag() addFlag({
.shortName(shortName) .longName = longName,
.longName(longName) .shortName = shortName,
.labels({label}) .description = description,
.description(description) .labels = {label},
.arity(1) .handler = {[=](std::string s) { fun(s); }}
.handler([=](std::vector<std::string> ss) { fun(ss[0]); }); });
} }
void mkFlag(char shortName, const std::string & name, void mkFlag(char shortName, const std::string & name,
@ -135,11 +139,12 @@ public:
void mkFlag(char shortName, const std::string & longName, const std::string & description, void mkFlag(char shortName, const std::string & longName, const std::string & description,
T * dest, const T & value) T * dest, const T & value)
{ {
mkFlag() addFlag({
.shortName(shortName) .longName = longName,
.longName(longName) .shortName = shortName,
.description(description) .description = description,
.handler([=](std::vector<std::string> ss) { *dest = value; }); .handler = {[=]() { *dest = value; }}
});
} }
template<class I> template<class I>
@ -155,18 +160,18 @@ public:
void mkFlag(char shortName, const std::string & longName, void mkFlag(char shortName, const std::string & longName,
const std::string & description, std::function<void(I)> fun) const std::string & description, std::function<void(I)> fun)
{ {
mkFlag() addFlag({
.shortName(shortName) .longName = longName,
.longName(longName) .shortName = shortName,
.labels({"N"}) .description = description,
.description(description) .labels = {"N"},
.arity(1) .handler = {[=](std::string s) {
.handler([=](std::vector<std::string> ss) {
I n; I n;
if (!string2Int(ss[0], n)) if (!string2Int(s, n))
throw UsageError("flag '--%s' requires a integer argument", longName); throw UsageError("flag '--%s' requires a integer argument", longName);
fun(n); fun(n);
}); }}
});
} }
/* Expect a string argument. */ /* Expect a string argument. */

View file

@ -177,12 +177,13 @@ void BaseSetting<T>::toJSON(JSONPlaceholder & out)
template<typename T> template<typename T>
void BaseSetting<T>::convertToArg(Args & args, const std::string & category) void BaseSetting<T>::convertToArg(Args & args, const std::string & category)
{ {
args.mkFlag() args.addFlag({
.longName(name) .longName = name,
.description(description) .description = description,
.arity(1) .category = category,
.handler([=](std::vector<std::string> ss) { overriden = true; set(ss[0]); }) .labels = {"value"},
.category(category); .handler = {[=](std::string s) { overriden = true; set(s); }},
});
} }
template<> void BaseSetting<std::string>::set(const std::string & str) template<> void BaseSetting<std::string>::set(const std::string & str)
@ -227,16 +228,18 @@ template<> std::string BaseSetting<bool>::to_string() const
template<> void BaseSetting<bool>::convertToArg(Args & args, const std::string & category) template<> void BaseSetting<bool>::convertToArg(Args & args, const std::string & category)
{ {
args.mkFlag() args.addFlag({
.longName(name) .longName = name,
.description(description) .description = description,
.handler([=](std::vector<std::string> ss) { override(true); }) .category = category,
.category(category); .handler = {[=]() { override(true); }}
args.mkFlag() });
.longName("no-" + name) args.addFlag({
.description(description) .longName = "no-" + name,
.handler([=](std::vector<std::string> ss) { override(false); }) .description = description,
.category(category); .category = category,
.handler = {[=]() { override(false); }}
});
} }
template<> void BaseSetting<Strings>::set(const std::string & str) template<> void BaseSetting<Strings>::set(const std::string & str)

View file

@ -14,12 +14,13 @@ struct CmdAddToStore : MixDryRun, StoreCommand
{ {
expectArg("path", &path); expectArg("path", &path);
mkFlag() addFlag({
.longName("name") .longName = "name",
.shortName('n') .shortName = 'n',
.description("name component of the store path") .description = "name component of the store path",
.labels({"name"}) .labels = {"name"},
.dest(&namePart); .handler = {&namePart},
});
} }
std::string description() override std::string description() override

View file

@ -11,17 +11,19 @@ struct CmdBuild : InstallablesCommand, MixDryRun, MixProfile
CmdBuild() CmdBuild()
{ {
mkFlag() addFlag({
.longName("out-link") .longName = "out-link",
.shortName('o') .shortName = 'o',
.description("path of the symlink to the build result") .description = "path of the symlink to the build result",
.labels({"path"}) .labels = {"path"},
.dest(&outLink); .handler = {&outLink},
});
mkFlag() addFlag({
.longName("no-link") .longName = "no-link",
.description("do not create a symlink to the build result") .description = "do not create a symlink to the build result",
.set(&outLink, Path("")); .handler = {&outLink, Path("")},
});
} }
std::string description() override std::string description() override

View file

@ -35,16 +35,18 @@ StorePathsCommand::StorePathsCommand(bool recursive)
: recursive(recursive) : recursive(recursive)
{ {
if (recursive) if (recursive)
mkFlag() addFlag({
.longName("no-recursive") .longName = "no-recursive",
.description("apply operation to specified paths only") .description = "apply operation to specified paths only",
.set(&this->recursive, false); .handler = {&this->recursive, false},
});
else else
mkFlag() addFlag({
.longName("recursive") .longName = "recursive",
.shortName('r') .shortName = 'r',
.description("apply operation to closure of the specified paths") .description = "apply operation to closure of the specified paths",
.set(&this->recursive, true); .handler = {&this->recursive, true},
});
mkFlag(0, "all", "apply operation to the entire store", &all); mkFlag(0, "all", "apply operation to the entire store", &all);
} }
@ -101,11 +103,12 @@ Strings editorFor(const Pos & pos)
MixProfile::MixProfile() MixProfile::MixProfile()
{ {
mkFlag() addFlag({
.longName("profile") .longName = "profile",
.description("profile to update") .description = "profile to update",
.labels({"path"}) .labels = {"path"},
.dest(&profile); .handler = {&profile},
});
} }
void MixProfile::updateProfile(const StorePath & storePath) void MixProfile::updateProfile(const StorePath & storePath)
@ -145,28 +148,30 @@ MixDefaultProfile::MixDefaultProfile()
profile = getDefaultProfile(); profile = getDefaultProfile();
} }
MixEnvironment::MixEnvironment() : ignoreEnvironment(false) { MixEnvironment::MixEnvironment() : ignoreEnvironment(false)
mkFlag() {
.longName("ignore-environment") addFlag({
.shortName('i') .longName = "ignore-environment",
.description("clear the entire environment (except those specified with --keep)") .shortName = 'i',
.set(&ignoreEnvironment, true); .description = "clear the entire environment (except those specified with --keep)",
.handler = {&ignoreEnvironment, true},
});
mkFlag() addFlag({
.longName("keep") .longName = "keep",
.shortName('k') .shortName = 'k',
.description("keep specified environment variable") .description = "keep specified environment variable",
.arity(1) .labels = {"name"},
.labels({"name"}) .handler = {[&](std::string s) { keep.insert(s); }},
.handler([&](std::vector<std::string> ss) { keep.insert(ss.front()); }); });
mkFlag() addFlag({
.longName("unset") .longName = "unset",
.shortName('u') .shortName = 'u',
.description("unset specified environment variable") .description = "unset specified environment variable",
.arity(1) .labels = {"name"},
.labels({"name"}) .handler = {[&](std::string s) { unset.insert(s); }},
.handler([&](std::vector<std::string> ss) { unset.insert(ss.front()); }); });
} }
void MixEnvironment::setEnviron() { void MixEnvironment::setEnviron() {

View file

@ -19,27 +19,32 @@ struct CmdCopy : StorePathsCommand
CmdCopy() CmdCopy()
: StorePathsCommand(true) : StorePathsCommand(true)
{ {
mkFlag() addFlag({
.longName("from") .longName = "from",
.labels({"store-uri"}) .description = "URI of the source Nix store",
.description("URI of the source Nix store") .labels = {"store-uri"},
.dest(&srcUri); .handler = {&srcUri},
mkFlag() });
.longName("to")
.labels({"store-uri"})
.description("URI of the destination Nix store")
.dest(&dstUri);
mkFlag() addFlag({
.longName("no-check-sigs") .longName = "to",
.description("do not require that paths are signed by trusted keys") .description = "URI of the destination Nix store",
.set(&checkSigs, NoCheckSigs); .labels = {"store-uri"},
.handler = {&dstUri},
});
mkFlag() addFlag({
.longName("substitute-on-destination") .longName = "no-check-sigs",
.shortName('s') .description = "do not require that paths are signed by trusted keys",
.description("whether to try substitutes on the destination store (only supported by SSH)") .handler = {&checkSigs, NoCheckSigs},
.set(&substitute, Substitute); });
addFlag({
.longName = "substitute-on-destination",
.shortName = 's',
.description = "whether to try substitutes on the destination store (only supported by SSH)",
.handler = {&substitute, Substitute},
});
} }
std::string description() override std::string description() override

View file

@ -237,16 +237,16 @@ struct CmdDevShell : Common, MixEnvironment
CmdDevShell() CmdDevShell()
{ {
mkFlag() addFlag({
.longName("command") .longName = "command",
.shortName('c') .shortName = 'c',
.description("command and arguments to be executed insted of an interactive shell") .description = "command and arguments to be executed insted of an interactive shell",
.labels({"command", "args"}) .labels = {"command", "args"},
.arity(ArityAny) .handler = {[&](std::vector<std::string> ss) {
.handler([&](std::vector<std::string> ss) {
if (ss.empty()) throw UsageError("--command requires at least one argument"); if (ss.empty()) throw UsageError("--command requires at least one argument");
command = ss; command = ss;
}); }}
});
} }
std::string description() override std::string description() override

View file

@ -23,9 +23,7 @@ struct CmdHash : Command
mkFlag(0, "base64", "print hash in base-64", &base, Base64); mkFlag(0, "base64", "print hash in base-64", &base, Base64);
mkFlag(0, "base32", "print hash in base-32 (Nix-specific)", &base, Base32); mkFlag(0, "base32", "print hash in base-32 (Nix-specific)", &base, Base32);
mkFlag(0, "base16", "print hash in base-16", &base, Base16); mkFlag(0, "base16", "print hash in base-16", &base, Base16);
mkFlag() addFlag(Flag::mkHashTypeFlag("type", &ht));
.longName("type")
.mkHashTypeFlag(&ht);
#if 0 #if 0
mkFlag() mkFlag()
.longName("modulo") .longName("modulo")
@ -76,9 +74,7 @@ struct CmdToBase : Command
CmdToBase(Base base) : base(base) CmdToBase(Base base) : base(base)
{ {
mkFlag() addFlag(Flag::mkHashTypeFlag("type", &ht));
.longName("type")
.mkHashTypeFlag(&ht);
expectArgs("strings", &args); expectArgs("strings", &args);
} }

View file

@ -15,12 +15,13 @@ namespace nix {
SourceExprCommand::SourceExprCommand() SourceExprCommand::SourceExprCommand()
{ {
mkFlag() addFlag({
.shortName('f') .longName = "file",
.longName("file") .shortName = 'f',
.label("file") .description = "evaluate FILE rather than the default",
.description("evaluate FILE rather than the default") .labels = {"file"},
.dest(&file); .handler = {&file}
});
} }
Value * SourceExprCommand::getSourceExpr(EvalState & state) Value * SourceExprCommand::getSourceExpr(EvalState & state)

View file

@ -59,15 +59,16 @@ struct NixArgs : virtual MultiCommand, virtual MixCommonArgs
NixArgs() : MultiCommand(*RegisterCommand::commands), MixCommonArgs("nix") NixArgs() : MultiCommand(*RegisterCommand::commands), MixCommonArgs("nix")
{ {
mkFlag() addFlag({
.longName("help") .longName = "help",
.description("show usage information") .description = "show usage information",
.handler([&]() { showHelpAndExit(); }); .handler = {[&]() { showHelpAndExit(); }},
});
mkFlag() addFlag({
.longName("help-config") .longName = "help-config",
.description("show configuration options") .description = "show configuration options",
.handler([&]() { .handler = {[&]() {
std::cout << "The following configuration options are available:\n\n"; std::cout << "The following configuration options are available:\n\n";
Table2 tbl; Table2 tbl;
std::map<std::string, Config::SettingInfo> settings; std::map<std::string, Config::SettingInfo> settings;
@ -76,28 +77,33 @@ struct NixArgs : virtual MultiCommand, virtual MixCommonArgs
tbl.emplace_back(s.first, s.second.description); tbl.emplace_back(s.first, s.second.description);
printTable(std::cout, tbl); printTable(std::cout, tbl);
throw Exit(); throw Exit();
}); }},
});
mkFlag() addFlag({
.longName("print-build-logs") .longName = "print-build-logs",
.shortName('L') .shortName = 'L',
.description("print full build logs on stderr") .description = "print full build logs on stderr",
.set(&printBuildLogs, true); .handler = {&printBuildLogs, true},
});
mkFlag() addFlag({
.longName("version") .longName = "version",
.description("show version information") .description = "show version information",
.handler([&]() { printVersion(programName); }); .handler = {[&]() { printVersion(programName); }},
});
mkFlag() addFlag({
.longName("no-net") .longName = "no-net",
.description("disable substituters and consider all previously downloaded files up-to-date") .description = "disable substituters and consider all previously downloaded files up-to-date",
.handler([&]() { useNet = false; }); .handler = {[&]() { useNet = false; }},
});
mkFlag() addFlag({
.longName("refresh") .longName = "refresh",
.description("consider all previously downloaded files out-of-date") .description = "consider all previously downloaded files out-of-date",
.handler([&]() { refresh = true; }); .handler = {[&]() { refresh = true; }},
});
} }
void printFlags(std::ostream & out) override void printFlags(std::ostream & out) override

View file

@ -63,16 +63,16 @@ struct CmdShell : InstallablesCommand, RunCommon, MixEnvironment
CmdShell() CmdShell()
{ {
mkFlag() addFlag({
.longName("command") .longName = "command",
.shortName('c') .shortName = 'c',
.description("command and arguments to be executed; defaults to '$SHELL'") .description = "command and arguments to be executed; defaults to '$SHELL'",
.labels({"command", "args"}) .labels = {"command", "args"},
.arity(ArityAny) .handler = {[&](std::vector<std::string> ss) {
.handler([&](std::vector<std::string> ss) {
if (ss.empty()) throw UsageError("--command requires at least one argument"); if (ss.empty()) throw UsageError("--command requires at least one argument");
command = ss; command = ss;
}); }}
});
} }
std::string description() override std::string description() override

View file

@ -40,16 +40,18 @@ struct CmdSearch : SourceExprCommand, MixJSON
{ {
expectArgs("regex", &res); expectArgs("regex", &res);
mkFlag() addFlag({
.longName("update-cache") .longName = "update-cache",
.shortName('u') .shortName = 'u',
.description("update the package search cache") .description = "update the package search cache",
.handler([&]() { writeCache = true; useCache = false; }); .handler = {[&]() { writeCache = true; useCache = false; }}
});
mkFlag() addFlag({
.longName("no-cache") .longName = "no-cache",
.description("do not use or update the package search cache") .description = "do not use or update the package search cache",
.handler([&]() { writeCache = false; useCache = false; }); .handler = {[&]() { writeCache = false; useCache = false; }}
});
} }
std::string description() override std::string description() override

View file

@ -15,11 +15,12 @@ struct CmdShowDerivation : InstallablesCommand
CmdShowDerivation() CmdShowDerivation()
{ {
mkFlag() addFlag({
.longName("recursive") .longName = "recursive",
.shortName('r') .shortName = 'r',
.description("include the dependencies of the specified derivations") .description = "include the dependencies of the specified derivations",
.set(&recursive, true); .handler = {&recursive, true}
});
} }
std::string description() override std::string description() override

View file

@ -13,13 +13,13 @@ struct CmdCopySigs : StorePathsCommand
CmdCopySigs() CmdCopySigs()
{ {
mkFlag() addFlag({
.longName("substituter") .longName = "substituter",
.shortName('s') .shortName = 's',
.labels({"store-uri"}) .description = "use signatures from specified store",
.description("use signatures from specified store") .labels = {"store-uri"},
.arity(1) .handler = {[&](std::string s) { substituterUris.push_back(s); }},
.handler([&](std::vector<std::string> ss) { substituterUris.push_back(ss[0]); }); });
} }
std::string description() override std::string description() override
@ -98,12 +98,13 @@ struct CmdSignPaths : StorePathsCommand
CmdSignPaths() CmdSignPaths()
{ {
mkFlag() addFlag({
.shortName('k') .longName = "key-file",
.longName("key-file") .shortName = 'k',
.label("file") .description = "file containing the secret signing key",
.description("file containing the secret signing key") .labels = {"file"},
.dest(&secretKeyFile); .handler = {&secretKeyFile}
});
} }
std::string description() override std::string description() override

View file

@ -16,18 +16,20 @@ struct CmdUpgradeNix : MixDryRun, StoreCommand
CmdUpgradeNix() CmdUpgradeNix()
{ {
mkFlag() addFlag({
.longName("profile") .longName = "profile",
.shortName('p') .shortName = 'p',
.labels({"profile-dir"}) .description = "the Nix profile to upgrade",
.description("the Nix profile to upgrade") .labels = {"profile-dir"},
.dest(&profileDir); .handler = {&profileDir}
});
mkFlag() addFlag({
.longName("nix-store-paths-url") .longName = "nix-store-paths-url",
.labels({"url"}) .description = "URL of the file that contains the store paths of the latest Nix release",
.description("URL of the file that contains the store paths of the latest Nix release") .labels = {"url"},
.dest(&storePathsUrl); .handler = {&storePathsUrl}
});
} }
std::string description() override std::string description() override

View file

@ -20,13 +20,13 @@ struct CmdVerify : StorePathsCommand
{ {
mkFlag(0, "no-contents", "do not verify the contents of each store path", &noContents); mkFlag(0, "no-contents", "do not verify the contents of each store path", &noContents);
mkFlag(0, "no-trust", "do not verify whether each store path is trusted", &noTrust); mkFlag(0, "no-trust", "do not verify whether each store path is trusted", &noTrust);
mkFlag() addFlag({
.longName("substituter") .longName = "substituter",
.shortName('s') .shortName = 's',
.labels({"store-uri"}) .description = "use signatures from specified store",
.description("use signatures from specified store") .labels = {"store-uri"},
.arity(1) .handler = {[&](std::string s) { substituterUris.push_back(s); }}
.handler([&](std::vector<std::string> ss) { substituterUris.push_back(ss[0]); }); });
mkIntFlag('n', "sigs-needed", "require that each path has at least N valid signatures", &sigsNeeded); mkIntFlag('n', "sigs-needed", "require that each path has at least N valid signatures", &sigsNeeded);
} }

View file

@ -37,11 +37,12 @@ struct CmdWhyDepends : SourceExprCommand
expectArg("package", &_package); expectArg("package", &_package);
expectArg("dependency", &_dependency); expectArg("dependency", &_dependency);
mkFlag() addFlag({
.longName("all") .longName = "all",
.shortName('a') .shortName = 'a',
.description("show all edges in the dependency graph leading from 'package' to 'dependency', rather than just a shortest path") .description = "show all edges in the dependency graph leading from 'package' to 'dependency', rather than just a shortest path",
.set(&all, true); .handler = {&all, true},
});
} }
std::string description() override std::string description() override