-
-
Notifications
You must be signed in to change notification settings - Fork 69
Expand file tree
/
Copy pathcmdline_run_test.cpp
More file actions
159 lines (155 loc) · 3.82 KB
/
cmdline_run_test.cpp
File metadata and controls
159 lines (155 loc) · 3.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/*
Copyright René Ferdinand Rivera Morell
Distributed under the Boost Software License, Version 1.0.
(See accompanying file LICENSE.txt or copy at
http://www.boost.org/LICENSE_1_0.txt)
*/
#include <lyra/lyra.hpp>
#include "mini_test.hpp"
int main()
{
using namespace lyra;
bfg::mini_test::scope test;
struct Config
{
std::string process_name;
std::string file_name;
int number = 0;
int index = 0;
bool flag = false;
std::string first_pos;
std::string second_pos;
std::vector<std::string> unpositional;
} config;
auto parser
= exe_name( config.process_name )
| opt( config.file_name, "filename" )
["-o"]["--output"]
( "specifies output file" )
| opt( config.number, "an integral value" )
["-n"]
| opt( [&]( int i ) {
if (i < 0 || i > 10)
return parser_result::error(
parser_result_type::no_match,
"index must be between 0 and 10");
else {
config.index = i;
return parser_result::ok( parser_result_type::matched );
}
}, "index" )
["-i"]
( "An index, which is an integer between 0 and 10, inclusive" )
| opt( config.flag )
["-f"]
( "A flag" )
| arg( config.first_pos, "first arg" )
( "First position" )
| arg( config.second_pos, "second arg" )
( "Second position" );
config = Config();
{
auto result = parser.parse( { "TestApp", "-o", "filename.ext" } );
test
(REQUIRE( result ))
(REQUIRE( config.process_name == "TestApp" ));
}
config = Config();
{
auto result = parser.parse( { "TestApp", "-o", "filename.ext" } );
test
(REQUIRE( result ))
(REQUIRE( config.file_name == "filename.ext" ));
}
config = Config();
{
auto result = parser.parse( { "TestApp", "-o=filename.ext" } );
test
(REQUIRE( result ))
(REQUIRE( config.file_name == "filename.ext" ));
}
config = Config();
{
auto result = parser.parse( { "TestApp", "--output", "%stdout" } );
test
(REQUIRE( result ))
(REQUIRE( config.file_name == "%stdout" ));
}
config = Config();
{
auto result = parser.parse( { "TestApp", "-n", "42" } );
test
(REQUIRE( result ))
(REQUIRE( config.number == 42 ));
}
config = Config();
{
auto result = parser.parse( { "TestApp", "-n", "forty-two" } );
test
(REQUIRE( !result ))
(REQUIRE( result.message() == "Unable to convert 'forty-two' to destination type" ))
(REQUIRE( config.number == 0 ));
}
config = Config();
{
auto result = parser.parse( { "TestApp", "-i", "3" } );
test
(REQUIRE( result ))
(REQUIRE( config.index == 3 ));
}
config = Config();
{
auto result = parser.parse( { "TestApp", "-i", "42" } );
test
(REQUIRE( !result ))
(REQUIRE( result.message() == "index must be between 0 and 10" ));
}
config = Config();
{
auto result = parser.parse({ "TestApp", "-f" });
test
(REQUIRE( result ))
(REQUIRE(config.flag));
}
config = Config();
{
auto result = parser.parse({ "TestApp" });
test
(REQUIRE( result ))
(REQUIRE( result.value().type() == parser_result_type::empty_match))
(REQUIRE( config.flag == false ));
}
config = Config();
{
auto result = parser.parse({ "TestApp", "-f", "something" });
test
(REQUIRE( result ))
(REQUIRE( config.flag ))
(REQUIRE( config.first_pos == "something" ));
}
config = Config();
{
auto result = parser.parse({ "TestApp", "something", "-f" });
test
(REQUIRE( result ))
(REQUIRE( config.flag ))
(REQUIRE( config.first_pos == "something" ));
}
config = Config();
{
auto result = parser.parse({ "TestApp", "something" });
test
(REQUIRE( result ))
(REQUIRE( config.flag == false ))
(REQUIRE( config.first_pos == "something" ));
}
config = Config();
{
auto result = parser.parse( { "TestApp", "-f", "1st", "-o", "filename", "2nd" } );
test
(REQUIRE( result ))
(REQUIRE( config.first_pos == "1st" ))
(REQUIRE( config.second_pos == "2nd" ));
}
return test;
}