/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.65k | const char *Argv[], int ArgP, const bool &VersionOpt) noexcept { |
16 | 1.65k | ProgramNames.reserve(ProgramNamePrefix.size() + 1); |
17 | 1.65k | ProgramNames.assign(ProgramNamePrefix.begin(), ProgramNamePrefix.end()); |
18 | 1.65k | if (ArgP < Argc) { |
19 | 1.65k | ProgramNames.push_back(Argv[ArgP]); |
20 | 1.65k | } |
21 | 1.65k | ArgumentDescriptor *CurrentDesc = nullptr; |
22 | 1.65k | bool FirstNonOption = true; |
23 | 1.65k | bool Escaped = false; |
24 | 1.65k | auto PositionalIter = PositionalList.cbegin(); |
25 | 1.47M | for (int ArgI = ArgP + 1; ArgI < Argc; ++ArgI) { |
26 | 1.47M | std::string_view Arg = Argv[ArgI]; |
27 | 1.47M | if (!Escaped && Arg.size() >= 2 && Arg[0] == '-') { |
28 | 226k | if (Arg[1] == '-') { |
29 | 224k | if (Arg.size() == 2) { |
30 | 46 | Escaped = true; |
31 | 224k | } else { |
32 | | // long option |
33 | 224k | if (CurrentDesc && CurrentDesc->nargs() == 0) { |
34 | 2.59k | CurrentDesc->default_value(); |
35 | 2.59k | } |
36 | 224k | if (auto Res = consume_long_option_with_argument(Arg); !Res) { |
37 | 519 | return cxx20::unexpected(Res.error()); |
38 | 224k | } else { |
39 | 224k | CurrentDesc = *Res; |
40 | 224k | } |
41 | 224k | } |
42 | 224k | } else { |
43 | | // short options |
44 | 2.08k | if (CurrentDesc && CurrentDesc->nargs() == 0) { |
45 | 224 | CurrentDesc->default_value(); |
46 | 224 | } |
47 | 2.08k | if (auto Res = consume_short_options(Arg); !Res) { |
48 | 38 | return cxx20::unexpected(Res.error()); |
49 | 2.05k | } else { |
50 | 2.05k | CurrentDesc = *Res; |
51 | 2.05k | } |
52 | 2.08k | } |
53 | 1.24M | } else if (!Escaped && CurrentDesc) { |
54 | 488 | if (auto Res = consume_argument(*CurrentDesc, Arg); !Res) { |
55 | 12 | return cxx20::unexpected(Res.error()); |
56 | 12 | } |
57 | 476 | CurrentDesc = nullptr; |
58 | 1.24M | } else { |
59 | | // no more options |
60 | 1.24M | if (FirstNonOption) { |
61 | 516 | FirstNonOption = false; |
62 | 516 | 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 | 516 | } |
71 | 1.24M | Escaped = true; |
72 | 1.24M | if (CurrentDesc) { |
73 | 1.24M | if (auto Res = consume_argument(*CurrentDesc, Arg); !Res) { |
74 | 8 | return cxx20::unexpected(Res.error()); |
75 | 1.24M | } else { |
76 | 1.24M | CurrentDesc = *Res; |
77 | 1.24M | } |
78 | 1.24M | } else { |
79 | 757 | 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 | 757 | if (auto Res = |
85 | 757 | consume_argument(ArgumentDescriptors[*PositionalIter], Arg); |
86 | 757 | !Res) { |
87 | 0 | return cxx20::unexpected(Res.error()); |
88 | 757 | } else { |
89 | 757 | CurrentDesc = *Res; |
90 | 757 | } |
91 | 757 | ++PositionalIter; |
92 | 757 | } |
93 | 1.24M | } |
94 | 1.47M | } |
95 | 1.07k | if (CurrentDesc && CurrentDesc->nargs() == 0) { |
96 | 37 | CurrentDesc->default_value(); |
97 | 37 | } |
98 | | |
99 | 1.07k | if (VersionOpt) { |
100 | 208 | return true; |
101 | 208 | } |
102 | 871 | if (!HelpOpt->value()) { |
103 | 15.3k | for (const auto &Desc : ArgumentDescriptors) { |
104 | 15.3k | if (Desc.nargs() < Desc.min_nargs()) { |
105 | 361 | help(Out); |
106 | 361 | return false; |
107 | 361 | } |
108 | 15.3k | } |
109 | 820 | } else { |
110 | 51 | help(Out); |
111 | 51 | return true; |
112 | 51 | } |
113 | 459 | return true; |
114 | 871 | } |
115 | | |
116 | | void ArgumentParser::SubCommandDescriptor::usage( |
117 | 412 | std::FILE *Out) const noexcept { |
118 | 412 | fmt::print(Out, "{}USAGE{}\n"sv, YELLOW_COLOR, RESET_COLOR); |
119 | 412 | for (const char *Part : ProgramNames) { |
120 | 412 | fmt::print(Out, "\t{}"sv, Part); |
121 | 412 | } |
122 | 412 | if (!SubCommandList.empty()) { |
123 | 0 | fmt::print(Out, " [SUBCOMMANDS]"sv); |
124 | 0 | } |
125 | 412 | if (!NonpositionalList.empty()) { |
126 | 412 | fmt::print(Out, " [OPTIONS]"sv); |
127 | 412 | } |
128 | 412 | bool First = true; |
129 | 824 | for (const auto &Index : PositionalList) { |
130 | 824 | const auto &Desc = ArgumentDescriptors[Index]; |
131 | 824 | if (Desc.hidden()) { |
132 | 0 | continue; |
133 | 0 | } |
134 | | |
135 | 824 | if (First) { |
136 | 412 | fmt::print(Out, " [--]"sv); |
137 | 412 | First = false; |
138 | 412 | } |
139 | | |
140 | 824 | const bool Optional = (Desc.min_nargs() == 0); |
141 | 824 | fmt::print(Out, " "sv); |
142 | 824 | if (Optional) { |
143 | 412 | fmt::print(Out, "["sv); |
144 | 412 | } |
145 | 824 | switch (ArgumentDescriptors[Index].max_nargs()) { |
146 | 0 | case 0: |
147 | 0 | break; |
148 | 412 | case 1: |
149 | 412 | fmt::print(Out, "{}"sv, Desc.meta()); |
150 | 412 | break; |
151 | 412 | default: |
152 | 412 | fmt::print(Out, "{} ..."sv, Desc.meta()); |
153 | 412 | break; |
154 | 824 | } |
155 | 824 | if (Optional) { |
156 | 412 | fmt::print(Out, "]"sv); |
157 | 412 | } |
158 | 824 | } |
159 | 412 | fmt::print(Out, "\n"sv); |
160 | 412 | } |
161 | | |
162 | 412 | 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 | 412 | usage(Out); |
177 | 412 | const constexpr std::string_view kIndent = "\t"sv; |
178 | | |
179 | 412 | fmt::print(Out, "\n"sv); |
180 | 412 | 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 | 412 | fmt::print(Out, "{}OPTIONS{}\n"sv, YELLOW_COLOR, RESET_COLOR); |
200 | 11.9k | for (const auto &Index : NonpositionalList) { |
201 | 11.9k | const auto &Desc = ArgumentDescriptors[Index]; |
202 | 11.9k | if (Desc.hidden()) { |
203 | 0 | continue; |
204 | 0 | } |
205 | | |
206 | 11.9k | fmt::print(Out, "{}{}\n"sv, kIndent, GREEN_COLOR); |
207 | 11.9k | bool First = true; |
208 | 12.7k | for (const auto &Option : Desc.options()) { |
209 | 12.7k | if (!First) { |
210 | 824 | fmt::print(Out, "|"sv); |
211 | 824 | } |
212 | 12.7k | if (Option.size() == 1) { |
213 | 824 | fmt::print(Out, "-{}"sv, Option); |
214 | 11.9k | } else { |
215 | 11.9k | fmt::print(Out, "--{}"sv, Option); |
216 | 11.9k | } |
217 | 12.7k | First = false; |
218 | 12.7k | } |
219 | 11.9k | fmt::print(Out, "{}\n"sv, RESET_COLOR); |
220 | 11.9k | indent_output(Out, kIndent, 2, 80, Desc.description()); |
221 | 11.9k | fmt::print(Out, "\n"sv); |
222 | 11.9k | } |
223 | 412 | } |
224 | | |
225 | | void ArgumentParser::SubCommandDescriptor::indent_output( |
226 | | std::FILE *Out, const std::string_view kIndent, std::size_t IndentCount, |
227 | 11.9k | std::size_t ScreenWidth, std::string_view Desc) const noexcept { |
228 | 11.9k | const std::size_t Width = ScreenWidth - kIndent.size() * IndentCount; |
229 | 14.0k | while (Desc.size() > Width) { |
230 | 2.06k | const std::size_t SpacePos = Desc.find_last_of(' ', Width); |
231 | 2.06k | if (SpacePos != std::string_view::npos) { |
232 | 6.18k | for (std::size_t I = 0; I < IndentCount; ++I) { |
233 | 4.12k | fmt::print(Out, "{}"sv, kIndent); |
234 | 4.12k | } |
235 | 2.06k | fmt::print(Out, "{}\n"sv, Desc.substr(0, SpacePos)); |
236 | 2.06k | const std::size_t WordPos = Desc.find_first_not_of(' ', SpacePos); |
237 | 2.06k | if (WordPos != std::string_view::npos) { |
238 | 2.06k | Desc = Desc.substr(WordPos); |
239 | 2.06k | } else { |
240 | 0 | Desc = {}; |
241 | 0 | } |
242 | 2.06k | } |
243 | 2.06k | } |
244 | 11.9k | if (!Desc.empty()) { |
245 | 35.8k | for (std::size_t I = 0; I < IndentCount; ++I) { |
246 | 23.8k | fmt::print(Out, "{}"sv, kIndent); |
247 | 23.8k | } |
248 | 11.9k | fmt::print(Out, "{}"sv, Desc); |
249 | 11.9k | } |
250 | 11.9k | } |
251 | | |
252 | | cxx20::expected<ArgumentParser::ArgumentDescriptor *, Error> |
253 | | ArgumentParser::SubCommandDescriptor::consume_short_options( |
254 | 2.08k | std::string_view Arg) noexcept { |
255 | 2.08k | ArgumentDescriptor *CurrentDesc = nullptr; |
256 | 557k | for (std::size_t I = 1; I < Arg.size(); ++I) { |
257 | 555k | if (CurrentDesc && CurrentDesc->nargs() == 0) { |
258 | 0 | CurrentDesc->default_value(); |
259 | 0 | } |
260 | 555k | std::string_view Option = Arg.substr(I, 1); |
261 | 555k | if (auto Res = consume_short_option(Option); !Res) { |
262 | 38 | return cxx20::unexpected(Res.error()); |
263 | 555k | } else { |
264 | 555k | CurrentDesc = *Res; |
265 | 555k | } |
266 | 555k | } |
267 | 2.05k | return CurrentDesc; |
268 | 2.08k | } |
269 | | |
270 | | cxx20::expected<ArgumentParser::ArgumentDescriptor *, Error> |
271 | | ArgumentParser::SubCommandDescriptor::consume_long_option_with_argument( |
272 | 224k | std::string_view Arg) noexcept { |
273 | 224k | if (auto Pos = Arg.find('=', 2); Pos != std::string_view::npos) { |
274 | | // long option with argument |
275 | 215k | std::string_view Option = Arg.substr(2, Pos - 2); |
276 | 215k | std::string_view Argument = Arg.substr(Pos + 1); |
277 | 215k | if (auto Res = consume_long_option(Option); !Res) { |
278 | 59 | return cxx20::unexpected<Error>(Res.error()); |
279 | 215k | } else if (ArgumentDescriptor *CurrentDesc = *Res; !CurrentDesc) { |
280 | 9 | return cxx20::unexpected<Error>( |
281 | 9 | std::in_place, ErrCode::InvalidArgument, |
282 | 9 | fmt::format("option {} doesn't need arguments."sv, Option)); |
283 | 215k | } else { |
284 | 215k | if (auto ConsumeRes = consume_argument(*CurrentDesc, Argument); |
285 | 215k | !ConsumeRes) { |
286 | 179 | return cxx20::unexpected<Error>(ConsumeRes.error()); |
287 | 179 | } |
288 | 215k | return nullptr; |
289 | 215k | } |
290 | 215k | } else { |
291 | | // long option without argument |
292 | 8.72k | std::string_view Option = Arg.substr(2); |
293 | 8.72k | return consume_long_option(Option); |
294 | 8.72k | } |
295 | 224k | } |
296 | | |
297 | | cxx20::expected<ArgumentParser::ArgumentDescriptor *, Error> |
298 | | ArgumentParser::SubCommandDescriptor::consume_short_option( |
299 | 555k | std::string_view Option) noexcept { |
300 | 555k | auto Iter = ArgumentMap.find(Option); |
301 | 555k | if (Iter == ArgumentMap.end()) { |
302 | 38 | return cxx20::unexpected<Error>(std::in_place, ErrCode::InvalidArgument, |
303 | 38 | "unknown option: "s + std::string(Option)); |
304 | 38 | } |
305 | 555k | ArgumentDescriptor &CurrentDesc = ArgumentDescriptors[Iter->second]; |
306 | 555k | if (CurrentDesc.max_nargs() == 0) { |
307 | 555k | CurrentDesc.default_value(); |
308 | 555k | return nullptr; |
309 | 555k | } |
310 | 0 | return &CurrentDesc; |
311 | 555k | } |
312 | | |
313 | | cxx20::expected<ArgumentParser::ArgumentDescriptor *, Error> |
314 | | ArgumentParser::SubCommandDescriptor::consume_long_option( |
315 | 224k | std::string_view Option) noexcept { |
316 | 224k | auto Iter = ArgumentMap.find(Option); |
317 | 224k | if (Iter == ArgumentMap.end()) { |
318 | 331 | return cxx20::unexpected<Error>(std::in_place, ErrCode::InvalidArgument, |
319 | 331 | "unknown option: "s + std::string(Option)); |
320 | 331 | } |
321 | 224k | ArgumentDescriptor &CurrentDesc = ArgumentDescriptors[Iter->second]; |
322 | 224k | if (CurrentDesc.max_nargs() == 0) { |
323 | 1.79k | CurrentDesc.default_value(); |
324 | 1.79k | return nullptr; |
325 | 1.79k | } |
326 | 222k | return &CurrentDesc; |
327 | 224k | } |
328 | | |
329 | | cxx20::expected<ArgumentParser::ArgumentDescriptor *, Error> |
330 | | ArgumentParser::SubCommandDescriptor::consume_argument( |
331 | 1.46M | ArgumentDescriptor &CurrentDesc, std::string_view Argument) noexcept { |
332 | 1.46M | if (auto Res = CurrentDesc.argument(std::string(Argument)); !Res) { |
333 | 199 | return cxx20::unexpected(Res.error()); |
334 | 199 | } |
335 | 1.46M | if (++CurrentDesc.nargs() >= CurrentDesc.max_nargs()) { |
336 | 73.4k | return nullptr; |
337 | 73.4k | } |
338 | 1.39M | return &CurrentDesc; |
339 | 1.46M | } |
340 | | |
341 | | bool ArgumentParser::parse(std::FILE *Out, int Argc, |
342 | 1.65k | const char *Argv[]) noexcept { |
343 | 1.65k | if (auto Res = SubCommandDescriptors.front().parse(Out, {}, Argc, Argv, 0, |
344 | 1.65k | VerOpt.value()); |
345 | 1.65k | !Res) { |
346 | 577 | fmt::print(Out, "{}\n"sv, Res.error().message()); |
347 | 577 | return false; |
348 | 1.07k | } else { |
349 | 1.07k | return *Res || VerOpt.value(); |
350 | 1.07k | } |
351 | 1.65k | } |
352 | | |
353 | | } // namespace PO |
354 | | } // namespace WasmEdge |