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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
|
/* Copyright © 2016 Taylor C. Richberger <taywee@gmx.com>
* This code is released under the license described in the LICENSE file
*/
#include <tuple>
#include <iostream>
std::istream& operator>>(std::istream& is, std::tuple<int, int>& ints)
{
is >> std::get<0>(ints);
is.get();
is >> std::get<1>(ints);
return is;
}
#include <args.hxx>
#define CATCH_CONFIG_MAIN
#include "catch.hpp"
TEST_CASE("Help flag throws Help exception", "[args]")
{
args::ArgumentParser parser("This is a test program.", "This goes after the options.");
args::HelpFlag help(parser, "help", "Display this help menu", args::Matcher{'h', "help"});
REQUIRE_NOTHROW(parser.ParseArgs(std::vector<std::string>{}));
REQUIRE_THROWS_AS(parser.ParseArgs(std::vector<std::string>{"--help"}), args::Help);
}
TEST_CASE("Unknown flags throw exceptions", "[args]")
{
args::ArgumentParser parser("This is a test program.", "This goes after the options.");
args::HelpFlag help(parser, "help", "Display this help menu", args::Matcher{'h', "help"});
REQUIRE_NOTHROW(parser.ParseArgs(std::vector<std::string>{}));
REQUIRE_THROWS_AS(parser.ParseArgs(std::vector<std::string>{"--Help"}), args::ParseError);
REQUIRE_THROWS_AS(parser.ParseArgs(std::vector<std::string>{"-H"}), args::ParseError);
}
TEST_CASE("Boolean flags work as expected, with clustering", "[args]")
{
args::ArgumentParser parser("This is a test program.", "This goes after the options.");
args::Flag foo(parser, "FOO", "test flag", args::Matcher{'f', "foo"});
args::Flag bar(parser, "BAR", "test flag", args::Matcher{'b', "bar"});
args::Flag baz(parser, "BAZ", "test flag", args::Matcher{'a', "baz"});
args::Flag bix(parser, "BAZ", "test flag", args::Matcher{'x', "bix"});
parser.ParseArgs(std::vector<std::string>{"--baz", "-fb"});
REQUIRE(foo);
REQUIRE(bar);
REQUIRE(baz);
REQUIRE_FALSE(bix);
}
TEST_CASE("Count flag works as expected", "[args]")
{
args::ArgumentParser parser("This is a test program.", "This goes after the options.");
args::Counter foo(parser, "FOO", "test flag", args::Matcher{'f', "foo"});
args::Counter bar(parser, "BAR", "test flag", args::Matcher{'b', "bar"}, 7);
args::Counter baz(parser, "BAZ", "test flag", args::Matcher{'z', "baz"}, 7);
parser.ParseArgs(std::vector<std::string>{"--foo", "-fb", "--bar", "-b", "-f", "--foo"});
REQUIRE(foo);
REQUIRE(bar);
REQUIRE_FALSE(baz);
REQUIRE(args::get(foo) == 4);
REQUIRE(args::get(bar) == 10);
REQUIRE(args::get(baz) == 7);
}
TEST_CASE("Argument flags work as expected, with clustering", "[args]")
{
args::ArgumentParser parser("This is a test program.", "This goes after the options.");
args::ArgFlag<std::string> foo(parser, "FOO", "test flag", args::Matcher{'f', "foo"});
args::Flag bar(parser, "BAR", "test flag", args::Matcher{'b', "bar"});
args::ArgFlag<double> baz(parser, "BAZ", "test flag", args::Matcher{'a', "baz"});
args::ArgFlag<char> bim(parser, "BAZ", "test flag", args::Matcher{'B', "bim"});
args::Flag bix(parser, "BAZ", "test flag", args::Matcher{'x', "bix"});
parser.ParseArgs(std::vector<std::string>{"-bftest", "--baz=7.555e2", "--bim", "c"});
REQUIRE(foo);
REQUIRE(args::get(foo) == "test");
REQUIRE(bar);
REQUIRE(baz);
REQUIRE((args::get(baz) > 755.49 && args::get(baz) < 755.51));
REQUIRE(bim);
REQUIRE(args::get(bim) == 'c');
REQUIRE_FALSE(bix);
}
TEST_CASE("Passing an argument to a non-argument flag throws an error", "[args]")
{
args::ArgumentParser parser("This is a test program.", "This goes after the options.");
args::Flag bar(parser, "BAR", "test flag", args::Matcher{'b', "bar"});
REQUIRE_THROWS_AS(parser.ParseArgs(std::vector<std::string>{"--bar=test"}), args::ParseError);
}
TEST_CASE("Unified argument lists for match work", "[args]")
{
args::ArgumentParser parser("This is a test program.", "This goes after the options.");
args::ArgFlag<std::string> foo(parser, "FOO", "test flag", args::Matcher{'f', "foo"});
args::Flag bar(parser, "BAR", "test flag", args::Matcher{"bar", 'b'});
args::ArgFlag<double> baz(parser, "BAZ", "test flag", args::Matcher{'a', "baz"});
args::ArgFlag<char> bim(parser, "BAZ", "test flag", args::Matcher{'B', "bim"});
args::Flag bix(parser, "BAZ", "test flag", args::Matcher{"bix"});
parser.ParseArgs(std::vector<std::string>{"-bftest", "--baz=7.555e2", "--bim", "c"});
REQUIRE(foo);
REQUIRE(args::get(foo) == "test");
REQUIRE(bar);
REQUIRE(baz);
REQUIRE((args::get(baz) > 755.49 && args::get(baz) < 755.51));
REQUIRE(bim);
REQUIRE(args::get(bim) == 'c');
REQUIRE_FALSE(bix);
}
TEST_CASE("Get can be assigned to for non-reference types", "[args]")
{
args::ArgumentParser parser("This is a test program.", "This goes after the options.");
args::ArgFlag<std::string> foo(parser, "FOO", "test flag", args::Matcher{'f', "foo"});
parser.ParseArgs(std::vector<std::string>{"--foo=test"});
REQUIRE(foo);
REQUIRE(args::get(foo) == "test");
args::get(foo) = "bar";
REQUIRE(args::get(foo) == "bar");
}
TEST_CASE("Invalid argument parsing throws parsing exceptions", "[args]")
{
args::ArgumentParser parser("This is a test program.", "This goes after the options.");
args::ArgFlag<int> foo(parser, "FOO", "test flag", args::Matcher{'f', "foo"});
REQUIRE_THROWS_AS(parser.ParseArgs(std::vector<std::string>{"--foo=7.5"}), args::ParseError);
REQUIRE_THROWS_AS(parser.ParseArgs(std::vector<std::string>{"--foo", "7a"}), args::ParseError);
REQUIRE_THROWS_AS(parser.ParseArgs(std::vector<std::string>{"--foo", "7e4"}), args::ParseError);
}
TEST_CASE("Argument flag lists work as expected", "[args]")
{
args::ArgumentParser parser("This is a test program.", "This goes after the options.");
args::ArgFlagList<int> foo(parser, "FOO", "test flag", args::Matcher{'f', "foo"});
parser.ParseArgs(std::vector<std::string>{"--foo=7", "-f2", "-f", "9", "--foo", "42"});
REQUIRE((args::get(foo) == std::vector<int>{7, 2, 9, 42}));
}
TEST_CASE("Positional arguments and positional argument lists work as expected", "[args]")
{
args::ArgumentParser parser("This is a test program.", "This goes after the options.");
args::PosArg<std::string> foo(parser, "FOO", "test flag");
args::PosArg<bool> bar(parser, "BAR", "test flag");
args::PosArgList<char> baz(parser, "BAZ", "test flag");
parser.ParseArgs(std::vector<std::string>{"this is a test flag", "0", "a", "b", "c", "x", "y", "z"});
REQUIRE(foo);
REQUIRE((args::get(foo) == "this is a test flag"));
REQUIRE(bar);
REQUIRE(!args::get(bar));
REQUIRE(baz);
REQUIRE((args::get(baz) == std::vector<char>{'a', 'b', 'c', 'x', 'y', 'z'}));
}
TEST_CASE("Positionals that are unspecified evaluate false", "[args]")
{
args::ArgumentParser parser("This is a test program.", "This goes after the options.");
args::PosArg<std::string> foo(parser, "FOO", "test flag");
args::PosArg<bool> bar(parser, "BAR", "test flag");
args::PosArgList<char> baz(parser, "BAZ", "test flag");
parser.ParseArgs(std::vector<std::string>{"this is a test flag again"});
REQUIRE(foo);
REQUIRE((args::get(foo) == "this is a test flag again"));
REQUIRE_FALSE(bar);
REQUIRE_FALSE(baz);
}
TEST_CASE("Additional positionals throw an exception", "[args]")
{
args::ArgumentParser parser("This is a test program.", "This goes after the options.");
args::PosArg<std::string> foo(parser, "FOO", "test flag");
args::PosArg<bool> bar(parser, "BAR", "test flag");
REQUIRE_THROWS_AS(parser.ParseArgs(std::vector<std::string>{"this is a test flag again", "1", "this has no positional available"}), args::ParseError);
}
TEST_CASE("Argument groups should throw when validation fails", "[args]")
{
args::ArgumentParser parser("This is a test program.", "This goes after the options.");
args::Group xorgroup(parser, "this group provides xor validation", args::Group::Validators::Xor);
args::Flag a(xorgroup, "a", "test flag", args::Matcher({'a'}));
args::Flag b(xorgroup, "b", "test flag", args::Matcher({'b'}));
args::Flag c(xorgroup, "c", "test flag", args::Matcher({'c'}));
args::Group nxor(parser, "this group provides all-or-none (nxor) validation", args::Group::Validators::AllOrNone);
args::Flag d(nxor, "d", "test flag", args::Matcher({'d'}));
args::Flag e(nxor, "e", "test flag", args::Matcher({'e'}));
args::Flag f(nxor, "f", "test flag", args::Matcher({'f'}));
args::Group atleastone(parser, "this group provides at-least-one validation", args::Group::Validators::AtLeastOne);
args::Flag g(atleastone, "g", "test flag", args::Matcher({'g'}));
args::Flag h(atleastone, "h", "test flag", args::Matcher({'h'}));
// Needs g or h
REQUIRE_THROWS_AS(parser.ParseArgs(std::vector<std::string>{"-a"}), args::ValidationError);
REQUIRE_NOTHROW(parser.ParseArgs(std::vector<std::string>{"-g", "-a"}));
REQUIRE_NOTHROW(parser.ParseArgs(std::vector<std::string>{"-h", "-a"}));
REQUIRE_NOTHROW(parser.ParseArgs(std::vector<std::string>{"-gh", "-a"}));
// Xor stuff
REQUIRE_THROWS_AS(parser.ParseArgs(std::vector<std::string>{"-g"}), args::ValidationError);
REQUIRE_NOTHROW(parser.ParseArgs(std::vector<std::string>{"-h", "-b"}));
REQUIRE_THROWS_AS(parser.ParseArgs(std::vector<std::string>{"-g", "-ab"}), args::ValidationError);
REQUIRE_THROWS_AS(parser.ParseArgs(std::vector<std::string>{"-g", "-ac"}), args::ValidationError);
REQUIRE_THROWS_AS(parser.ParseArgs(std::vector<std::string>{"-g", "-abc"}), args::ValidationError);
// Nxor stuff
REQUIRE_NOTHROW(parser.ParseArgs(std::vector<std::string>{"-h", "-a"}));
REQUIRE_NOTHROW(parser.ParseArgs(std::vector<std::string>{"-h", "-adef"}));
REQUIRE_THROWS_AS(parser.ParseArgs(std::vector<std::string>{"-g", "-ad"}), args::ValidationError);
REQUIRE_THROWS_AS(parser.ParseArgs(std::vector<std::string>{"-g", "-adf"}), args::ValidationError);
REQUIRE_THROWS_AS(parser.ParseArgs(std::vector<std::string>{"-g", "-aef"}), args::ValidationError);
}
TEST_CASE("Argument groups should nest", "[args]")
{
args::ArgumentParser parser("This is a test program.", "This goes after the options.");
args::Group xorgroup(parser, "this group provides xor validation", args::Group::Validators::Xor);
args::Flag a(xorgroup, "a", "test flag", args::Matcher({'a'}));
args::Flag b(xorgroup, "b", "test flag", args::Matcher({'b'}));
args::Flag c(xorgroup, "c", "test flag", args::Matcher({'c'}));
args::Group nxor(xorgroup, "this group provides all-or-none (nxor) validation", args::Group::Validators::AllOrNone);
args::Flag d(nxor, "d", "test flag", args::Matcher({'d'}));
args::Flag e(nxor, "e", "test flag", args::Matcher({'e'}));
args::Flag f(nxor, "f", "test flag", args::Matcher({'f'}));
args::Group atleastone(xorgroup, "this group provides at-least-one validation", args::Group::Validators::AtLeastOne);
args::Flag g(atleastone, "g", "test flag", args::Matcher({'g'}));
args::Flag h(atleastone, "h", "test flag", args::Matcher({'h'}));
// Nothing actually matches, because nxor validates properly when it's empty,
REQUIRE_NOTHROW(parser.ParseArgs(std::vector<std::string>{}));
REQUIRE_NOTHROW(parser.ParseArgs(std::vector<std::string>{"-a", "-d"}));
REQUIRE_NOTHROW(parser.ParseArgs(std::vector<std::string>{"-c", "-f"}));
REQUIRE_NOTHROW(parser.ParseArgs(std::vector<std::string>{"-de", "-f"}));
REQUIRE_NOTHROW(parser.ParseArgs(std::vector<std::string>{"-gh", "-f"}));
REQUIRE_THROWS_AS(parser.ParseArgs(std::vector<std::string>{"-g"}), args::ValidationError);
REQUIRE_THROWS_AS(parser.ParseArgs(std::vector<std::string>{"-a"}), args::ValidationError);
REQUIRE_THROWS_AS(parser.ParseArgs(std::vector<std::string>{"-b"}), args::ValidationError);
REQUIRE_THROWS_AS(parser.ParseArgs(std::vector<std::string>{"-a", "-dg"}), args::ValidationError);
}
void DoublesReader(const std::string &name, const std::string &value, std::tuple<double, double> &destination)
{
size_t commapos = 0;
std::get<0>(destination) = std::stod(value, &commapos);
std::get<1>(destination) = std::stod(std::string(value, commapos + 1));
}
TEST_CASE("Custom types work", "[args]")
{
{
args::ArgumentParser parser("This is a test program.");
args::PosArg<std::tuple<int, int>> ints(parser, "INTS", "This takes a pair of integers.");
args::PosArg<std::tuple<double, double>, DoublesReader> doubles(parser, "DOUBLES", "This takes a pair of doubles.");
REQUIRE_THROWS_AS(parser.ParseArgs(std::vector<std::string>{"1.2,2", "3.8,4"}), args::ParseError);
}
args::ArgumentParser parser("This is a test program.");
args::PosArg<std::tuple<int, int>> ints(parser, "INTS", "This takes a pair of integers.");
args::PosArg<std::tuple<double, double>, DoublesReader> doubles(parser, "DOUBLES", "This takes a pair of doubles.");
parser.ParseArgs(std::vector<std::string>{"1,2", "3.8,4"});
REQUIRE(std::get<0>(args::get(ints)) == 1);
REQUIRE(std::get<1>(args::get(ints)) == 2);
REQUIRE((std::get<0>(args::get(doubles)) > 3.79 && std::get<0>(args::get(doubles)) < 3.81));
REQUIRE((std::get<1>(args::get(doubles)) > 3.99 && std::get<1>(args::get(doubles)) < 4.01));
}
TEST_CASE("Custom parser prefixes (dd-style)", "[args]")
{
args::ArgumentParser parser("This command likes to break your disks");
parser.LongPrefix("");
parser.LongSeparator("=");
args::HelpFlag help(parser, "HELP", "Show this help menu.", args::Matcher({"help"}));
args::ArgFlag<long> bs(parser, "BYTES", "Block size", args::Matcher({"bs"}), 512);
args::ArgFlag<long> skip(parser, "BYTES", "Bytes to skip", args::Matcher({"skip"}), 0);
args::ArgFlag<std::string> input(parser, "BLOCK SIZE", "Block size", args::Matcher({"if"}));
args::ArgFlag<std::string> output(parser, "BLOCK SIZE", "Block size", args::Matcher({"of"}));
parser.ParseArgs(std::vector<std::string>{"skip=8", "if=/dev/null"});
REQUIRE_FALSE(bs);
REQUIRE(args::get(bs) == 512);
REQUIRE(skip);
REQUIRE(args::get(skip) == 8);
REQUIRE(input);
REQUIRE(args::get(input) == "/dev/null");
REQUIRE_FALSE(output);
}
TEST_CASE("Custom parser prefixes (Some Windows styles)", "[args]")
{
args::ArgumentParser parser("This command likes to break your disks");
parser.LongPrefix("/");
parser.LongSeparator(":");
args::HelpFlag help(parser, "HELP", "Show this help menu.", args::Matcher({"help"}));
args::ArgFlag<long> bs(parser, "BYTES", "Block size", args::Matcher({"bs"}), 512);
args::ArgFlag<long> skip(parser, "BYTES", "Bytes to skip", args::Matcher({"skip"}), 0);
args::ArgFlag<std::string> input(parser, "BLOCK SIZE", "Block size", args::Matcher({"if"}));
args::ArgFlag<std::string> output(parser, "BLOCK SIZE", "Block size", args::Matcher({"of"}));
parser.ParseArgs(std::vector<std::string>{"/skip:8", "/if:/dev/null"});
REQUIRE_FALSE(bs);
REQUIRE(args::get(bs) == 512);
REQUIRE(skip);
REQUIRE(args::get(skip) == 8);
REQUIRE(input);
REQUIRE(args::get(input) == "/dev/null");
REQUIRE_FALSE(output);
}
TEST_CASE("Help menu can be grabbed as a string, passed into a stream, or by using the overloaded stream operator", "[args]")
{
std::ostream null(nullptr);
args::ArgumentParser parser("This command likes to break your disks");
args::HelpFlag help(parser, "HELP", "Show this help menu.", args::Matcher({"help"}));
args::ArgFlag<long> bs(parser, "BYTES", "Block size", args::Matcher({"bs"}), 512);
args::ArgFlag<long> skip(parser, "BYTES", "Bytes to skip", args::Matcher({"skip"}), 0);
args::ArgFlag<std::string> input(parser, "BLOCK SIZE", "Block size", args::Matcher({"if"}));
args::ArgFlag<std::string> output(parser, "BLOCK SIZE", "Block size", args::Matcher({"of"}));
parser.ParseArgs(std::vector<std::string>{"--skip=8", "--if=/dev/null"});
null << parser.Help();
parser.Help(null);
null << parser;
}
TEST_CASE("Required argument separation being violated throws an error", "[args]")
{
args::ArgumentParser parser("This is a test program.", "This goes after the options.");
args::ArgFlag<std::string> bar(parser, "BAR", "test flag", args::Matcher{'b', "bar"});
REQUIRE_NOTHROW(parser.ParseArgs(std::vector<std::string>{"-btest"}));
REQUIRE_NOTHROW(parser.ParseArgs(std::vector<std::string>{"--bar=test"}));
REQUIRE_NOTHROW(parser.ParseArgs(std::vector<std::string>{"-b", "test"}));
REQUIRE_NOTHROW(parser.ParseArgs(std::vector<std::string>{"--bar", "test"}));
parser.SetArgumentSeparations(true, false, false, false);
REQUIRE_NOTHROW(parser.ParseArgs(std::vector<std::string>{"-btest"}));
REQUIRE_THROWS_AS(parser.ParseArgs(std::vector<std::string>{"--bar=test"}), args::ParseError);
REQUIRE_THROWS_AS(parser.ParseArgs(std::vector<std::string>{"-b", "test"}), args::ParseError);
REQUIRE_THROWS_AS(parser.ParseArgs(std::vector<std::string>{"--bar", "test"}), args::ParseError);
parser.SetArgumentSeparations(false, true, false, false);
REQUIRE_THROWS_AS(parser.ParseArgs(std::vector<std::string>{"-btest"}), args::ParseError);
REQUIRE_NOTHROW(parser.ParseArgs(std::vector<std::string>{"--bar=test"}));
REQUIRE_THROWS_AS(parser.ParseArgs(std::vector<std::string>{"-b", "test"}), args::ParseError);
REQUIRE_THROWS_AS(parser.ParseArgs(std::vector<std::string>{"--bar", "test"}), args::ParseError);
parser.SetArgumentSeparations(false, false, true, false);
REQUIRE_THROWS_AS(parser.ParseArgs(std::vector<std::string>{"-btest"}), args::ParseError);
REQUIRE_THROWS_AS(parser.ParseArgs(std::vector<std::string>{"--bar=test"}), args::ParseError);
REQUIRE_NOTHROW(parser.ParseArgs(std::vector<std::string>{"-b", "test"}));
REQUIRE_THROWS_AS(parser.ParseArgs(std::vector<std::string>{"--bar", "test"}), args::ParseError);
parser.SetArgumentSeparations(false, false, false, true);
REQUIRE_THROWS_AS(parser.ParseArgs(std::vector<std::string>{"-btest"}), args::ParseError);
REQUIRE_THROWS_AS(parser.ParseArgs(std::vector<std::string>{"--bar=test"}), args::ParseError);
REQUIRE_THROWS_AS(parser.ParseArgs(std::vector<std::string>{"-b", "test"}), args::ParseError);
REQUIRE_NOTHROW(parser.ParseArgs(std::vector<std::string>{"--bar", "test"}));
}
|