aboutsummaryrefslogtreecommitdiff
path: root/src/libutil/args.cc
diff options
context:
space:
mode:
authorEelco Dolstra <edolstra@gmail.com>2020-06-26 08:46:46 +0200
committerEelco Dolstra <edolstra@gmail.com>2020-06-26 08:46:46 +0200
commitadf2fbbdc2c94644b0d1023d844c7dc0e485a20f (patch)
tree5999d2c223cb24402995747d79a3de6d6aaebb73 /src/libutil/args.cc
parent09fc06daab280735dd2ec94276f00a9c5bffd9b2 (diff)
parentb7ccf7ae2af3d7eaf3696358ecbbce5a2bcfe652 (diff)
Merge remote-tracking branch 'origin/master' into flakes
Diffstat (limited to 'src/libutil/args.cc')
-rw-r--r--src/libutil/args.cc28
1 files changed, 21 insertions, 7 deletions
diff --git a/src/libutil/args.cc b/src/libutil/args.cc
index e0dfc841f..b16a2e213 100644
--- a/src/libutil/args.cc
+++ b/src/libutil/args.cc
@@ -201,6 +201,13 @@ bool Args::processArgs(const Strings & args, bool finish)
return res;
}
+static void hashTypeCompleter(size_t index, std::string_view prefix)
+{
+ for (auto & type : hashTypes)
+ if (hasPrefix(type, prefix))
+ completions->insert(type);
+}
+
Args::Flag Args::Flag::mkHashTypeFlag(std::string && longName, HashType * ht)
{
return Flag {
@@ -209,14 +216,21 @@ Args::Flag Args::Flag::mkHashTypeFlag(std::string && longName, HashType * ht)
.labels = {"hash-algo"},
.handler = {[ht](std::string s) {
*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);
- }
+ .completer = hashTypeCompleter
+ };
+}
+
+Args::Flag Args::Flag::mkHashTypeOptFlag(std::string && longName, std::optional<HashType> * oht)
+{
+ return Flag {
+ .longName = std::move(longName),
+ .description = "hash algorithm ('md5', 'sha1', 'sha256', or 'sha512'). Optional as can also be gotten from SRI hash itself.",
+ .labels = {"hash-algo"},
+ .handler = {[oht](std::string s) {
+ *oht = std::optional<HashType> { parseHashType(s) };
+ }},
+ .completer = hashTypeCompleter
};
}