1 /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
2 file Copyright.txt or https://cmake.org/licensing for details. */
5 #include <initializer_list>
12 #include <cm/optional>
13 #include <cm/string_view>
14 #include <cmext/string_view>
16 #include "cmArgumentParser.h"
17 #include "cmArgumentParserTypes.h"
21 struct Result : public ArgumentParser::ParseResult
27 cm::optional<std::string> String2;
28 cm::optional<std::string> String3;
29 ArgumentParser::Maybe<std::string> String4;
30 ArgumentParser::NonEmpty<std::string> String5;
31 ArgumentParser::NonEmpty<std::string> String6;
33 ArgumentParser::NonEmpty<std::vector<std::string>> List1;
34 ArgumentParser::NonEmpty<std::vector<std::string>> List2;
35 cm::optional<ArgumentParser::NonEmpty<std::vector<std::string>>> List3;
36 cm::optional<ArgumentParser::NonEmpty<std::vector<std::string>>> List4;
37 cm::optional<ArgumentParser::NonEmpty<std::vector<std::string>>> List5;
38 cm::optional<ArgumentParser::MaybeEmpty<std::vector<std::string>>> List6;
40 std::vector<std::vector<std::string>> Multi1;
41 std::vector<std::vector<std::string>> Multi2;
42 cm::optional<std::vector<std::vector<std::string>>> Multi3;
43 cm::optional<std::vector<std::vector<std::string>>> Multi4;
45 cm::optional<std::string> Pos0;
46 cm::optional<std::string> Pos1;
47 cm::optional<std::string> Pos2;
50 ArgumentParser::Continue Func0(cm::string_view)
53 return ArgumentParser::Continue::No;
57 ArgumentParser::Continue Func1(cm::string_view arg)
59 Func1_ = std::string(arg);
60 return ArgumentParser::Continue::No;
63 std::map<std::string, std::vector<std::string>> Func2_;
64 ArgumentParser::Continue Func2(cm::string_view key, cm::string_view arg)
66 Func2_[std::string(key)].emplace_back(arg);
67 return key == "FUNC_2b" ? ArgumentParser::Continue::Yes
68 : ArgumentParser::Continue::No;
71 std::vector<std::string> Func3_;
72 ArgumentParser::Continue Func3(cm::string_view arg)
74 Func3_.emplace_back(arg);
75 return ArgumentParser::Continue::Yes;
78 std::map<std::string, std::vector<std::string>> Func4_;
79 ArgumentParser::Continue Func4(cm::string_view key, cm::string_view arg)
81 Func4_[std::string(key)].emplace_back(arg);
82 return key == "FUNC_4b" ? ArgumentParser::Continue::Yes
83 : ArgumentParser::Continue::No;
86 ArgumentParser::Maybe<std::string> UnboundMaybe{ 'u', 'n', 'b', 'o',
88 ArgumentParser::MaybeEmpty<std::vector<std::string>> UnboundMaybeEmpty{
91 ArgumentParser::NonEmpty<std::vector<std::string>> UnboundNonEmpty{
94 ArgumentParser::NonEmpty<std::string> UnboundNonEmptyStr{ 'u', 'n', 'b', 'o',
97 std::vector<cm::string_view> ParsedKeywords;
100 std::initializer_list<cm::string_view> const args = {
101 /* clang-format off */
102 "pos0", // position index 0
103 "OPTION_1", // option
104 "pos2", // position index 2, ignored because after keyword
105 // "OPTION_2", // option that is not present
106 "STRING_1", // string arg missing value
107 "STRING_2", "foo", "bar", // string arg + unparsed value, presence captured
108 // "STRING_3", // string arg that is not present
109 "STRING_4", // string arg allowed to be missing value
110 "STRING_5", "foo", // string arg that is not empty
111 "STRING_6", "", // string arg that is empty
112 "LIST_1", // list arg missing values
113 "LIST_2", "foo", "bar", // list arg with 2 elems
114 "LIST_3", "bar", // list arg ...
115 "LIST_3", "foo", // ... with continuation
116 "LIST_4", // list arg missing values, presence captured
117 // "LIST_5", // list arg that is not present
118 "LIST_6", // list arg allowed to be empty
119 "MULTI_2", // multi list with 0 lists
120 "MULTI_3", "foo", "bar", // multi list with first list with two elems
121 "MULTI_3", "bar", "foo", // multi list with second list with two elems
122 // "MULTI_4", // multi list arg that is not present
123 "FUNC_0", // callback arg missing value
124 "FUNC_1", "foo", "ign1", // callback with one arg + unparsed value
125 "FUNC_2a", "foo", "ign2", // callback with keyword-dependent arg count
126 "FUNC_2b", "bar", "zot", // callback with keyword-dependent arg count
127 "FUNC_3", "foo", "bar", // callback with list arg ...
128 "FUNC_4a", "foo", "ign4", // callback with keyword-dependent arg count
129 "FUNC_4b", "bar", "zot", // callback with keyword-dependent arg count
130 /* clang-format on */
133 bool verifyResult(Result const& result,
134 std::vector<std::string> const& unparsedArguments)
136 static std::vector<std::string> const foobar = { "foo", "bar" };
137 static std::vector<std::string> const barfoo = { "bar", "foo" };
138 static std::vector<std::string> const unbound = { "unbound" };
139 static std::vector<cm::string_view> const parsedKeywords = {
140 /* clang-format off */
163 /* clang-format on */
165 static std::map<std::string, std::vector<std::string>> const func2map = {
166 { "FUNC_2a", { "foo" } }, { "FUNC_2b", { "bar", "zot" } }
168 static std::map<std::string, std::vector<std::string>> const func4map = {
169 { "FUNC_4a", { "foo" } }, { "FUNC_4b", { "bar", "zot" } }
171 static std::map<cm::string_view, std::string> const keywordErrors = {
172 { "STRING_1"_s, " missing required value\n" },
173 { "STRING_6"_s, " empty string not allowed\n" },
174 { "LIST_1"_s, " missing required value\n" },
175 { "LIST_4"_s, " missing required value\n" },
176 { "FUNC_0"_s, " missing required value\n" }
178 static std::vector<std::string> const unparsed = { "pos2", "bar", "ign1",
181 #define ASSERT_TRUE(x) \
184 std::cout << "ASSERT_TRUE(" #x ") failed on line " << __LINE__ << "\n"; \
189 ASSERT_TRUE(!result);
191 ASSERT_TRUE(result.Option1);
192 ASSERT_TRUE(!result.Option2);
194 ASSERT_TRUE(result.String1.empty());
195 ASSERT_TRUE(result.String2);
196 ASSERT_TRUE(*result.String2 == "foo");
197 ASSERT_TRUE(!result.String3);
198 ASSERT_TRUE(result.String4.empty());
199 ASSERT_TRUE(result.String5 == "foo");
200 ASSERT_TRUE(result.String6.empty());
202 ASSERT_TRUE(result.List1.empty());
203 ASSERT_TRUE(result.List2 == foobar);
204 ASSERT_TRUE(result.List3);
205 ASSERT_TRUE(*result.List3 == barfoo);
206 ASSERT_TRUE(result.List4);
207 ASSERT_TRUE(result.List4->empty());
208 ASSERT_TRUE(!result.List5);
209 ASSERT_TRUE(result.List6);
210 ASSERT_TRUE(result.List6->empty());
212 ASSERT_TRUE(result.Multi1.empty());
213 ASSERT_TRUE(result.Multi2.size() == 1);
214 ASSERT_TRUE(result.Multi2[0].empty());
215 ASSERT_TRUE(result.Multi3);
216 ASSERT_TRUE((*result.Multi3).size() == 2);
217 ASSERT_TRUE((*result.Multi3)[0] == foobar);
218 ASSERT_TRUE((*result.Multi3)[1] == barfoo);
219 ASSERT_TRUE(!result.Multi4);
221 ASSERT_TRUE(result.Pos0 == "pos0");
222 ASSERT_TRUE(!result.Pos1);
223 ASSERT_TRUE(!result.Pos2);
225 ASSERT_TRUE(result.Func0_ == false);
226 ASSERT_TRUE(result.Func1_ == "foo");
227 ASSERT_TRUE(result.Func2_ == func2map);
228 ASSERT_TRUE(result.Func3_ == foobar);
229 ASSERT_TRUE(result.Func4_ == func4map);
231 ASSERT_TRUE(unparsedArguments == unparsed);
233 ASSERT_TRUE(result.UnboundMaybe == "unbound");
234 ASSERT_TRUE(result.UnboundMaybeEmpty == unbound);
235 ASSERT_TRUE(result.UnboundNonEmpty == unbound);
236 ASSERT_TRUE(result.UnboundNonEmptyStr == "unbound");
238 ASSERT_TRUE(result.ParsedKeywords == parsedKeywords);
240 ASSERT_TRUE(result.GetKeywordErrors().size() == keywordErrors.size());
241 for (auto const& ke : result.GetKeywordErrors()) {
242 auto const ki = keywordErrors.find(ke.first);
243 ASSERT_TRUE(ki != keywordErrors.end());
244 ASSERT_TRUE(ke.second == ki->second);
250 bool testArgumentParserDynamic()
253 std::vector<std::string> unparsedArguments;
255 std::function<ArgumentParser::Continue(cm::string_view, cm::string_view)>
256 func4 = [&result](cm::string_view key,
257 cm::string_view arg) -> ArgumentParser::Continue {
258 return result.Func4(key, arg);
261 static_cast<ArgumentParser::ParseResult&>(result) =
262 cmArgumentParser<void>{}
263 .Bind(0, result.Pos0)
264 .Bind(1, result.Pos1)
265 .Bind(2, result.Pos2)
266 .Bind("OPTION_1"_s, result.Option1)
267 .Bind("OPTION_2"_s, result.Option2)
268 .Bind("STRING_1"_s, result.String1)
269 .Bind("STRING_2"_s, result.String2)
270 .Bind("STRING_3"_s, result.String3)
271 .Bind("STRING_4"_s, result.String4)
272 .Bind("STRING_5"_s, result.String5)
273 .Bind("STRING_6"_s, result.String6)
274 .Bind("LIST_1"_s, result.List1)
275 .Bind("LIST_2"_s, result.List2)
276 .Bind("LIST_3"_s, result.List3)
277 .Bind("LIST_4"_s, result.List4)
278 .Bind("LIST_5"_s, result.List5)
279 .Bind("LIST_6"_s, result.List6)
280 .Bind("MULTI_1"_s, result.Multi1)
281 .Bind("MULTI_2"_s, result.Multi2)
282 .Bind("MULTI_3"_s, result.Multi3)
283 .Bind("MULTI_4"_s, result.Multi4)
285 [&result](cm::string_view arg) -> ArgumentParser::Continue {
286 return result.Func0(arg);
289 [&result](cm::string_view arg) -> ArgumentParser::Continue {
290 return result.Func1(arg);
293 [&result](cm::string_view key, cm::string_view arg)
294 -> ArgumentParser::Continue { return result.Func2(key, arg); })
296 [&result](cm::string_view key, cm::string_view arg)
297 -> ArgumentParser::Continue { return result.Func2(key, arg); })
299 [&result](cm::string_view arg) -> ArgumentParser::Continue {
300 return result.Func3(arg);
302 .Bind("FUNC_4a"_s, func4)
303 .Bind("FUNC_4b"_s, func4)
304 .BindParsedKeywords(result.ParsedKeywords)
305 .Parse(args, &unparsedArguments);
307 return verifyResult(result, unparsedArguments);
310 static auto const parserStaticFunc4 =
311 [](Result& result, cm::string_view key,
312 cm::string_view arg) -> ArgumentParser::Continue {
313 return result.Func4(key, arg);
315 static auto const parserStatic = //
316 cmArgumentParser<Result>{}
317 .Bind(0, &Result::Pos0)
318 .Bind(1, &Result::Pos1)
319 .Bind(2, &Result::Pos2)
320 .Bind("OPTION_1"_s, &Result::Option1)
321 .Bind("OPTION_2"_s, &Result::Option2)
322 .Bind("STRING_1"_s, &Result::String1)
323 .Bind("STRING_2"_s, &Result::String2)
324 .Bind("STRING_3"_s, &Result::String3)
325 .Bind("STRING_4"_s, &Result::String4)
326 .Bind("STRING_5"_s, &Result::String5)
327 .Bind("STRING_6"_s, &Result::String6)
328 .Bind("LIST_1"_s, &Result::List1)
329 .Bind("LIST_2"_s, &Result::List2)
330 .Bind("LIST_3"_s, &Result::List3)
331 .Bind("LIST_4"_s, &Result::List4)
332 .Bind("LIST_5"_s, &Result::List5)
333 .Bind("LIST_6"_s, &Result::List6)
334 .Bind("MULTI_1"_s, &Result::Multi1)
335 .Bind("MULTI_2"_s, &Result::Multi2)
336 .Bind("MULTI_3"_s, &Result::Multi3)
337 .Bind("MULTI_4"_s, &Result::Multi4)
338 .Bind("FUNC_0"_s, &Result::Func0)
339 .Bind("FUNC_1"_s, &Result::Func1)
340 .Bind("FUNC_2a"_s, &Result::Func2)
341 .Bind("FUNC_2b"_s, &Result::Func2)
343 [](Result& result, cm::string_view arg) -> ArgumentParser::Continue {
344 return result.Func3(arg);
346 .Bind("FUNC_4a"_s, parserStaticFunc4)
347 .Bind("FUNC_4b"_s, parserStaticFunc4)
348 .BindParsedKeywords(&Result::ParsedKeywords)
349 /* keep semicolon on own line */;
351 bool testArgumentParserStatic()
353 std::vector<std::string> unparsedArguments;
354 Result const result = parserStatic.Parse(args, &unparsedArguments);
355 return verifyResult(result, unparsedArguments);
358 bool testArgumentParserStaticBool()
360 std::vector<std::string> unparsedArguments;
362 ASSERT_TRUE(parserStatic.Parse(result, args, &unparsedArguments) == false);
363 return verifyResult(result, unparsedArguments);
368 int testArgumentParser(int /*unused*/, char* /*unused*/ [])
370 if (!testArgumentParserDynamic()) {
371 std::cout << "While executing testArgumentParserDynamic().\n";
375 if (!testArgumentParserStatic()) {
376 std::cout << "While executing testArgumentParserStatic().\n";
380 if (!testArgumentParserStaticBool()) {
381 std::cout << "While executing testArgumentParserStaticBool().\n";