/src/WasmEdge/lib/po/argument_parser.cpp
Line | Count | Source |
1 | | // SPDX-License-Identifier: Apache-2.0 |
2 | | // SPDX-FileCopyrightText: Copyright The WasmEdge Authors |
3 | | |
4 | | #include "po/argument_parser.h" |
5 | | #include "common/defines.h" |
6 | | #include "common/spdlog.h" |
7 | | #include "system/winapi.h" |
8 | | #include <cstdio> |
9 | | |
10 | | namespace WasmEdge { |
11 | | namespace PO { |
12 | | |
13 | | cxx20::expected<bool, Error> ArgumentParser::SubCommandDescriptor::parse( |
14 | | std::FILE *Out, Span<const char *> ProgramNamePrefix, int Argc, |
15 | 1.61k | const char *Argv[], int ArgP, const bool &VersionOpt) noexcept { |
16 | 1.61k | ProgramNames.reserve(ProgramNamePrefix.size() + 1); |
17 | 1.61k | ProgramNames.assign(ProgramNamePrefix.begin(), ProgramNamePrefix.end()); |
18 | 1.61k | if (ArgP < Argc) { |
19 | 1.61k | ProgramNames.push_back(Argv[ArgP]); |
20 | 1.61k | } |
21 | 1.61k | ArgumentDescriptor *CurrentDesc = nullptr; |
22 | 1.61k | bool FirstNonOption = true; |
23 | 1.61k | bool Escaped = false; |
24 | 1.61k | auto PositionalIter = PositionalList.cbegin(); |
25 | 1.32M | for (int ArgI = ArgP + 1; ArgI < Argc; ++ArgI) { |
26 | 1.32M | std::string_view Arg = Argv[ArgI]; |
27 | 1.32M | if (!Escaped && Arg.size() >= 2 && Arg[0] == '-') { |
28 | 244k | if (Arg[1] == '-') { |
29 | 241k | if (Arg.size() == 2) { |
30 | 42 | Escaped = true; |
31 | 241k | } else { |
32 | | // long option |
33 | 241k | if (CurrentDesc && CurrentDesc->nargs() == 0) { |
34 | 1.94k | CurrentDesc->default_value(); |
35 | 1.94k | } |
36 | 241k | if (auto Res = consume_long_option_with_argument(Arg); !Res) { |
37 | 508 | return cxx20::unexpected(Res.error()); |
38 | 241k | } else { |
39 | 241k | CurrentDesc = *Res; |
40 | 241k | } |
41 | 241k | } |
42 | 241k | } else { |
43 | | // short options |
44 | 3.12k | if (CurrentDesc && CurrentDesc->nargs() == 0) { |
45 | 244 | CurrentDesc->default_value(); |
46 | 244 | } |
47 | 3.12k | if (auto Res = consume_short_options(Arg); !Res) { |
48 | 31 | return cxx20::unexpected(Res.error()); |
49 | 3.09k | } else { |
50 | 3.09k | CurrentDesc = *Res; |
51 | 3.09k | } |
52 | 3.12k | } |
53 | 1.07M | } else if (!Escaped && CurrentDesc) { |
54 | 677 | if (auto Res = consume_argument(*CurrentDesc, Arg); !Res) { |
55 | 16 | return cxx20::unexpected(Res.error()); |
56 | 16 | } |
57 | 661 | CurrentDesc = nullptr; |
58 | 1.07M | } else { |
59 | | // no more options |
60 | 1.07M | if (FirstNonOption) { |
61 | 496 | FirstNonOption = false; |
62 | 496 | if (!SubCommandMap.empty()) { |
63 | 0 | if (auto Iter = SubCommandMap.find(Arg); |
64 | 0 | Iter != SubCommandMap.end()) { |
65 | 0 | auto &Child = this[Iter->second]; |
66 | 0 | Child.SC->select(); |
67 | 0 | return Child.parse(Out, ProgramNames, Argc, Argv, ArgI, VersionOpt); |
68 | 0 | } |
69 | 0 | } |
70 | 496 | } |
71 | 1.07M | Escaped = true; |
72 | 1.07M | if (CurrentDesc) { |
73 | 1.07M | if (auto Res = consume_argument(*CurrentDesc, Arg); !Res) { |
74 | 7 | return cxx20::unexpected(Res.error()); |
75 | 1.07M | } else { |
76 | 1.07M | CurrentDesc = *Res; |
77 | 1.07M | } |
78 | 1.07M | } else { |
79 | 736 | if (PositionalIter == PositionalList.cend()) { |
80 | 0 | return cxx20::unexpected<Error>( |
81 | 0 | std::in_place, ErrCode::InvalidArgument, |
82 | 0 | "positional argument exceeds maximum consuming."s); |
83 | 0 | } |
84 | 736 | if (auto Res = |
85 | 736 | consume_argument(ArgumentDescriptors[*PositionalIter], Arg); |
86 | 736 | !Res) { |
87 | 0 | return cxx20::unexpected(Res.error()); |
88 | 736 | } else { |
89 | 736 | CurrentDesc = *Res; |
90 | 736 | } |
91 | 736 | ++PositionalIter; |
92 | 736 | } |
93 | 1.07M | } |
94 | 1.32M | } |
95 | 1.05k | if (CurrentDesc && CurrentDesc->nargs() == 0) { |
96 | 32 | CurrentDesc->default_value(); |
97 | 32 | } |
98 | | |
99 | 1.05k | if (VersionOpt) { |
100 | 200 | return true; |
101 | 200 | } |
102 | 851 | if (!HelpOpt->value()) { |
103 | 14.6k | for (const auto &Desc : ArgumentDescriptors) { |
104 | 14.6k | if (Desc.nargs() < Desc.min_nargs()) { |
105 | 368 | help(Out); |
106 | 368 | return false; |
107 | 368 | } |
108 | 14.6k | } |
109 | 804 | } else { |
110 | 47 | help(Out); |
111 | 47 | return true; |
112 | 47 | } |
113 | 436 | return true; |
114 | 851 | } |
115 | | |
116 | | void ArgumentParser::SubCommandDescriptor::usage( |
117 | 415 | std::FILE *Out) const noexcept { |
118 | 415 | fmt::print(Out, "{}USAGE{}\n"sv, YELLOW_COLOR, RESET_COLOR); |
119 | 415 | for (const char *Part : ProgramNames) { |
120 | 415 | fmt::print(Out, "\t{}"sv, Part); |
121 | 415 | } |
122 | 415 | if (!SubCommandList.empty()) { |
123 | 0 | fmt::print(Out, " [SUBCOMMANDS]"sv); |
124 | 0 | } |
125 | 415 | if (!NonpositionalList.empty()) { |
126 | 415 | fmt::print(Out, " [OPTIONS]"sv); |
127 | 415 | } |
128 | 415 | bool First = true; |
129 | 830 | for (const auto &Index : PositionalList) { |
130 | 830 | const auto &Desc = ArgumentDescriptors[Index]; |
131 | 830 | if (Desc.hidden()) { |
132 | 0 | continue; |
133 | 0 | } |
134 | | |
135 | 830 | if (First) { |
136 | 415 | fmt::print(Out, " [--]"sv); |
137 | 415 | First = false; |
138 | 415 | } |
139 | | |
140 | 830 | const bool Optional = (Desc.min_nargs() == 0); |
141 | 830 | fmt::print(Out, " "sv); |
142 | 830 | if (Optional) { |
143 | 415 | fmt::print(Out, "["sv); |
144 | 415 | } |
145 | 830 | switch (ArgumentDescriptors[Index].max_nargs()) { |
146 | 0 | case 0: |
147 | 0 | break; |
148 | 415 | case 1: |
149 | 415 | fmt::print(Out, "{}"sv, Desc.meta()); |
150 | 415 | break; |
151 | 415 | default: |
152 | 415 | fmt::print(Out, "{} ..."sv, Desc.meta()); |
153 | 415 | break; |
154 | 830 | } |
155 | 830 | if (Optional) { |
156 | 415 | fmt::print(Out, "]"sv); |
157 | 415 | } |
158 | 830 | } |
159 | 415 | fmt::print(Out, "\n"sv); |
160 | 415 | } |
161 | | |
162 | 415 | void ArgumentParser::SubCommandDescriptor::help(std::FILE *Out) const noexcept { |
163 | | // For enabling Windows PowerShell color support. |
164 | | #if WASMEDGE_OS_WINDOWS && WINAPI_PARTITION_DESKTOP |
165 | | winapi::HANDLE_ OutputHandler = |
166 | | winapi::GetStdHandle(winapi::STD_OUTPUT_HANDLE_); |
167 | | if (OutputHandler != winapi::INVALID_HANDLE_VALUE_) { |
168 | | winapi::DWORD_ ConsoleMode = 0; |
169 | | if (winapi::GetConsoleMode(OutputHandler, &ConsoleMode)) { |
170 | | ConsoleMode |= winapi::ENABLE_VIRTUAL_TERMINAL_PROCESSING_; |
171 | | winapi::SetConsoleMode(OutputHandler, ConsoleMode); |
172 | | } |
173 | | } |
174 | | #endif |
175 | | |
176 | 415 | usage(Out); |
177 | 415 | const constexpr std::string_view kIndent = "\t"sv; |
178 | | |
179 | 415 | fmt::print(Out, "\n"sv); |
180 | 415 | if (!SubCommandList.empty()) { |
181 | 0 | fmt::print(Out, "{}SUBCOMMANDS{}\n"sv, YELLOW_COLOR, RESET_COLOR); |
182 | 0 | for (const auto Offset : SubCommandList) { |
183 | 0 | fmt::print(Out, "{}{}"sv, kIndent, GREEN_COLOR); |
184 | 0 | bool First = true; |
185 | 0 | for (const auto &Name : this[Offset].SubCommandNames) { |
186 | 0 | if (!First) { |
187 | 0 | fmt::print(Out, "|"sv); |
188 | 0 | } |
189 | 0 | fmt::print(Out, "{}"sv, Name); |
190 | 0 | First = false; |
191 | 0 | } |
192 | 0 | fmt::print(Out, "{}\n"sv, RESET_COLOR); |
193 | 0 | indent_output(Out, kIndent, 2, 80, this[Offset].SC->description()); |
194 | 0 | fmt::print(Out, "\n"sv); |
195 | 0 | } |
196 | 0 | fmt::print(Out, "\n"sv); |
197 | 0 | } |
198 | | |
199 | 415 | fmt::print(Out, "{}OPTIONS{}\n"sv, YELLOW_COLOR, RESET_COLOR); |
200 | 12.0k | for (const auto &Index : NonpositionalList) { |
201 | 12.0k | const auto &Desc = ArgumentDescriptors[Index]; |
202 | 12.0k | if (Desc.hidden()) { |
203 | 0 | continue; |
204 | 0 | } |
205 | | |
206 | 12.0k | fmt::print(Out, "{}{}\n"sv, kIndent, GREEN_COLOR); |
207 | 12.0k | bool First = true; |
208 | 12.8k | for (const auto &Option : Desc.options()) { |
209 | 12.8k | if (!First) { |
210 | 830 | fmt::print(Out, "|"sv); |
211 | 830 | } |
212 | 12.8k | if (Option.size() == 1) { |
213 | 830 | fmt::print(Out, "-{}"sv, Option); |
214 | 12.0k | } else { |
215 | 12.0k | fmt::print(Out, "--{}"sv, Option); |
216 | 12.0k | } |
217 | 12.8k | First = false; |
218 | 12.8k | } |
219 | 12.0k | fmt::print(Out, "{}\n"sv, RESET_COLOR); |
220 | 12.0k | indent_output(Out, kIndent, 2, 80, Desc.description()); |
221 | 12.0k | fmt::print(Out, "\n"sv); |
222 | 12.0k | } |
223 | 415 | } |
224 | | |
225 | | void ArgumentParser::SubCommandDescriptor::indent_output( |
226 | | std::FILE *Out, const std::string_view kIndent, std::size_t IndentCount, |
227 | 12.0k | std::size_t ScreenWidth, std::string_view Desc) const noexcept { |
228 | 12.0k | const std::size_t Width = ScreenWidth - kIndent.size() * IndentCount; |
229 | 14.1k | while (Desc.size() > Width) { |
230 | 2.07k | const std::size_t SpacePos = Desc.find_last_of(' ', Width); |
231 | 2.07k | if (SpacePos != std::string_view::npos) { |
232 | 6.22k | for (std::size_t I = 0; I < IndentCount; ++I) { |
233 | 4.15k | fmt::print(Out, "{}"sv, kIndent); |
234 | 4.15k | } |
235 | 2.07k | fmt::print(Out, "{}\n"sv, Desc.substr(0, SpacePos)); |
236 | 2.07k | const std::size_t WordPos = Desc.find_first_not_of(' ', SpacePos); |
237 | 2.07k | if (WordPos != std::string_view::npos) { |
238 | 2.07k | Desc = Desc.substr(WordPos); |
239 | 2.07k | } else { |
240 | 0 | Desc = {}; |
241 | 0 | } |
242 | 2.07k | } |
243 | 2.07k | } |
244 | 12.0k | if (!Desc.empty()) { |
245 | 36.1k | for (std::size_t I = 0; I < IndentCount; ++I) { |
246 | 24.0k | fmt::print(Out, "{}"sv, kIndent); |
247 | 24.0k | } |
248 | 12.0k | fmt::print(Out, "{}"sv, Desc); |
249 | 12.0k | } |
250 | 12.0k | } |
251 | | |
252 | | cxx20::expected<ArgumentParser::ArgumentDescriptor *, Error> |
253 | | ArgumentParser::SubCommandDescriptor::consume_short_options( |
254 | 3.12k | std::string_view Arg) noexcept { |
255 | 3.12k | ArgumentDescriptor *CurrentDesc = nullptr; |
256 | 275k | for (std::size_t I = 1; I < Arg.size(); ++I) { |
257 | 272k | if (CurrentDesc && CurrentDesc->nargs() == 0) { |
258 | 0 | CurrentDesc->default_value(); |
259 | 0 | } |
260 | 272k | std::string_view Option = Arg.substr(I, 1); |
261 | 272k | if (auto Res = consume_short_option(Option); !Res) { |
262 | 31 | return cxx20::unexpected(Res.error()); |
263 | 272k | } else { |
264 | 272k | CurrentDesc = *Res; |
265 | 272k | } |
266 | 272k | } |
267 | 3.09k | return CurrentDesc; |
268 | 3.12k | } |
269 | | |
270 | | cxx20::expected<ArgumentParser::ArgumentDescriptor *, Error> |
271 | | ArgumentParser::SubCommandDescriptor::consume_long_option_with_argument( |
272 | 241k | std::string_view Arg) noexcept { |
273 | 241k | if (auto Pos = Arg.find('=', 2); Pos != std::string_view::npos) { |
274 | | // long option with argument |
275 | 233k | std::string_view Option = Arg.substr(2, Pos - 2); |
276 | 233k | std::string_view Argument = Arg.substr(Pos + 1); |
277 | 233k | if (auto Res = consume_long_option(Option); !Res) { |
278 | 50 | return cxx20::unexpected<Error>(Res.error()); |
279 | 233k | } else if (ArgumentDescriptor *CurrentDesc = *Res; !CurrentDesc) { |
280 | 7 | return cxx20::unexpected<Error>( |
281 | 7 | std::in_place, ErrCode::InvalidArgument, |
282 | 7 | fmt::format("option {} doesn't need arguments.", Option)); |
283 | 233k | } else { |
284 | 233k | if (auto ConsumeRes = consume_argument(*CurrentDesc, Argument); |
285 | 233k | !ConsumeRes) { |
286 | 184 | return cxx20::unexpected<Error>(ConsumeRes.error()); |
287 | 184 | } |
288 | 232k | return nullptr; |
289 | 233k | } |
290 | 233k | } else { |
291 | | // long option without argument |
292 | 8.37k | std::string_view Option = Arg.substr(2); |
293 | 8.37k | return consume_long_option(Option); |
294 | 8.37k | } |
295 | 241k | } |
296 | | |
297 | | cxx20::expected<ArgumentParser::ArgumentDescriptor *, Error> |
298 | | ArgumentParser::SubCommandDescriptor::consume_short_option( |
299 | 272k | std::string_view Option) noexcept { |
300 | 272k | auto Iter = ArgumentMap.find(Option); |
301 | 272k | if (Iter == ArgumentMap.end()) { |
302 | 31 | return cxx20::unexpected<Error>(std::in_place, ErrCode::InvalidArgument, |
303 | 31 | "unknown option: "s + std::string(Option)); |
304 | 31 | } |
305 | 272k | ArgumentDescriptor &CurrentDesc = ArgumentDescriptors[Iter->second]; |
306 | 272k | if (CurrentDesc.max_nargs() == 0) { |
307 | 272k | CurrentDesc.default_value(); |
308 | 272k | return nullptr; |
309 | 272k | } |
310 | 0 | return &CurrentDesc; |
311 | 272k | } |
312 | | |
313 | | cxx20::expected<ArgumentParser::ArgumentDescriptor *, Error> |
314 | | ArgumentParser::SubCommandDescriptor::consume_long_option( |
315 | 241k | std::string_view Option) noexcept { |
316 | 241k | auto Iter = ArgumentMap.find(Option); |
317 | 241k | if (Iter == ArgumentMap.end()) { |
318 | 317 | return cxx20::unexpected<Error>(std::in_place, ErrCode::InvalidArgument, |
319 | 317 | "unknown option: "s + std::string(Option)); |
320 | 317 | } |
321 | 241k | ArgumentDescriptor &CurrentDesc = ArgumentDescriptors[Iter->second]; |
322 | 241k | if (CurrentDesc.max_nargs() == 0) { |
323 | 2.20k | CurrentDesc.default_value(); |
324 | 2.20k | return nullptr; |
325 | 2.20k | } |
326 | 239k | return &CurrentDesc; |
327 | 241k | } |
328 | | |
329 | | cxx20::expected<ArgumentParser::ArgumentDescriptor *, Error> |
330 | | ArgumentParser::SubCommandDescriptor::consume_argument( |
331 | 1.31M | ArgumentDescriptor &CurrentDesc, std::string_view Argument) noexcept { |
332 | 1.31M | if (auto Res = CurrentDesc.argument(std::string(Argument)); !Res) { |
333 | 207 | return cxx20::unexpected(Res.error()); |
334 | 207 | } |
335 | 1.31M | if (++CurrentDesc.nargs() >= CurrentDesc.max_nargs()) { |
336 | 70.3k | return nullptr; |
337 | 70.3k | } |
338 | 1.24M | return &CurrentDesc; |
339 | 1.31M | } |
340 | | |
341 | | bool ArgumentParser::parse(std::FILE *Out, int Argc, |
342 | 1.61k | const char *Argv[]) noexcept { |
343 | 1.61k | if (auto Res = SubCommandDescriptors.front().parse(Out, {}, Argc, Argv, 0, |
344 | 1.61k | VerOpt.value()); |
345 | 1.61k | !Res) { |
346 | 562 | fmt::print(Out, "{}\n"sv, Res.error().message()); |
347 | 562 | return false; |
348 | 1.05k | } else { |
349 | 1.05k | return *Res || VerOpt.value(); |
350 | 1.05k | } |
351 | 1.61k | } |
352 | | |
353 | | } // namespace PO |
354 | | } // namespace WasmEdge |