This commit is contained in:
Eelco Dolstra 2020-05-10 21:50:32 +02:00
parent e0c19ee620
commit 0884f180f5
5 changed files with 23 additions and 27 deletions

View file

@ -155,7 +155,7 @@ void overrideRegistry(
static std::shared_ptr<Registry> getGlobalRegistry(ref<Store> store) static std::shared_ptr<Registry> getGlobalRegistry(ref<Store> store)
{ {
static auto reg = [&]() { static auto reg = [&]() {
auto path = settings.flakeRegistry; auto path = settings.flakeRegistry.get();
if (!hasPrefix(path, "/")) { if (!hasPrefix(path, "/")) {
auto storePath = downloadFile(store, path, "flake-registry.json", false).storePath; auto storePath = downloadFile(store, path, "flake-registry.json", false).storePath;

View file

@ -31,19 +31,21 @@ MixCommonArgs::MixCommonArgs(const string & programName)
.labels = {"name", "value"}, .labels = {"name", "value"},
.handler = {[](std::string name, std::string value) { .handler = {[](std::string name, std::string value) {
try { try {
if (auto prefix = needsCompletion(name)) {
std::map<std::string, Config::SettingInfo> settings;
globalConfig.getSettings(settings);
for (auto & s : settings)
if (hasPrefix(s.first, *prefix))
completions->insert(s.first);
}
globalConfig.set(name, value); globalConfig.set(name, value);
} catch (UsageError & e) { } catch (UsageError & e) {
if (!completions) if (!completions)
warn(e.what()); warn(e.what());
} }
}}, }},
.completer = [](size_t index, std::string_view prefix) {
if (index == 0) {
std::map<std::string, Config::SettingInfo> settings;
globalConfig.getSettings(settings);
for (auto & s : settings)
if (hasPrefix(s.first, prefix))
completions->insert(s.first);
}
}
}); });
addFlag({ addFlag({

View file

@ -122,23 +122,16 @@ 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;
bool anyNeedsCompletion = false;
for (size_t n = 0 ; n < flag.handler.arity; ++n) { for (size_t n = 0 ; n < flag.handler.arity; ++n) {
if (pos == end) { if (pos == end) {
if (flag.handler.arity == ArityAny) break; if (flag.handler.arity == ArityAny) break;
if (anyNeedsCompletion)
args.push_back("");
else
throw UsageError("flag '%s' requires %d argument(s)", name, flag.handler.arity); throw UsageError("flag '%s' requires %d argument(s)", name, flag.handler.arity);
} else { }
if (needsCompletion(*pos)) { if (auto prefix = needsCompletion(*pos))
if (flag.completer) if (flag.completer)
flag.completer(n, *pos); flag.completer(n, *prefix);
anyNeedsCompletion = true;
}
args.push_back(*pos++); args.push_back(*pos++);
} }
}
flag.handler.fun(std::move(args)); flag.handler.fun(std::move(args));
return true; return true;
}; };
@ -209,14 +202,15 @@ Args::Flag Args::Flag::mkHashTypeFlag(std::string && longName, HashType * ht)
.description = "hash algorithm ('md5', 'sha1', 'sha256', or 'sha512')", .description = "hash algorithm ('md5', 'sha1', 'sha256', or 'sha512')",
.labels = {"hash-algo"}, .labels = {"hash-algo"},
.handler = {[ht](std::string s) { .handler = {[ht](std::string s) {
if (auto prefix = needsCompletion(s))
for (auto & type : hashTypes)
if (hasPrefix(type, *prefix))
completions->insert(type);
*ht = parseHashType(s); *ht = parseHashType(s);
if (*ht == htUnknown) if (*ht == htUnknown)
throw UsageError("unknown hash type '%1%'", s); 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);
}
}; };
} }

View file

@ -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; return s.compare(0, prefix.size(), prefix) == 0;
} }

View file

@ -431,7 +431,7 @@ template<class N> bool string2Float(const string & s, N & n)
/* Return true iff `s' starts with `prefix'. */ /* 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'. */ /* Return true iff `s' ends in `suffix'. */