1 /*
2 Copyright 2019-2022 René Ferdinand Rivera Morell
3 Distributed under the Boost Software License, Version 1.0.
4 (See accompanying file LICENSE.txt or copy at
5 http://www.boost.org/LICENSE_1_0.txt)
6 */
7 
8 #include <lyra/lyra.hpp>
9 #include "mini_test.hpp"
10 
main()11 int main()
12 {
13 	using namespace lyra;
14 	bfg::mini_test::scope test;
15 
16 	{
17 		std::string choice;
18 		auto p = cli() | opt(choice, "choice")["-c"]["--choice"]("the choice")
19 			.choices("one", "two", "other");
20 		auto result = p.parse( { "TestApp", "-c", "two" } );
21 		test
22 			(REQUIRE(result))
23 			(REQUIRE(choice == "two"));
24 	}
25 	{
26 		std::string choice;
27 		auto p = cli() | opt(choice, "choice")["-c"]["--choice"]("the choice")
28 			.choices("one", "two", "other");
29 		auto result = p.parse( { "TestApp", "-c", "foo" } );
30 		test
31 			(REQUIRE(!result));
32 	}
33 	{
34 		int choice = 0;
35 		auto p = cli() | opt(choice, "choice")["-c"]["--choice"]("the choice")
36 			.choices(1, 2, 3);
37 		auto result = p.parse( { "TestApp", "-c", "2" } );
38 		test
39 			(REQUIRE(result))
40 			(REQUIRE(choice == 2));
41 	}
42 	{
43 		int choice = 0;
44 		auto p = cli() | opt(choice, "choice")["-c"]["--choice"]("the choice")
45 			.choices(1, 2, 3);
46 		auto result = p.parse( { "TestApp", "-c", "5" } );
47 		test
48 			(REQUIRE(!result))
49 			(REQUIRE(choice == 0));
50 	}
51 	{
52 		int choice = 0;
53 		auto p = cli() | opt(choice, "choice")["-c"]["--choice"]("the choice")
54 			.choices(1);
55 		auto result = p.parse( { "TestApp", "-c", "1" } );
56 		test
57 			(REQUIRE(result))
58 			(REQUIRE(choice == 1));
59 	}
60 	{
61 		int choice = 20;
62 		auto p = cli() | opt(choice, "choice")["-c"]["--choice"]("the choice")
63 			.choices([](int value) -> bool { return 10 <= value && value <= 20; });
64 		auto result = p.parse( { "TestApp", "-c", "15" } );
65 		test
66 			(REQUIRE(result))
67 			(REQUIRE(choice == 15));
68 	}
69 	{
70 		int choice = 20;
71 		auto p = cli() | opt(choice, "choice")["-c"]["--choice"]("the choice")
72 			.choices([](int value) -> bool { return 10 <= value && value <= 20; });
73 		auto result = p.parse( { "TestApp", "-c", "40" } );
74 		test
75 			(REQUIRE(!result))
76 			(REQUIRE(choice == 20));
77 	}
78 	{
79 		std::string choice;
80 		auto p = cli() | arg(choice, "choice")("the choice")
81 			.choices("walk", "run", "jump");
82 		auto result = p.parse( { "TestApp", "run" } );
83 		test
84 			(REQUIRE(result))
85 			(REQUIRE(choice == "run"));
86 	}
87 	{
88 		std::string choice;
89 		const std::string choices[] = { "one", "two", "other" };
90 		auto p = cli() | opt(choice, "choice")["-c"]["--choice"]("the choice")
91 			.choices(choices);
92 		auto result = p.parse( { "TestApp", "-c", "two" } );
93 		test
94 			(REQUIRE(result))
95 			(REQUIRE(choice == "two"));
96 	}
97 
98 	return test;
99 }
100