mirror of
https://github.com/NixOS/nix
synced 2025-06-28 17:51:15 +02:00
Simplify
This commit is contained in:
parent
e0c19ee620
commit
0884f180f5
5 changed files with 23 additions and 27 deletions
|
@ -122,22 +122,15 @@ bool Args::processFlag(Strings::iterator & pos, Strings::iterator end)
|
|||
auto process = [&](const std::string & name, const Flag & flag) -> bool {
|
||||
++pos;
|
||||
std::vector<std::string> args;
|
||||
bool anyNeedsCompletion = false;
|
||||
for (size_t n = 0 ; n < flag.handler.arity; ++n) {
|
||||
if (pos == end) {
|
||||
if (flag.handler.arity == ArityAny) break;
|
||||
if (anyNeedsCompletion)
|
||||
args.push_back("");
|
||||
else
|
||||
throw UsageError("flag '%s' requires %d argument(s)", name, flag.handler.arity);
|
||||
} else {
|
||||
if (needsCompletion(*pos)) {
|
||||
if (flag.completer)
|
||||
flag.completer(n, *pos);
|
||||
anyNeedsCompletion = true;
|
||||
}
|
||||
args.push_back(*pos++);
|
||||
throw UsageError("flag '%s' requires %d argument(s)", name, flag.handler.arity);
|
||||
}
|
||||
if (auto prefix = needsCompletion(*pos))
|
||||
if (flag.completer)
|
||||
flag.completer(n, *prefix);
|
||||
args.push_back(*pos++);
|
||||
}
|
||||
flag.handler.fun(std::move(args));
|
||||
return true;
|
||||
|
@ -209,14 +202,15 @@ Args::Flag Args::Flag::mkHashTypeFlag(std::string && longName, HashType * ht)
|
|||
.description = "hash algorithm ('md5', 'sha1', 'sha256', or 'sha512')",
|
||||
.labels = {"hash-algo"},
|
||||
.handler = {[ht](std::string s) {
|
||||
if (auto prefix = needsCompletion(s))
|
||||
for (auto & type : hashTypes)
|
||||
if (hasPrefix(type, *prefix))
|
||||
completions->insert(type);
|
||||
*ht = parseHashType(s);
|
||||
if (*ht == htUnknown)
|
||||
throw UsageError("unknown hash type '%1%'", s);
|
||||
}}
|
||||
}},
|
||||
.completer = [](size_t index, std::string_view prefix) {
|
||||
for (auto & type : hashTypes)
|
||||
if (hasPrefix(type, prefix))
|
||||
completions->insert(type);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue