1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#pragma once
///@file

#include <string_view>
#include <type_traits>
#include <utility>
#include <optional>
#include <ranges>

#include "lix/libutil/args.hh"

namespace nix::cli {
template<typename Enum>
struct enum_cli_traits;

template<typename Enum>
constexpr std::string_view toString(Enum value)
{
    for (const auto & [name, val] : enum_cli_traits<Enum>::values) {
        if (val == value) {
            return name;
        }
    }
    std::terminate();
}

template<typename Enum>
std::optional<Enum> fromString(std::string_view str)
{
    for (const auto & [name, val] : enum_cli_traits<Enum>::values) {
        if (name == str) {
            return val;
        }
    }
    return std::nullopt;
}

template<typename Enum>
void completeAmongEnumChoices(AddCompletions & completions, size_t, std::string_view prefix)
{
    for (const auto & [name, _] : enum_cli_traits<Enum>::values) {
        if (name.starts_with(prefix)) {
            completions.add(name);
        }
    }
}

template<typename Enum>
Enum parseEnumArg(std::string text)
{
    auto valueOpt = fromString<Enum>(text);

    if (valueOpt) {
        return *valueOpt;
    } else {
        auto names = std::ranges::views::keys(enum_cli_traits<Enum>::values)
            | std::ranges::to<std::set<std::string>>();
        auto suggestions = Suggestions::bestMatches(names, text);
        throw UsageError(suggestions, "'%s' is not a recognised '%s'", text, enum_cli_traits<Enum>::typeName);
    }
}

template<typename Enum>
std::optional<Enum> parseOptionalEnumArg(std::string text)
{
    auto target = fromString<Enum>(text);

    if (!target && text != "") {
        auto names = std::ranges::views::keys(enum_cli_traits<Enum>::values) | std::ranges::to<std::set>();
        auto suggestions = Suggestions::bestMatches(names, text);
        throw UsageError(suggestions, "'%s' is not a recognised '%s'", text, enum_cli_traits<Enum>::typeName);
    }

    return target;
}
}