Read-only mirrors. Development, issues and pull requests are on GitHub.

summaryrefslogtreecommitdiffhomepage
path: root/Sources/CommandLine.cpp
blob: 67288d99c1d193b22898a5c49d501e06c4681684 (plain)
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
// SPDX-FileCopyrightText: 2026 Progmasoft <support@progmasoft.com>
// SPDX-License-Identifier: MPL-2.0 WITH AdditionRef-Progmasoft-Exception-2.0

#include <array>
#include <fmt/format.h>
#include <utility>

#include "Progmasoft/Tazelish/CommandLine.hpp"

namespace Progmasoft::Tazelish
{
    namespace
    {

        constexpr std::array<std::pair<std::string_view, Topic>, 5> kTopics
            = { {
                { "install", Topic::kInstall },
                { "use", Topic::kUse },
                { "default", Topic::kDefault },
                { "self", Topic::kSelf },
                { "help", Topic::kHelp },
            } };

        constexpr std::string_view kVersionForms
            = "MAJOR.SEMIMAJOR.MINOR.PATCH, unstable, nightly, "
              "YYYY-MM-DD-unstable or "
              "YYYY-MM-DD-nightly";

        Rejection
        Reject(std::string message)
        {
            return Rejection{ std::move(message) };
        }

        // The one version of a command, written as the argument [text].
        std::variant<Version, Rejection>
        ReadVersion(std::string_view command, std::string_view text)
        {
            if (auto version = ParseVersion(text))
            {
                return *version;
            }
            return Reject(
                fmt::format("'{}' is not a version; tazelish {} takes {}",
                            text,
                            command,
                            kVersionForms));
        }

        // `install <version>`.
        ParseResult
        ParseInstall(const std::vector<std::string_view> &rest)
        {
            if (rest.size() != 1)
            {
                return Reject("tazelish install takes one version, such as "
                              "tazelish install 0.4.0.3");
            }
            auto version = ReadVersion("install", rest.front());
            if (auto *rejection = std::get_if<Rejection>(&version))
            {
                return std::move(*rejection);
            }
            return Command{ Install{ std::get<Version>(version) } };
        }

        // `use|default [--force] <version>`, the flag before or after the
        // version.
        template<typename Writing>
        ParseResult
        ParseWriting(std::string_view command,
                     const std::vector<std::string_view> &rest)
        {
            bool force = false;
            std::optional<std::string_view> text;
            for (const std::string_view argument : rest)
            {
                if (argument == "--force")
                {
                    if (force)
                    {
                        return Reject(fmt::format(
                            "--force is written twice in tazelish {}",
                            command));
                    }
                    force = true;
                }
                else if (argument.size() > 1 && argument.front() == '-')
                {
                    return Reject(fmt::format("'{}' is not an option of "
                                              "tazelish {}; it takes --force",
                                              argument,
                                              command));
                }
                else if (text)
                {
                    return Reject(
                        fmt::format("tazelish {} takes one version", command));
                }
                else
                {
                    text = argument;
                }
            }
            if (!text)
            {
                return Reject(fmt::format(
                    "tazelish {} takes a version, such as tazelish {} 0.4.0.3",
                    command,
                    command));
            }
            auto version = ReadVersion(command, *text);
            if (auto *rejection = std::get_if<Rejection>(&version))
            {
                return std::move(*rejection);
            }
            return Command{ Writing{ std::get<Version>(version), force } };
        }

        // `self update|remove|purge`.
        ParseResult
        ParseSelf(const std::vector<std::string_view> &rest)
        {
            if (rest.size() == 1)
            {
                if (rest.front() == "update")
                {
                    return Command{ Self{ SelfAction::kUpdate } };
                }
                if (rest.front() == "remove")
                {
                    return Command{ Self{ SelfAction::kRemove } };
                }
                if (rest.front() == "purge")
                {
                    return Command{ Self{ SelfAction::kPurge } };
                }
            }
            return Reject("tazelish self takes one of update, remove or purge");
        }

        // `help [command]`.
        ParseResult
        ParseHelp(const std::vector<std::string_view> &rest)
        {
            if (rest.empty())
            {
                return Command{ Help{} };
            }
            if (rest.size() == 1)
            {
                for (const auto &[name, topic] : kTopics)
                {
                    if (rest.front() == name)
                    {
                        return Command{ Help{ topic } };
                    }
                }
                return Reject(
                    fmt::format("'{}' is not a command of tazelish; write "
                                "tazelish help for the commands",
                                rest.front()));
            }
            return Reject("tazelish help takes at most one command");
        }

    } // namespace

    ParseResult
    ParseCommandLine(const std::vector<std::string_view> &arguments)
    {
        if (arguments.empty())
        {
            return Command{ Help{} };
        }
        const std::string_view command = arguments.front();
        const std::vector<std::string_view> rest(arguments.begin() + 1,
                                                 arguments.end());
        if (command == "install")
        {
            return ParseInstall(rest);
        }
        if (command == "use")
        {
            return ParseWriting<Use>(command, rest);
        }
        if (command == "default")
        {
            return ParseWriting<Default>(command, rest);
        }
        if (command == "self")
        {
            return ParseSelf(rest);
        }
        if (command == "help")
        {
            return ParseHelp(rest);
        }
        return Reject(fmt::format(
            "'{}' is not a command of tazelish; write tazelish help for the "
            "commands",
            command));
    }

    std::string_view
    TopicName(Topic topic)
    {
        for (const auto &[name, candidate] : kTopics)
        {
            if (candidate == topic)
            {
                return name;
            }
        }
        return {};
    }

} // namespace Progmasoft::Tazelish