1 /*
2 Copyright 2021-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 "mini_test.hpp"
9 #include <lyra/lyra.hpp>
10
test_style(bfg::mini_test::scope & test,const lyra::option_style & style)11 void test_style(bfg::mini_test::scope & test, const lyra::option_style & style)
12 {
13 using namespace lyra;
14
15 bool a = false, b = false, c = false;
16 int x = 10;
17 float y = 3.14f;
18 bool sdf = false;
19
20 auto cli = lyra::cli().style(style);
21 auto opt_a = opt(a);
22 auto opt_b = opt(b);
23 auto opt_c = opt(c);
24 auto opt_x = opt(x, "int-x");
25 auto opt_y = opt(y, "float-y");
26 auto opt_sdf = opt(sdf);
27 if (style.short_option_size > 0)
28 {
29 const std::string p(
30 style.short_option_size, style.short_option_prefix[0]);
31 opt_a["-a"];
32 opt_b["-b"];
33 opt_c["-c"];
34 opt_x["-x"];
35 opt_y["-y"];
36 opt_sdf["-sdf"];
37 }
38 if (style.long_option_size > 0)
39 {
40 const std::string p(
41 style.long_option_size, style.long_option_prefix[0]);
42 opt_a["--flag-a"];
43 opt_b["--flag-b"];
44 opt_c["--flag-c"];
45 opt_x["--int-x"];
46 opt_y["--float-y"];
47 }
48 if (style.short_option_size > 0 && style.long_option_size > 0)
49 {
50 cli |= opt_a | opt_b | opt_c | opt_x | opt_y | opt_sdf;
51 }
52 else if (style.short_option_size > 0)
53 {
54 cli |= opt_a | opt_b | opt_c | opt_x | opt_y | opt_sdf;
55 }
56 else if (style.long_option_size > 0)
57 {
58 cli |= opt_a | opt_b | opt_c | opt_x | opt_y;
59 }
60
61 const std::string d(1, style.value_delimiters[0]);
62 if (style.short_option_size > 0)
63 {
64 const std::string p(
65 style.short_option_size, style.short_option_prefix[0]);
66 const std::string i = "'" + style.short_option_prefix + "' '"
67 + style.value_delimiters + "'";
68 a = false, b = false, c = false;
69 test(
70 i,
71 REQUIRE(cli.parse(
72 { "TestApp",
73 p + "a",
74 p + "b",
75 p + "c",
76 p + "x",
77 "3",
78 p + "y",
79 "6.6" },
80 style)));
81 test(i, REQUIRE(a))(i, REQUIRE(b))(i, REQUIRE(c));
82 a = false, b = false, c = false;
83 test(
84 i,
85 REQUIRE(cli.parse(
86 { "TestApp", p + "abc", p + "x", "3", p + "y", "6.6" },
87 style)));
88 test(i, REQUIRE(a))(i, REQUIRE(b))(i, REQUIRE(c));
89 x = 0;
90 test(i, REQUIRE(cli.parse({ "TestApp", p + "x", "3" })));
91 test(i, REQUIRE(x == 3));
92 x = 0;
93 test(i, REQUIRE(cli.parse({ "TestApp", p + "x" + d + "3" })));
94 test(i, REQUIRE(x == 3));
95 x = 0;
96 test(i, REQUIRE(cli.parse({ "TestApp", p + "x3" })));
97 test(i, REQUIRE(x == 3));
98 x = 0;
99 test(i, REQUIRE(!cli.parse({ "TestApp", p + "x3garbage" })));
100 test(i, REQUIRE(x == 0));
101 x = 0;
102 test(
103 i,
104 REQUIRE(
105 !cli.parse({ "TestApp", p + "x" + d + "3garbage" })));
106 test(i, REQUIRE(x == 0));
107 x = 0;
108 test(i, REQUIRE(!cli.parse({ "TestApp", p + "x", "3garbage" })));
109 test(i, REQUIRE(x == 0));
110 test(i, REQUIRE(cli.parse({ "TestApp", p + "s" })));
111 test(i, REQUIRE(cli.parse({ "TestApp", p + "d" })));
112 test(i, REQUIRE(cli.parse({ "TestApp", p + "f" })));
113 }
114 if (style.long_option_size > 0)
115 {
116 const std::string p(
117 style.long_option_size, style.long_option_prefix[0]);
118 const std::string i = "'" + style.long_option_prefix + "' '"
119 + style.value_delimiters + "'";
120 x = 0;
121 test(i, REQUIRE(cli.parse({ "TestApp", p + "int-x", "3" })));
122 test(i, REQUIRE(x == 3));
123 x = 0;
124 test(
125 i, REQUIRE(cli.parse({ "TestApp", p + "int-x" + d + "3" })));
126 test(i, REQUIRE(x == 3));
127 x = 0;
128 test(i, REQUIRE(!cli.parse({ "TestApp", p + "int-x3" })));
129 test(i, REQUIRE(x == 0));
130 }
131 }
132
main()133 int main()
134 {
135 using namespace lyra;
136 bfg::mini_test::scope test;
137 test_style(test, option_style::posix());
138 test_style(test, option_style::posix_brief());
139 test_style(test, option_style::windows());
140 return test;
141 }
142