aboutsummaryrefslogtreecommitdiff
path: root/toolsrc/src/VcpkgCmdArguments.cpp
blob: cb261930e6754120244a72cbf88e4d78c4c785ce (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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
#include "pch.h"

#include "VcpkgCmdArguments.h"
#include "metrics.h"
#include "vcpkg_Commands.h"
#include "vcpkg_GlobalState.h"
#include "vcpkg_System.h"

namespace vcpkg
{
    static void parse_value(const std::string* arg_begin,
                            const std::string* arg_end,
                            const std::string& option_name,
                            std::unique_ptr<std::string>& option_field)
    {
        if (arg_begin == arg_end)
        {
            System::println(System::Color::error, "Error: expected value after %s", option_name);
            Metrics::g_metrics.lock()->track_property("error", "error option name");
            Commands::Help::print_usage();
            Checks::exit_fail(VCPKG_LINE_INFO);
        }

        if (option_field != nullptr)
        {
            System::println(System::Color::error, "Error: %s specified multiple times", option_name);
            Metrics::g_metrics.lock()->track_property("error", "error option specified multiple times");
            Commands::Help::print_usage();
            Checks::exit_fail(VCPKG_LINE_INFO);
        }

        option_field = std::make_unique<std::string>(*arg_begin);
    }

    static void parse_switch(bool new_setting, const std::string& option_name, Optional<bool>& option_field)
    {
        if (option_field && option_field != new_setting)
        {
            System::println(System::Color::error, "Error: conflicting values specified for --%s", option_name);
            Metrics::g_metrics.lock()->track_property("error", "error conflicting switches");
            Commands::Help::print_usage();
            Checks::exit_fail(VCPKG_LINE_INFO);
        }
        option_field = new_setting;
    }

    VcpkgCmdArguments VcpkgCmdArguments::create_from_command_line(const int argc, const wchar_t* const* const argv)
    {
        std::vector<std::string> v;
        for (int i = 1; i < argc; ++i)
        {
            v.push_back(Strings::to_utf8(argv[i]));
        }

        return VcpkgCmdArguments::create_from_arg_sequence(v.data(), v.data() + v.size());
    }

    VcpkgCmdArguments VcpkgCmdArguments::create_from_arg_sequence(const std::string* arg_begin,
                                                                  const std::string* arg_end)
    {
        VcpkgCmdArguments args;

        for (; arg_begin != arg_end; ++arg_begin)
        {
            std::string arg = *arg_begin;

            if (arg.empty())
            {
                continue;
            }

            if (arg[0] == '-' && arg[1] != '-')
            {
                Metrics::g_metrics.lock()->track_property("error", "error short options are not supported");
                Checks::exit_with_message(VCPKG_LINE_INFO, "Error: short options are not supported: %s", arg);
            }

            if (arg[0] == '-' && arg[1] == '-')
            {
                // make argument case insensitive
                auto& f = std::use_facet<std::ctype<char>>(std::locale());
                f.tolower(&arg[0], &arg[0] + arg.size());
                // command switch
                if (arg == "--vcpkg-root")
                {
                    ++arg_begin;
                    parse_value(arg_begin, arg_end, "--vcpkg-root", args.vcpkg_root_dir);
                    continue;
                }
                if (arg == "--triplet")
                {
                    ++arg_begin;
                    parse_value(arg_begin, arg_end, "--triplet", args.triplet);
                    continue;
                }
                if (arg == "--debug")
                {
                    parse_switch(true, "debug", args.debug);
                    continue;
                }
                if (arg == "--sendmetrics")
                {
                    parse_switch(true, "sendmetrics", args.sendmetrics);
                    continue;
                }
                if (arg == "--printmetrics")
                {
                    parse_switch(true, "printmetrics", args.printmetrics);
                    continue;
                }
                if (arg == "--no-sendmetrics")
                {
                    parse_switch(false, "sendmetrics", args.sendmetrics);
                    continue;
                }
                if (arg == "--no-printmetrics")
                {
                    parse_switch(false, "printmetrics", args.printmetrics);
                    continue;
                }
                if (arg == "--featurepackages")
                {
                    GlobalState::feature_packages = true;
                    continue;
                }

                const auto eq_pos = arg.find('=');
                if (eq_pos != std::string::npos)
                {
                    args.optional_command_arguments.emplace(arg.substr(0, eq_pos), arg.substr(eq_pos + 1));
                }
                else
                {
                    args.optional_command_arguments.emplace(arg, nullopt);
                }
                continue;
            }

            if (args.command.empty())
            {
                args.command = arg;
            }
            else
            {
                args.command_arguments.push_back(arg);
            }
        }

        return args;
    }

    ParsedArguments VcpkgCmdArguments::check_and_get_optional_command_arguments(
        const std::vector<std::string>& valid_switches, const std::vector<std::string>& valid_settings) const
    {
        bool failed = false;
        ParsedArguments output;

        auto options_copy = this->optional_command_arguments;
        for (const std::string& option : valid_switches)
        {
            const auto it = options_copy.find(option);
            if (it != options_copy.end())
            {
                if (it->second.has_value())
                {
                    // Having a string value indicates it was passed like '--a=xyz'
                    System::println(System::Color::error, "The option '%s' does not accept an argument.", option);
                    failed = true;
                }
                else
                {
                    output.switches.insert(option);
                    options_copy.erase(it);
                }
            }
        }

        for (const std::string& option : valid_settings)
        {
            const auto it = options_copy.find(option);
            if (it != options_copy.end())
            {
                if (!it->second.has_value())
                {
                    // Not having a string value indicates it was passed like '--a'
                    System::println(System::Color::error, "The option '%s' must be passed an argument.", option);
                    failed = true;
                }
                else
                {
                    output.settings.emplace(option, it->second.value_or_exit(VCPKG_LINE_INFO));
                    options_copy.erase(it);
                }
            }
        }

        if (!options_copy.empty())
        {
            System::println(System::Color::error, "Unknown option(s) for command '%s':", this->command);
            for (auto&& option : options_copy)
            {
                System::println("    %s", option.first);
            }
            System::println("\nValid options are:", this->command);
            for (auto&& option : valid_switches)
            {
                System::println("    %s", option);
            }
            for (auto&& option : valid_settings)
            {
                System::println("    %s=...", option);
            }
            System::println("    --triplet <t>");
            System::println("    --vcpkg-root <path>");

            Checks::exit_fail(VCPKG_LINE_INFO);
        }
        if (failed) Checks::exit_fail(VCPKG_LINE_INFO);

        return output;
    }

    void VcpkgCmdArguments::check_max_arg_count(const size_t expected_arg_count) const
    {
        return check_max_arg_count(expected_arg_count, Strings::EMPTY);
    }

    void VcpkgCmdArguments::check_min_arg_count(const size_t expected_arg_count) const
    {
        return check_min_arg_count(expected_arg_count, Strings::EMPTY);
    }

    void VcpkgCmdArguments::check_exact_arg_count(const size_t expected_arg_count) const
    {
        return check_exact_arg_count(expected_arg_count, Strings::EMPTY);
    }

    void VcpkgCmdArguments::check_max_arg_count(const size_t expected_arg_count, const std::string& example_text) const
    {
        const size_t actual_arg_count = command_arguments.size();
        if (actual_arg_count > expected_arg_count)
        {
            System::println(System::Color::error,
                            "Error: `%s` requires at most %u arguments, but %u were provided",
                            this->command,
                            expected_arg_count,
                            actual_arg_count);
            System::print(example_text);
            Checks::exit_fail(VCPKG_LINE_INFO);
        }
    }

    void VcpkgCmdArguments::check_min_arg_count(const size_t expected_arg_count, const std::string& example_text) const
    {
        const size_t actual_arg_count = command_arguments.size();
        if (actual_arg_count < expected_arg_count)
        {
            System::println(System::Color::error,
                            "Error: `%s` requires at least %u arguments, but %u were provided",
                            this->command,
                            expected_arg_count,
                            actual_arg_count);
            System::print(example_text);
            Checks::exit_fail(VCPKG_LINE_INFO);
        }
    }

    void VcpkgCmdArguments::check_exact_arg_count(const size_t expected_arg_count,
                                                  const std::string& example_text) const
    {
        const size_t actual_arg_count = command_arguments.size();
        if (actual_arg_count != expected_arg_count)
        {
            System::println(System::Color::error,
                            "Error: `%s` requires %u arguments, but %u were provided",
                            this->command,
                            expected_arg_count,
                            actual_arg_count);
            System::print(example_text);
            Checks::exit_fail(VCPKG_LINE_INFO);
        }
    }
}