From 0884f180f5ca8a864e6db5256eaa10646e87d671 Mon Sep 17 00:00:00 2001 From: Eelco Dolstra Date: Sun, 10 May 2020 21:50:32 +0200 Subject: [PATCH] Simplify --- src/libfetchers/registry.cc | 2 +- src/libmain/common-args.cc | 16 +++++++++------- src/libutil/args.cc | 28 +++++++++++----------------- src/libutil/util.cc | 2 +- src/libutil/util.hh | 2 +- 5 files changed, 23 insertions(+), 27 deletions(-) diff --git a/src/libfetchers/registry.cc b/src/libfetchers/registry.cc index 77d3b3378..f6760d2d0 100644 --- a/src/libfetchers/registry.cc +++ b/src/libfetchers/registry.cc @@ -155,7 +155,7 @@ void overrideRegistry( static std::shared_ptr getGlobalRegistry(ref store) { static auto reg = [&]() { - auto path = settings.flakeRegistry; + auto path = settings.flakeRegistry.get(); if (!hasPrefix(path, "/")) { auto storePath = downloadFile(store, path, "flake-registry.json", false).storePath; diff --git a/src/libmain/common-args.cc b/src/libmain/common-args.cc index f3f508ff4..51a61f1ca 100644 --- a/src/libmain/common-args.cc +++ b/src/libmain/common-args.cc @@ -31,19 +31,21 @@ MixCommonArgs::MixCommonArgs(const string & programName) .labels = {"name", "value"}, .handler = {[](std::string name, std::string value) { try { - if (auto prefix = needsCompletion(name)) { - std::map settings; - globalConfig.getSettings(settings); - for (auto & s : settings) - if (hasPrefix(s.first, *prefix)) - completions->insert(s.first); - } globalConfig.set(name, value); } catch (UsageError & e) { if (!completions) warn(e.what()); } }}, + .completer = [](size_t index, std::string_view prefix) { + if (index == 0) { + std::map settings; + globalConfig.getSettings(settings); + for (auto & s : settings) + if (hasPrefix(s.first, prefix)) + completions->insert(s.first); + } + } }); addFlag({ diff --git a/src/libutil/args.cc b/src/libutil/args.cc index 320b1b4b2..257b4b6c4 100644 --- a/src/libutil/args.cc +++ b/src/libutil/args.cc @@ -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 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); + } }; } diff --git a/src/libutil/util.cc b/src/libutil/util.cc index 4c8e2b26d..f2782ce69 100644 --- a/src/libutil/util.cc +++ b/src/libutil/util.cc @@ -1330,7 +1330,7 @@ bool statusOk(int status) } -bool hasPrefix(const string & s, const string & prefix) +bool hasPrefix(std::string_view s, std::string_view prefix) { return s.compare(0, prefix.size(), prefix) == 0; } diff --git a/src/libutil/util.hh b/src/libutil/util.hh index 4be1d4580..a861d5aa6 100644 --- a/src/libutil/util.hh +++ b/src/libutil/util.hh @@ -431,7 +431,7 @@ template bool string2Float(const string & s, N & n) /* Return true iff `s' starts with `prefix'. */ -bool hasPrefix(const string & s, const string & prefix); +bool hasPrefix(std::string_view s, std::string_view prefix); /* Return true iff `s' ends in `suffix'. */