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