Line data Source code
1 : /*
2 : * Files:
3 : * tests/options_parser.cpp
4 : *
5 : * License:
6 : * Copyright (c) 2006-2019 Made to Order Software Corp. All Rights Reserved
7 : *
8 : * https://snapwebsites.org/
9 : * contact@m2osw.com
10 : *
11 : * This program is free software; you can redistribute it and/or modify
12 : * it under the terms of the GNU General Public License as published by
13 : * the Free Software Foundation; either version 2 of the License, or
14 : * (at your option) any later version.
15 : *
16 : * This program is distributed in the hope that it will be useful,
17 : * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 : * GNU General Public License for more details.
20 : *
21 : * You should have received a copy of the GNU General Public License along
22 : * with this program; if not, write to the Free Software Foundation, Inc.,
23 : * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 : *
25 : * Authors:
26 : * Alexis Wilke alexis@m2osw.com
27 : */
28 :
29 : // self
30 : //
31 : #include "main.h"
32 :
33 : // advgetopt lib
34 : //
35 : #include <advgetopt/exception.h>
36 :
37 : // C++ lib
38 : //
39 : #include <fstream>
40 :
41 :
42 :
43 :
44 :
45 6 : CATCH_TEST_CASE("options_parser", "[options][valid]")
46 : {
47 8 : CATCH_START_SECTION("System options only")
48 1 : advgetopt::options_environment environment_options;
49 1 : environment_options.f_project_name = "unittest";
50 1 : environment_options.f_options = nullptr;
51 1 : environment_options.f_environment_flags = advgetopt::GETOPT_ENVIRONMENT_FLAG_SYSTEM_PARAMETERS;
52 1 : environment_options.f_help_header = "Usage: test valid options from system options only";
53 :
54 : char const * cargv[] =
55 : {
56 : "tests/options-parser",
57 : "--license",
58 : nullptr
59 1 : };
60 1 : int const argc(sizeof(cargv) / sizeof(cargv[0]) - 1);
61 1 : char ** argv = const_cast<char **>(cargv);
62 :
63 2 : advgetopt::getopt opt(environment_options, argc, argv);
64 :
65 : // check that the result is valid
66 :
67 : // an invalid parameter, MUST NOT EXIST
68 1 : CATCH_REQUIRE(opt.get_option("invalid-parameter") == nullptr);
69 1 : CATCH_REQUIRE(opt.get_option('Z') == nullptr);
70 1 : CATCH_REQUIRE_FALSE(opt.is_defined("invalid-parameter"));
71 1 : CATCH_REQUIRE(opt.get_default("invalid-parameter").empty());
72 1 : CATCH_REQUIRE(opt.size("invalid-parameter") == 0);
73 :
74 : // the valid parameter
75 1 : CATCH_REQUIRE(opt.get_option("verbose") == nullptr);
76 1 : CATCH_REQUIRE(opt.get_option('v') == nullptr);
77 1 : CATCH_REQUIRE_FALSE(opt.is_defined("verbose"));
78 1 : CATCH_REQUIRE(opt.get_default("verbose").empty());
79 1 : CATCH_REQUIRE(opt.size("verbose") == 0);
80 :
81 : // "--help"
82 1 : CATCH_REQUIRE(opt.get_option("help") != nullptr);
83 1 : CATCH_REQUIRE(opt.get_option('h') != nullptr);
84 1 : CATCH_REQUIRE_FALSE(opt.is_defined("help"));
85 1 : CATCH_REQUIRE(opt.get_default("help").empty());
86 1 : CATCH_REQUIRE(opt.size("help") == 0);
87 :
88 : // "--version"
89 1 : CATCH_REQUIRE(opt.get_option("version") != nullptr);
90 1 : CATCH_REQUIRE(opt.get_option('V') != nullptr);
91 1 : CATCH_REQUIRE(opt.get_option('V') == opt.get_option("version"));
92 1 : CATCH_REQUIRE_FALSE(opt.is_defined("version"));
93 1 : CATCH_REQUIRE(opt.get_default("version").empty());
94 1 : CATCH_REQUIRE(opt.size("version") == 0);
95 :
96 : // "--copyright"
97 1 : CATCH_REQUIRE(opt.get_option("copyright") != nullptr);
98 1 : CATCH_REQUIRE(opt.get_option('C') != nullptr);
99 1 : CATCH_REQUIRE_FALSE(opt.is_defined("copyright"));
100 1 : CATCH_REQUIRE(opt.get_default("copyright").empty());
101 1 : CATCH_REQUIRE(opt.size("copyright") == 0);
102 :
103 : // "--license"
104 1 : CATCH_REQUIRE(opt.get_option("license") != nullptr);
105 1 : CATCH_REQUIRE(opt.get_option('L') != nullptr);
106 1 : CATCH_REQUIRE(opt.is_defined("license"));
107 1 : CATCH_REQUIRE(opt.get_string("license").empty());
108 1 : CATCH_REQUIRE(opt.get_default("license").empty());
109 1 : CATCH_REQUIRE(opt.size("license") == 1);
110 :
111 : // "--build-date"
112 1 : CATCH_REQUIRE(opt.get_option("build-date") != nullptr);
113 1 : CATCH_REQUIRE_FALSE(opt.is_defined("build-date"));
114 1 : CATCH_REQUIRE(opt.get_default("build-date").empty());
115 1 : CATCH_REQUIRE(opt.size("build-date") == 0);
116 :
117 : // "--environment-variable-name"
118 1 : CATCH_REQUIRE(opt.get_option("environment-variable-name") != nullptr);
119 1 : CATCH_REQUIRE_FALSE(opt.is_defined("environment-variable-name"));
120 1 : CATCH_REQUIRE(opt.get_default("environment-variable-name").empty());
121 1 : CATCH_REQUIRE(opt.size("environment-variable-name") == 0);
122 :
123 : // "--configuration-filename"
124 1 : CATCH_REQUIRE(opt.get_option("configuration-filenames") != nullptr);
125 1 : CATCH_REQUIRE_FALSE(opt.is_defined("configuration-filenames"));
126 1 : CATCH_REQUIRE(opt.get_default("configuration-filenames").empty());
127 1 : CATCH_REQUIRE(opt.size("configuration-filenames") == 0);
128 :
129 : // "--path-to-option-definitions"
130 1 : CATCH_REQUIRE(opt.get_option("path-to-option-definitions") != nullptr);
131 1 : CATCH_REQUIRE_FALSE(opt.is_defined("path-to-option-definitions"));
132 1 : CATCH_REQUIRE(opt.get_default("path-to-option-definitions").empty());
133 1 : CATCH_REQUIRE(opt.size("path-to-option-definitions") == 0);
134 :
135 :
136 : // other parameters
137 1 : CATCH_REQUIRE(opt.get_program_name() == "options-parser");
138 1 : CATCH_REQUIRE(opt.get_program_fullname() == "tests/options-parser");
139 : CATCH_END_SECTION()
140 :
141 8 : CATCH_START_SECTION("Duplicated options (ignored by system options)")
142 : advgetopt::option const options[] =
143 : {
144 : advgetopt::define_option(
145 : advgetopt::Name("verbose")
146 : , advgetopt::ShortName('V') // duplicate version short name
147 : , advgetopt::Flags(advgetopt::standalone_command_flags())
148 : , advgetopt::Help("print info as we work.")
149 : ),
150 : advgetopt::define_option(
151 : advgetopt::Name("copyright") // duplicate copyright
152 : , advgetopt::Flags(advgetopt::standalone_command_flags())
153 : , advgetopt::Help("print info as we work.")
154 : ),
155 : advgetopt::end_options()
156 1 : };
157 :
158 1 : advgetopt::options_environment environment_options;
159 1 : environment_options.f_project_name = "unittest";
160 1 : environment_options.f_options = options;
161 1 : environment_options.f_environment_flags = advgetopt::GETOPT_ENVIRONMENT_FLAG_SYSTEM_PARAMETERS;
162 1 : environment_options.f_help_header = "Usage: test valid options with duplicates";
163 :
164 : char const * cargv[] =
165 : {
166 : "options-parser",
167 : "--verbose",
168 : "--license",
169 : nullptr
170 1 : };
171 1 : int const argc(sizeof(cargv) / sizeof(cargv[0]) - 1);
172 1 : char ** argv = const_cast<char **>(cargv);
173 :
174 2 : advgetopt::getopt opt(environment_options, argc, argv);
175 :
176 : // check that the result is valid
177 :
178 : // an invalid parameter, MUST NOT EXIST
179 1 : CATCH_REQUIRE(opt.get_option("invalid-parameter") == nullptr);
180 1 : CATCH_REQUIRE(opt.get_option('Z') == nullptr);
181 1 : CATCH_REQUIRE_FALSE(opt.is_defined("invalid-parameter"));
182 1 : CATCH_REQUIRE(opt.get_default("invalid-parameter").empty());
183 1 : CATCH_REQUIRE(opt.size("invalid-parameter") == 0);
184 :
185 : // the valid parameter
186 1 : CATCH_REQUIRE(opt.get_option("verbose") != nullptr);
187 1 : CATCH_REQUIRE(opt.get_option('V') != nullptr);
188 1 : CATCH_REQUIRE(opt.is_defined("verbose"));
189 1 : CATCH_REQUIRE(opt.get_default("verbose").empty());
190 1 : CATCH_REQUIRE(opt.size("verbose") == 1);
191 :
192 : // "--help"
193 1 : CATCH_REQUIRE(opt.get_option("help") != nullptr);
194 1 : CATCH_REQUIRE(opt.get_option('h') != nullptr);
195 1 : CATCH_REQUIRE_FALSE(opt.is_defined("help"));
196 1 : CATCH_REQUIRE(opt.get_default("help").empty());
197 1 : CATCH_REQUIRE(opt.size("help") == 0);
198 :
199 : // "--version"
200 1 : CATCH_REQUIRE(opt.get_option("version") != nullptr);
201 1 : CATCH_REQUIRE(opt.get_option('V') != nullptr); // 'V' is defined, but it's for "verbose"...
202 1 : CATCH_REQUIRE(opt.get_option('V') != opt.get_option("version"));
203 1 : CATCH_REQUIRE(opt.get_option('V') == opt.get_option("verbose"));
204 1 : CATCH_REQUIRE_FALSE(opt.is_defined("version"));
205 1 : CATCH_REQUIRE(opt.get_default("version").empty());
206 1 : CATCH_REQUIRE(opt.size("version") == 0);
207 :
208 : // "--copyright"
209 1 : CATCH_REQUIRE(opt.get_option("copyright") != nullptr);
210 1 : CATCH_REQUIRE(opt.get_option('C') == nullptr); // no short name in our definition (which overwrites the system definition)
211 1 : CATCH_REQUIRE_FALSE(opt.is_defined("copyright"));
212 1 : CATCH_REQUIRE(opt.get_default("copyright").empty());
213 1 : CATCH_REQUIRE(opt.size("copyright") == 0);
214 :
215 : // "--license"
216 1 : CATCH_REQUIRE(opt.get_option("license") != nullptr);
217 1 : CATCH_REQUIRE(opt.get_option('L') != nullptr);
218 1 : CATCH_REQUIRE(opt.is_defined("license"));
219 1 : CATCH_REQUIRE(opt.get_string("license").empty());
220 1 : CATCH_REQUIRE(opt.get_default("license").empty());
221 1 : CATCH_REQUIRE(opt.size("license") == 1);
222 :
223 : // "--build-date"
224 1 : CATCH_REQUIRE(opt.get_option("build-date") != nullptr);
225 1 : CATCH_REQUIRE_FALSE(opt.is_defined("build-date"));
226 1 : CATCH_REQUIRE(opt.get_default("build-date").empty());
227 1 : CATCH_REQUIRE(opt.size("build-date") == 0);
228 :
229 : // "--environment-variable-name"
230 1 : CATCH_REQUIRE(opt.get_option("environment-variable-name") != nullptr);
231 1 : CATCH_REQUIRE_FALSE(opt.is_defined("environment-variable-name"));
232 1 : CATCH_REQUIRE(opt.get_default("environment-variable-name").empty());
233 1 : CATCH_REQUIRE(opt.size("environment-variable-name") == 0);
234 :
235 : // "--configuration-filename"
236 1 : CATCH_REQUIRE(opt.get_option("configuration-filenames") != nullptr);
237 1 : CATCH_REQUIRE_FALSE(opt.is_defined("configuration-filenames"));
238 1 : CATCH_REQUIRE(opt.get_default("configuration-filenames").empty());
239 1 : CATCH_REQUIRE(opt.size("configuration-filenames") == 0);
240 :
241 : // "--path-to-option-definitions"
242 1 : CATCH_REQUIRE(opt.get_option("path-to-option-definitions") != nullptr);
243 1 : CATCH_REQUIRE_FALSE(opt.is_defined("path-to-option-definitions"));
244 1 : CATCH_REQUIRE(opt.get_default("path-to-option-definitions").empty());
245 1 : CATCH_REQUIRE(opt.size("path-to-option-definitions") == 0);
246 :
247 : // other parameters
248 1 : CATCH_REQUIRE(opt.get_program_name() == "options-parser");
249 1 : CATCH_REQUIRE(opt.get_program_fullname() == "options-parser");
250 : CATCH_END_SECTION()
251 :
252 8 : CATCH_START_SECTION("Default option")
253 : advgetopt::option const options[] =
254 : {
255 : advgetopt::define_option(
256 : advgetopt::Name("verbose")
257 : , advgetopt::ShortName('v')
258 : , advgetopt::Flags(advgetopt::standalone_command_flags())
259 : , advgetopt::Help("print info as we work.")
260 : ),
261 : advgetopt::define_option(
262 : advgetopt::Name("filenames")
263 : , advgetopt::Flags(advgetopt::command_flags<advgetopt::GETOPT_FLAG_REQUIRED, advgetopt::GETOPT_FLAG_MULTIPLE, advgetopt::GETOPT_FLAG_DEFAULT_OPTION>())
264 : , advgetopt::Help("enter a list of filenames.")
265 : , advgetopt::DefaultValue("a.out")
266 : ),
267 : advgetopt::end_options()
268 1 : };
269 :
270 1 : advgetopt::options_environment environment_options;
271 1 : environment_options.f_project_name = "unittest";
272 1 : environment_options.f_options = options;
273 1 : environment_options.f_environment_flags = 0;
274 1 : environment_options.f_help_header = "Usage: test valid options with duplicates";
275 :
276 : char const * cargv[] =
277 : {
278 : "/usr/bin/options-parser",
279 : "file1",
280 : "file2",
281 : "file3",
282 : "file4",
283 : "file5",
284 : nullptr
285 1 : };
286 1 : int const argc(sizeof(cargv) / sizeof(cargv[0]) - 1);
287 1 : char ** argv = const_cast<char **>(cargv);
288 :
289 2 : advgetopt::getopt opt(environment_options, argc, argv);
290 :
291 : // check that the result is valid
292 :
293 : // an invalid parameter, MUST NOT EXIST
294 1 : CATCH_REQUIRE(opt.get_option("invalid-parameter") == nullptr);
295 1 : CATCH_REQUIRE(opt.get_option('Z') == nullptr);
296 1 : CATCH_REQUIRE_FALSE(opt.is_defined("invalid-parameter"));
297 1 : CATCH_REQUIRE(opt.get_default("invalid-parameter").empty());
298 1 : CATCH_REQUIRE(opt.size("invalid-parameter") == 0);
299 :
300 : // the valid parameter
301 1 : CATCH_REQUIRE(opt.get_option("verbose") != nullptr);
302 1 : CATCH_REQUIRE(opt.get_option('v') != nullptr);
303 1 : CATCH_REQUIRE_FALSE(opt.is_defined("verbose"));
304 1 : CATCH_REQUIRE(opt.get_default("verbose").empty());
305 1 : CATCH_REQUIRE(opt.size("verbose") == 0);
306 :
307 : // "--help"
308 1 : CATCH_REQUIRE(opt.get_option("help") == nullptr);
309 1 : CATCH_REQUIRE(opt.get_option('h') == nullptr);
310 1 : CATCH_REQUIRE_FALSE(opt.is_defined("help"));
311 1 : CATCH_REQUIRE(opt.get_default("help").empty());
312 1 : CATCH_REQUIRE(opt.size("help") == 0);
313 :
314 : // "--version"
315 1 : CATCH_REQUIRE(opt.get_option("version") == nullptr);
316 1 : CATCH_REQUIRE(opt.get_option('V') == nullptr); // 'V' is defined, but it's for "verbose"...
317 1 : CATCH_REQUIRE_FALSE(opt.is_defined("version"));
318 1 : CATCH_REQUIRE(opt.get_default("version").empty());
319 1 : CATCH_REQUIRE(opt.size("version") == 0);
320 :
321 : // "--copyright"
322 1 : CATCH_REQUIRE(opt.get_option("copyright") == nullptr);
323 1 : CATCH_REQUIRE(opt.get_option('C') == nullptr); // no short name in our definition (which overwrites the system definition)
324 1 : CATCH_REQUIRE_FALSE(opt.is_defined("copyright"));
325 1 : CATCH_REQUIRE(opt.get_default("copyright").empty());
326 1 : CATCH_REQUIRE(opt.size("copyright") == 0);
327 :
328 : // "--license"
329 1 : CATCH_REQUIRE(opt.get_option("license") == nullptr);
330 1 : CATCH_REQUIRE(opt.get_option('L') == nullptr);
331 1 : CATCH_REQUIRE_FALSE(opt.is_defined("license"));
332 1 : CATCH_REQUIRE(opt.get_default("license").empty());
333 1 : CATCH_REQUIRE(opt.size("license") == 0);
334 :
335 : // "--build-date"
336 1 : CATCH_REQUIRE(opt.get_option("build-date") == nullptr);
337 1 : CATCH_REQUIRE_FALSE(opt.is_defined("build-date"));
338 1 : CATCH_REQUIRE(opt.get_default("build-date").empty());
339 1 : CATCH_REQUIRE(opt.size("build-date") == 0);
340 :
341 : // "--environment-variable-name"
342 1 : CATCH_REQUIRE(opt.get_option("environment-variable-name") == nullptr);
343 1 : CATCH_REQUIRE_FALSE(opt.is_defined("environment-variable-name"));
344 1 : CATCH_REQUIRE_FALSE(opt.has_default("environment-variable-name"));
345 1 : CATCH_REQUIRE(opt.get_default("environment-variable-name").empty());
346 1 : CATCH_REQUIRE(opt.size("environment-variable-name") == 0);
347 :
348 : // "--configuration-filename"
349 1 : CATCH_REQUIRE(opt.get_option("configuration-filenames") == nullptr);
350 1 : CATCH_REQUIRE_FALSE(opt.is_defined("configuration-filenames"));
351 1 : CATCH_REQUIRE_FALSE(opt.has_default("configuration-filenames"));
352 1 : CATCH_REQUIRE(opt.get_default("configuration-filenames").empty());
353 1 : CATCH_REQUIRE(opt.size("configuration-filenames") == 0);
354 :
355 : // "--path-to-option-definitions"
356 1 : CATCH_REQUIRE(opt.get_option("path-to-option-definitions") == nullptr);
357 1 : CATCH_REQUIRE_FALSE(opt.is_defined("path-to-option-definitions"));
358 1 : CATCH_REQUIRE(opt.get_default("path-to-option-definitions").empty());
359 1 : CATCH_REQUIRE(opt.size("path-to-option-definitions") == 0);
360 :
361 : // "--configuration-filename"
362 1 : CATCH_REQUIRE(opt.get_option("filenames") != nullptr);
363 1 : CATCH_REQUIRE(opt.is_defined("filenames"));
364 1 : CATCH_REQUIRE(opt.get_string("filenames") == "file1");
365 1 : CATCH_REQUIRE(opt.get_string("filenames", 0) == "file1");
366 1 : CATCH_REQUIRE(opt.get_string("filenames", 1) == "file2");
367 1 : CATCH_REQUIRE(opt.get_string("filenames", 2) == "file3");
368 1 : CATCH_REQUIRE(opt.get_string("filenames", 3) == "file4");
369 1 : CATCH_REQUIRE(opt.get_string("filenames", 4) == "file5");
370 1 : CATCH_REQUIRE(opt.has_default("filenames"));
371 1 : CATCH_REQUIRE(opt.get_default("filenames") == "a.out");
372 1 : CATCH_REQUIRE(opt.size("filenames") == 5);
373 :
374 : // other parameters
375 1 : CATCH_REQUIRE(opt.get_program_name() == "options-parser");
376 1 : CATCH_REQUIRE(opt.get_program_fullname() == "/usr/bin/options-parser");
377 : CATCH_END_SECTION()
378 :
379 8 : CATCH_START_SECTION("Alias option")
380 : advgetopt::option const options[] =
381 : {
382 : advgetopt::define_option(
383 : advgetopt::Name("verbose")
384 : , advgetopt::ShortName(U'v')
385 : , advgetopt::Flags(advgetopt::standalone_command_flags())
386 : , advgetopt::Help("print info as we work.")
387 : ),
388 : advgetopt::define_option(
389 : advgetopt::Name("licence") // to allow French spelling
390 : , advgetopt::Alias("license")
391 : , advgetopt::Flags(advgetopt::standalone_command_flags<advgetopt::GETOPT_FLAG_GROUP_COMMANDS>())
392 : ),
393 : advgetopt::end_options()
394 1 : };
395 :
396 1 : advgetopt::options_environment environment_options;
397 1 : environment_options.f_project_name = "unittest";
398 1 : environment_options.f_options = options;
399 1 : environment_options.f_environment_flags = advgetopt::GETOPT_ENVIRONMENT_FLAG_SYSTEM_PARAMETERS;
400 1 : environment_options.f_help_header = "Usage: test valid options with duplicates";
401 :
402 : char const * cargv[] =
403 : {
404 : "options-parser",
405 : "--verbose",
406 : "--license",
407 : nullptr
408 1 : };
409 1 : int const argc(sizeof(cargv) / sizeof(cargv[0]) - 1);
410 1 : char ** argv = const_cast<char **>(cargv);
411 :
412 2 : advgetopt::getopt opt(environment_options, argc, argv);
413 :
414 : // check that the result is valid
415 :
416 : // an invalid parameter, MUST NOT EXIST
417 1 : CATCH_REQUIRE(opt.get_option("invalid-parameter") == nullptr);
418 1 : CATCH_REQUIRE(opt.get_option(U'Z') == nullptr);
419 1 : CATCH_REQUIRE_FALSE(opt.is_defined("invalid-parameter"));
420 1 : CATCH_REQUIRE_FALSE(opt.has_default("invalid-parameter"));
421 1 : CATCH_REQUIRE(opt.get_default("invalid-parameter").empty());
422 1 : CATCH_REQUIRE(opt.size("invalid-parameter") == 0);
423 :
424 : // the valid parameter
425 1 : CATCH_REQUIRE(opt.get_option("verbose") != nullptr);
426 1 : CATCH_REQUIRE(opt.get_option(U'v') != nullptr);
427 1 : CATCH_REQUIRE(opt.is_defined("verbose"));
428 1 : CATCH_REQUIRE_FALSE(opt.has_default("verbose"));
429 1 : CATCH_REQUIRE(opt.get_default("verbose").empty());
430 1 : CATCH_REQUIRE(opt.size("verbose") == 1);
431 :
432 : // "--help"
433 1 : CATCH_REQUIRE(opt.get_option("help") != nullptr);
434 1 : CATCH_REQUIRE(opt.get_option(U'h') != nullptr);
435 1 : CATCH_REQUIRE_FALSE(opt.is_defined("help"));
436 1 : CATCH_REQUIRE_FALSE(opt.has_default("help"));
437 1 : CATCH_REQUIRE(opt.get_default("help").empty());
438 1 : CATCH_REQUIRE(opt.size("help") == 0);
439 :
440 : // "--version"
441 1 : CATCH_REQUIRE(opt.get_option("version") != nullptr);
442 1 : CATCH_REQUIRE(opt.get_option(U'V') != nullptr); // 'V' is defined, but it's for "verbose"...
443 1 : CATCH_REQUIRE(opt.get_option(U'V') == opt.get_option("version"));
444 1 : CATCH_REQUIRE(opt.get_option(U'V') != opt.get_option("verbose"));
445 1 : CATCH_REQUIRE_FALSE(opt.is_defined("version"));
446 1 : CATCH_REQUIRE_FALSE(opt.has_default("version"));
447 1 : CATCH_REQUIRE(opt.get_default("version").empty());
448 1 : CATCH_REQUIRE(opt.size("version") == 0);
449 :
450 : // "--copyright"
451 1 : CATCH_REQUIRE(opt.get_option("copyright") != nullptr);
452 1 : CATCH_REQUIRE(opt.get_option(U'C') != nullptr); // no short name in our definition (which overwrites the system definition)
453 1 : CATCH_REQUIRE_FALSE(opt.is_defined("copyright"));
454 1 : CATCH_REQUIRE_FALSE(opt.has_default("copyright"));
455 1 : CATCH_REQUIRE(opt.get_default("copyright").empty());
456 1 : CATCH_REQUIRE(opt.size("copyright") == 0);
457 :
458 : // "--license"
459 1 : CATCH_REQUIRE(opt.get_option("license") != nullptr);
460 1 : CATCH_REQUIRE(opt.get_option(U'L') != nullptr);
461 1 : CATCH_REQUIRE(opt.is_defined("license"));
462 1 : CATCH_REQUIRE(opt.get_string("license").empty());
463 1 : CATCH_REQUIRE_FALSE(opt.has_default("license"));
464 1 : CATCH_REQUIRE(opt.get_default("license").empty());
465 1 : CATCH_REQUIRE(opt.size("license") == 1);
466 :
467 : // "--build-date"
468 1 : CATCH_REQUIRE(opt.get_option("build-date") != nullptr);
469 1 : CATCH_REQUIRE_FALSE(opt.is_defined("build-date"));
470 1 : CATCH_REQUIRE_FALSE(opt.has_default("build-date"));
471 1 : CATCH_REQUIRE(opt.get_default("build-date").empty());
472 1 : CATCH_REQUIRE(opt.size("build-date") == 0);
473 :
474 : // "--environment-variable-name"
475 1 : CATCH_REQUIRE(opt.get_option("environment-variable-name") != nullptr);
476 1 : CATCH_REQUIRE_FALSE(opt.is_defined("environment-variable-name"));
477 1 : CATCH_REQUIRE(opt.get_default("environment-variable-name").empty());
478 1 : CATCH_REQUIRE(opt.size("environment-variable-name") == 0);
479 :
480 : // "--configuration-filename"
481 1 : CATCH_REQUIRE(opt.get_option("configuration-filenames") != nullptr);
482 1 : CATCH_REQUIRE_FALSE(opt.is_defined("configuration-filenames"));
483 1 : CATCH_REQUIRE_FALSE(opt.has_default("configuration-filenames"));
484 1 : CATCH_REQUIRE(opt.get_default("configuration-filenames").empty());
485 1 : CATCH_REQUIRE(opt.size("configuration-filenames") == 0);
486 :
487 : // "--path-to-option-definitions"
488 1 : CATCH_REQUIRE(opt.get_option("path-to-option-definitions") != nullptr);
489 1 : CATCH_REQUIRE_FALSE(opt.is_defined("path-to-option-definitions"));
490 1 : CATCH_REQUIRE_FALSE(opt.has_default("path-to-option-definitions"));
491 1 : CATCH_REQUIRE(opt.get_default("path-to-option-definitions").empty());
492 1 : CATCH_REQUIRE(opt.size("path-to-option-definitions") == 0);
493 :
494 : // other parameters
495 1 : CATCH_REQUIRE(opt.get_program_name() == "options-parser");
496 1 : CATCH_REQUIRE(opt.get_program_fullname() == "options-parser");
497 : CATCH_END_SECTION()
498 4 : }
499 :
500 :
501 :
502 :
503 :
504 3 : CATCH_TEST_CASE("define_option_short_name", "[options][valid][config]")
505 : {
506 2 : CATCH_START_SECTION("Test adding '-<gear>' to '--config-dir'")
507 : {
508 : advgetopt::option const options[] =
509 : {
510 : advgetopt::define_option(
511 : advgetopt::Name("user")
512 : , advgetopt::ShortName('u')
513 : , advgetopt::Flags(advgetopt::command_flags<advgetopt::GETOPT_FLAG_REQUIRED>())
514 : , advgetopt::Help("user name.")
515 : ),
516 : advgetopt::end_options()
517 1 : };
518 :
519 1 : advgetopt::options_environment environment_options;
520 1 : environment_options.f_project_name = "unittest";
521 1 : environment_options.f_options = options;
522 1 : environment_options.f_environment_flags = advgetopt::GETOPT_ENVIRONMENT_FLAG_SYSTEM_PARAMETERS;
523 1 : environment_options.f_configuration_filename = "snaplog.conf";
524 1 : environment_options.f_help_header = "Usage: test --config-dir";
525 :
526 : char const * cargv[] =
527 : {
528 : "/usr/bin/arguments",
529 : "-u",
530 : "alexis",
531 : "-L",
532 : "-\xE2\x9A\x99", // GEAR character
533 : "/etc/secret/config",
534 : nullptr
535 1 : };
536 1 : int const argc(sizeof(cargv) / sizeof(cargv[0]) - 1);
537 1 : char ** argv = const_cast<char **>(cargv);
538 :
539 2 : advgetopt::getopt opt(environment_options);
540 1 : opt.parse_program_name(argv);
541 :
542 1 : CATCH_REQUIRE(opt.get_option("config-dir") != nullptr);
543 1 : opt.set_short_name("config-dir", 0x2699);
544 :
545 1 : opt.parse_arguments(argc, argv);
546 :
547 : // check that the result is valid
548 :
549 : // an invalid parameter, MUST NOT EXIST
550 1 : CATCH_REQUIRE(opt.get_option("invalid-parameter") == nullptr);
551 1 : CATCH_REQUIRE(opt.get_option('Z') == nullptr);
552 1 : CATCH_REQUIRE_FALSE(opt.is_defined("invalid-parameter"));
553 1 : CATCH_REQUIRE(opt.get_default("invalid-parameter").empty());
554 1 : CATCH_REQUIRE(opt.size("invalid-parameter") == 0);
555 :
556 : // the valid parameter
557 1 : CATCH_REQUIRE(opt.get_option("user") != nullptr);
558 1 : CATCH_REQUIRE(opt.get_option('u') == opt.get_option("user"));
559 1 : CATCH_REQUIRE(opt.is_defined("user"));
560 1 : CATCH_REQUIRE(opt.get_string("user") == "alexis");
561 1 : CATCH_REQUIRE(opt.get_string("user", 0) == "alexis");
562 1 : CATCH_REQUIRE(opt.get_default("user").empty());
563 1 : CATCH_REQUIRE(opt.size("user") == 1);
564 :
565 : // the license system parameter
566 1 : CATCH_REQUIRE(opt.get_option("license") != nullptr);
567 1 : CATCH_REQUIRE(opt.get_option('L') == opt.get_option("license"));
568 1 : CATCH_REQUIRE(opt.is_defined("license"));
569 1 : CATCH_REQUIRE(opt.get_default("license").empty());
570 1 : CATCH_REQUIRE(opt.size("license") == 1);
571 :
572 : // the config-dir system parameter
573 1 : CATCH_REQUIRE(opt.get_option("config-dir") != nullptr);
574 1 : CATCH_REQUIRE(opt.get_option(static_cast<advgetopt::short_name_t>(0x2699)) == opt.get_option("config-dir"));
575 1 : CATCH_REQUIRE(opt.is_defined("config-dir"));
576 1 : CATCH_REQUIRE(opt.get_default("config-dir").empty());
577 1 : CATCH_REQUIRE(opt.size("config-dir") == 1);
578 1 : CATCH_REQUIRE(opt.get_string("config-dir") == "/etc/secret/config");
579 :
580 : // other parameters
581 1 : CATCH_REQUIRE(opt.get_program_name() == "arguments");
582 1 : CATCH_REQUIRE(opt.get_program_fullname() == "/usr/bin/arguments");
583 : }
584 : CATCH_END_SECTION()
585 1 : }
586 :
587 :
588 :
589 :
590 :
591 :
592 :
593 : // With C++17, all of these invalid cases should be handled in the
594 : // define_option() and option_flags() templates
595 : //
596 12 : CATCH_TEST_CASE("invalid_options_parser", "[options][invalid]")
597 : {
598 20 : CATCH_START_SECTION("No options")
599 1 : advgetopt::options_environment environment_options;
600 1 : environment_options.f_project_name = "unittest";
601 1 : environment_options.f_options = nullptr;
602 1 : environment_options.f_environment_flags = 0;
603 1 : environment_options.f_help_header = "Usage: test detection of no options available at all";
604 :
605 : char const * cargv[] =
606 : {
607 : "tests/no-options-parser",
608 : "--missing",
609 : nullptr
610 1 : };
611 1 : int const argc(sizeof(cargv) / sizeof(cargv[0]) - 1);
612 1 : char ** argv = const_cast<char **>(cargv);
613 :
614 1 : CATCH_REQUIRE_THROWS_MATCHES(std::make_shared<advgetopt::getopt>(environment_options, argc, argv)
615 : , advgetopt::getopt_exception_logic
616 : , Catch::Matchers::ExceptionMessage(
617 : "an empty list of options is not legal, you must"
618 : " defined at least one (i.e. --version, --help...)"));
619 : CATCH_END_SECTION()
620 :
621 20 : CATCH_START_SECTION("Options without a name (null pointer)")
622 : advgetopt::option const options[] =
623 : {
624 : advgetopt::define_option(
625 : advgetopt::Name("verbose")
626 : , advgetopt::ShortName('v')
627 : , advgetopt::Flags(advgetopt::standalone_command_flags())
628 : , advgetopt::Help("print info as we work.")
629 : ),
630 : {
631 : // we have to enter this manually because define_option()
632 : // forces you to enter a name, with C++17, we will also
633 : // be able to verify the whole table at compile time
634 : //
635 : '\0',
636 : advgetopt::GETOPT_FLAG_FLAG,
637 : nullptr,
638 : nullptr,
639 : nullptr,
640 : nullptr,
641 : },
642 : advgetopt::define_option(
643 : advgetopt::Name("licence") // to allow French spelling
644 : , advgetopt::Alias("license")
645 : , advgetopt::Flags(advgetopt::standalone_command_flags())
646 : ),
647 : advgetopt::end_options()
648 1 : };
649 :
650 1 : advgetopt::options_environment environment_options;
651 1 : environment_options.f_project_name = "unittest";
652 1 : environment_options.f_options = options;
653 1 : environment_options.f_environment_flags = 0;
654 1 : environment_options.f_help_header = "Usage: name is nullptr";
655 :
656 : char const * cargv[] =
657 : {
658 : "tests/option-without-a-name",
659 : "--missing-name",
660 : nullptr
661 1 : };
662 1 : int const argc(sizeof(cargv) / sizeof(cargv[0]) - 1);
663 1 : char ** argv = const_cast<char **>(cargv);
664 :
665 1 : CATCH_REQUIRE_THROWS_MATCHES(std::make_shared<advgetopt::getopt>(environment_options, argc, argv)
666 : , advgetopt::getopt_exception_logic
667 : , Catch::Matchers::ExceptionMessage(
668 : "option long name missing or empty."));
669 : CATCH_END_SECTION()
670 :
671 20 : CATCH_START_SECTION("Options without a name (empty string)")
672 : advgetopt::option const options[] =
673 : {
674 : advgetopt::define_option(
675 : advgetopt::Name("verbose")
676 : , advgetopt::ShortName('v')
677 : , advgetopt::Flags(advgetopt::standalone_command_flags())
678 : , advgetopt::Help("print info as we work.")
679 : ),
680 : {
681 : // we have to enter this manually because define_option()
682 : // forces you to enter a name, with C++17, we will also
683 : // be able to verify the whole table at compile time
684 : //
685 : '\0',
686 : advgetopt::GETOPT_FLAG_FLAG,
687 : "",
688 : nullptr,
689 : nullptr,
690 : nullptr,
691 : },
692 : advgetopt::define_option(
693 : advgetopt::Name("licence")
694 : , advgetopt::Flags(advgetopt::standalone_command_flags())
695 : ),
696 : advgetopt::end_options()
697 1 : };
698 :
699 1 : advgetopt::options_environment environment_options;
700 1 : environment_options.f_project_name = "unittest";
701 1 : environment_options.f_options = options;
702 1 : environment_options.f_environment_flags = 0;
703 1 : environment_options.f_help_header = "Usage: name has a string but it's empty";
704 :
705 : char const * cargv[] =
706 : {
707 : "tests/option-without-a-name",
708 : "--missing-name",
709 : nullptr
710 1 : };
711 1 : int const argc(sizeof(cargv) / sizeof(cargv[0]) - 1);
712 1 : char ** argv = const_cast<char **>(cargv);
713 :
714 1 : CATCH_REQUIRE_THROWS_MATCHES(std::make_shared<advgetopt::getopt>(environment_options, argc, argv)
715 : , advgetopt::getopt_exception_logic
716 : , Catch::Matchers::ExceptionMessage(
717 : "option long name missing or empty."));
718 : CATCH_END_SECTION()
719 :
720 20 : CATCH_START_SECTION("Options with a one letter name")
721 : advgetopt::option const options[] =
722 : {
723 : advgetopt::define_option(
724 : advgetopt::Name("verbose")
725 : , advgetopt::ShortName('v')
726 : , advgetopt::Flags(advgetopt::standalone_command_flags())
727 : , advgetopt::Help("print info as we work.")
728 : ),
729 : {
730 : // we have to enter this manually because define_option()
731 : // forces you to enter a name, with C++17, we will also
732 : // be able to verify the whole table at compile time
733 : //
734 : '\0',
735 : advgetopt::GETOPT_FLAG_FLAG,
736 : "h",
737 : nullptr,
738 : nullptr,
739 : nullptr,
740 : },
741 : advgetopt::define_option(
742 : advgetopt::Name("licence")
743 : , advgetopt::Flags(advgetopt::standalone_command_flags())
744 : ),
745 : advgetopt::end_options()
746 1 : };
747 :
748 1 : advgetopt::options_environment environment_options;
749 1 : environment_options.f_project_name = "unittest";
750 1 : environment_options.f_options = options;
751 1 : environment_options.f_environment_flags = 0;
752 1 : environment_options.f_help_header = "Usage: name is only one letter";
753 :
754 : char const * cargv[] =
755 : {
756 : "tests/option-with-name-too-short",
757 : "--missing-name",
758 : nullptr
759 1 : };
760 1 : int const argc(sizeof(cargv) / sizeof(cargv[0]) - 1);
761 1 : char ** argv = const_cast<char **>(cargv);
762 :
763 1 : CATCH_REQUIRE_THROWS_MATCHES(std::make_shared<advgetopt::getopt>(environment_options, argc, argv)
764 : , advgetopt::getopt_exception_logic
765 : , Catch::Matchers::ExceptionMessage(
766 : "a long name option must be at least 2 characters."));
767 : CATCH_END_SECTION()
768 :
769 20 : CATCH_START_SECTION("Default option with a short name")
770 : advgetopt::option const options[] =
771 : {
772 : advgetopt::define_option(
773 : advgetopt::Name("verbose")
774 : , advgetopt::ShortName('v')
775 : , advgetopt::Flags(advgetopt::standalone_command_flags())
776 : , advgetopt::Help("print info as we work.")
777 : ),
778 : advgetopt::define_option(
779 : advgetopt::Name("--")
780 : , advgetopt::ShortName('f')
781 : , advgetopt::Flags(advgetopt::option_flags<advgetopt::GETOPT_FLAG_COMMAND_LINE>())
782 : , advgetopt::Help("list of filenames.")
783 : ),
784 : advgetopt::end_options()
785 1 : };
786 :
787 1 : advgetopt::options_environment environment_options;
788 1 : environment_options.f_project_name = "unittest";
789 1 : environment_options.f_options = options;
790 1 : environment_options.f_environment_flags = 0;
791 1 : environment_options.f_help_header = "Usage: short name not acceptable with \"--\"";
792 :
793 : char const * cargv[] =
794 : {
795 : "tests/option-with-name-too-short",
796 : "--verbose",
797 : "file.txt",
798 : nullptr
799 1 : };
800 1 : int const argc(sizeof(cargv) / sizeof(cargv[0]) - 1);
801 1 : char ** argv = const_cast<char **>(cargv);
802 :
803 1 : CATCH_REQUIRE_THROWS_MATCHES(std::make_shared<advgetopt::getopt>(environment_options, argc, argv)
804 : , advgetopt::getopt_exception_logic
805 : , Catch::Matchers::ExceptionMessage(
806 : "option_info::option_info(): the default parameter \"--\" cannot include a short name ('f'.)"));
807 : CATCH_END_SECTION()
808 :
809 20 : CATCH_START_SECTION("Duplicated Options (Long Name)")
810 : advgetopt::option const options[] =
811 : {
812 : advgetopt::define_option(
813 : advgetopt::Name("verbose")
814 : , advgetopt::ShortName('v')
815 : , advgetopt::Flags(advgetopt::standalone_command_flags())
816 : , advgetopt::Help("print info as we work.")
817 : ),
818 : advgetopt::define_option(
819 : advgetopt::Name("licence")
820 : , advgetopt::Flags(advgetopt::standalone_command_flags())
821 : ),
822 : advgetopt::define_option(
823 : advgetopt::Name("licence") // duplicate
824 : , advgetopt::Flags(advgetopt::standalone_command_flags())
825 : ),
826 : advgetopt::end_options()
827 1 : };
828 :
829 1 : advgetopt::options_environment environment_options;
830 1 : environment_options.f_project_name = "unittest";
831 1 : environment_options.f_options = options;
832 1 : environment_options.f_environment_flags = 0;
833 1 : environment_options.f_help_header = "Usage: one name can't be redefined";
834 :
835 : char const * cargv[] =
836 : {
837 : "tests/duplicated-option",
838 : "--missing-name",
839 : nullptr
840 1 : };
841 1 : int const argc(sizeof(cargv) / sizeof(cargv[0]) - 1);
842 1 : char ** argv = const_cast<char **>(cargv);
843 :
844 1 : CATCH_REQUIRE_THROWS_MATCHES(std::make_shared<advgetopt::getopt>(environment_options, argc, argv)
845 : , advgetopt::getopt_exception_logic
846 : , Catch::Matchers::ExceptionMessage(
847 : "option named \"licence\" found twice."));
848 : CATCH_END_SECTION()
849 :
850 20 : CATCH_START_SECTION("Duplicated Options (short name)")
851 : advgetopt::option const options[] =
852 : {
853 : advgetopt::define_option(
854 : advgetopt::Name("verbose")
855 : , advgetopt::ShortName('v')
856 : , advgetopt::Flags(advgetopt::standalone_command_flags())
857 : , advgetopt::Help("print info as we work.")
858 : ),
859 : advgetopt::define_option(
860 : advgetopt::Name("look")
861 : , advgetopt::ShortName('l')
862 : , advgetopt::Flags(advgetopt::standalone_command_flags())
863 : ),
864 : advgetopt::define_option(
865 : advgetopt::Name("lock")
866 : , advgetopt::ShortName('l') // duplicate
867 : , advgetopt::Flags(advgetopt::standalone_command_flags())
868 : ),
869 : advgetopt::end_options()
870 1 : };
871 :
872 1 : advgetopt::options_environment environment_options;
873 1 : environment_options.f_project_name = "unittest";
874 1 : environment_options.f_options = options;
875 1 : environment_options.f_environment_flags = 0;
876 1 : environment_options.f_help_header = "Usage: one name can't be redefined";
877 :
878 : char const * cargv[] =
879 : {
880 : "tests/duplicated-option",
881 : "--missing-name",
882 : nullptr
883 1 : };
884 1 : int const argc(sizeof(cargv) / sizeof(cargv[0]) - 1);
885 1 : char ** argv = const_cast<char **>(cargv);
886 :
887 1 : CATCH_REQUIRE_THROWS_MATCHES(std::make_shared<advgetopt::getopt>(environment_options, argc, argv)
888 : , advgetopt::getopt_exception_logic
889 : , Catch::Matchers::ExceptionMessage(
890 : "option with short name \"l\" found twice."));
891 : CATCH_END_SECTION()
892 :
893 20 : CATCH_START_SECTION("Duplicated Default Options")
894 : advgetopt::option const options[] =
895 : {
896 : advgetopt::define_option(
897 : advgetopt::Name("verbose")
898 : , advgetopt::ShortName('v')
899 : , advgetopt::Flags(advgetopt::standalone_command_flags())
900 : , advgetopt::Help("print info as we work.")
901 : ),
902 : advgetopt::define_option(
903 : advgetopt::Name("ins")
904 : , advgetopt::Flags(advgetopt::command_flags<advgetopt::GETOPT_FLAG_DEFAULT_OPTION>())
905 : ),
906 : advgetopt::define_option(
907 : advgetopt::Name("outs")
908 : , advgetopt::Flags(advgetopt::command_flags<advgetopt::GETOPT_FLAG_DEFAULT_OPTION>()) // default option again
909 : ),
910 : advgetopt::end_options()
911 1 : };
912 :
913 1 : advgetopt::options_environment environment_options;
914 1 : environment_options.f_project_name = "unittest";
915 1 : environment_options.f_options = options;
916 1 : environment_options.f_environment_flags = 0;
917 1 : environment_options.f_help_header = "Usage: one name can't be redefined";
918 :
919 : char const * cargv[] =
920 : {
921 : "tests/duplicated-option",
922 : "--missing-name",
923 : nullptr
924 1 : };
925 1 : int const argc(sizeof(cargv) / sizeof(cargv[0]) - 1);
926 1 : char ** argv = const_cast<char **>(cargv);
927 :
928 1 : CATCH_REQUIRE_THROWS_MATCHES(std::make_shared<advgetopt::getopt>(environment_options, argc, argv)
929 : , advgetopt::getopt_exception_logic
930 : , Catch::Matchers::ExceptionMessage(
931 : "two default options found after check of long names duplication."));
932 : CATCH_END_SECTION()
933 :
934 20 : CATCH_START_SECTION("Default Option marked as being a FLAG")
935 : advgetopt::option const options[] =
936 : {
937 : advgetopt::define_option(
938 : advgetopt::Name("verbose")
939 : , advgetopt::ShortName('v')
940 : , advgetopt::Flags(advgetopt::standalone_command_flags())
941 : , advgetopt::Help("print info as we work.")
942 : ),
943 : // the define_option() already catches this error at compile time
944 : {
945 : 'o'
946 : , advgetopt::GETOPT_FLAG_COMMAND_LINE | advgetopt::GETOPT_FLAG_DEFAULT_OPTION | advgetopt::GETOPT_FLAG_FLAG
947 : , "output"
948 : , nullptr
949 : , nullptr
950 : , nullptr
951 : },
952 : advgetopt::end_options()
953 1 : };
954 :
955 1 : advgetopt::options_environment environment_options;
956 1 : environment_options.f_project_name = "unittest";
957 1 : environment_options.f_options = options;
958 1 : environment_options.f_environment_flags = 0;
959 1 : environment_options.f_help_header = "Usage: one name can't be redefined";
960 :
961 : char const * cargv[] =
962 : {
963 : "tests/duplicated-option",
964 : "--missing-name",
965 : nullptr
966 1 : };
967 1 : int const argc(sizeof(cargv) / sizeof(cargv[0]) - 1);
968 1 : char ** argv = const_cast<char **>(cargv);
969 :
970 1 : CATCH_REQUIRE_THROWS_MATCHES(std::make_shared<advgetopt::getopt>(environment_options, argc, argv)
971 : , advgetopt::getopt_exception_logic
972 : , Catch::Matchers::ExceptionMessage(
973 : "a default option must accept parameters, it can't be a GETOPT_FLAG_FLAG."));
974 : CATCH_END_SECTION()
975 :
976 20 : CATCH_START_SECTION("Option with an alias and mismatched flags")
977 : advgetopt::option const options[] =
978 : {
979 : advgetopt::define_option(
980 : advgetopt::Name("verbose")
981 : , advgetopt::ShortName('v')
982 : , advgetopt::Flags(advgetopt::standalone_command_flags())
983 : , advgetopt::Help("print info as we work.")
984 : ),
985 : advgetopt::define_option(
986 : advgetopt::Name("licence") // to allow French spelling
987 : , advgetopt::Alias("license")
988 : , advgetopt::Flags(advgetopt::command_flags<advgetopt::GETOPT_FLAG_GROUP_COMMANDS
989 : , advgetopt::GETOPT_FLAG_REQUIRED>()) // not a match
990 : ),
991 : advgetopt::end_options()
992 1 : };
993 :
994 1 : advgetopt::options_environment environment_options;
995 1 : environment_options.f_project_name = "unittest";
996 1 : environment_options.f_options = options;
997 1 : environment_options.f_environment_flags = advgetopt::GETOPT_ENVIRONMENT_FLAG_SYSTEM_PARAMETERS;
998 1 : environment_options.f_help_header = "Usage: flags are not equal";
999 :
1000 : char const * cargv[] =
1001 : {
1002 : "tests/option-without-a-name",
1003 : "--incompatible-flags",
1004 : nullptr
1005 1 : };
1006 1 : int const argc(sizeof(cargv) / sizeof(cargv[0]) - 1);
1007 1 : char ** argv = const_cast<char **>(cargv);
1008 :
1009 1 : CATCH_REQUIRE_THROWS_MATCHES(std::make_shared<advgetopt::getopt>(environment_options, argc, argv)
1010 : , advgetopt::getopt_exception_logic
1011 : , Catch::Matchers::ExceptionMessage(
1012 : "the flags of alias \"licence\" (0x100041) are different"
1013 : " than the flags of \"license\" (0x100021)."));
1014 : CATCH_END_SECTION()
1015 10 : }
1016 :
1017 :
1018 :
1019 :
1020 4 : CATCH_TEST_CASE("invalid_config_dir_short_name", "[arguments][invalid][getopt][config]")
1021 : {
1022 4 : CATCH_START_SECTION("Trying to set '-o' as '--config-dir' short name")
1023 : {
1024 : advgetopt::option const options[] =
1025 : {
1026 : advgetopt::define_option(
1027 : advgetopt::Name("out")
1028 : , advgetopt::ShortName('o')
1029 : , advgetopt::Flags(advgetopt::command_flags<advgetopt::GETOPT_FLAG_REQUIRED>())
1030 : , advgetopt::Help("output filename.")
1031 : ),
1032 : advgetopt::end_options()
1033 1 : };
1034 :
1035 1 : advgetopt::options_environment environment_options;
1036 1 : environment_options.f_project_name = "unittest";
1037 1 : environment_options.f_options = options;
1038 1 : environment_options.f_environment_flags = advgetopt::GETOPT_ENVIRONMENT_FLAG_SYSTEM_PARAMETERS;
1039 1 : environment_options.f_configuration_filename = "snapwatchdog.conf";
1040 1 : environment_options.f_help_header = "Usage: test --config-dir";
1041 :
1042 2 : advgetopt::getopt opt(environment_options);
1043 :
1044 1 : CATCH_REQUIRE(opt.get_option("config-dir") != nullptr);
1045 1 : CATCH_REQUIRE_THROWS_MATCHES(
1046 : opt.set_short_name("config-dir", U'o')
1047 : , advgetopt::getopt_exception_logic
1048 : , Catch::Matchers::ExceptionMessage(
1049 : "found another option (\"out\") with short name 'o'."));
1050 : }
1051 : CATCH_END_SECTION()
1052 :
1053 4 : CATCH_START_SECTION("Trying to set '-c' as '--config-dir' short name, buf configuration filename is nullptr")
1054 : {
1055 : advgetopt::option const options[] =
1056 : {
1057 : advgetopt::define_option(
1058 : advgetopt::Name("out")
1059 : , advgetopt::ShortName('o')
1060 : , advgetopt::Flags(advgetopt::command_flags<advgetopt::GETOPT_FLAG_REQUIRED>())
1061 : , advgetopt::Help("output filename.")
1062 : ),
1063 : advgetopt::end_options()
1064 1 : };
1065 :
1066 1 : advgetopt::options_environment environment_options;
1067 1 : environment_options.f_project_name = "unittest";
1068 1 : environment_options.f_options = options;
1069 1 : environment_options.f_environment_flags = advgetopt::GETOPT_ENVIRONMENT_FLAG_SYSTEM_PARAMETERS;
1070 1 : environment_options.f_configuration_filename = nullptr;
1071 1 : environment_options.f_help_header = "Usage: test --config-dir";
1072 :
1073 2 : advgetopt::getopt opt(environment_options);
1074 :
1075 : advgetopt::option_info::pointer_t config_dir();
1076 1 : CATCH_REQUIRE(opt.get_option("config-dir") == nullptr);
1077 1 : CATCH_REQUIRE_THROWS_MATCHES(
1078 : opt.set_short_name("config-dir", U'c')
1079 : , advgetopt::getopt_exception_logic
1080 : , Catch::Matchers::ExceptionMessage(
1081 : "option with name \"config-dir\" not found."));
1082 : }
1083 : CATCH_END_SECTION()
1084 :
1085 : // CATCH_START_SECTION("Trying to set NO_SHORT_NAME as '--config-dir' short name")
1086 : // {
1087 : // advgetopt::option const options[] =
1088 : // {
1089 : // advgetopt::define_option(
1090 : // advgetopt::Name("out")
1091 : // , advgetopt::ShortName('o')
1092 : // , advgetopt::Flags(advgetopt::command_flags<advgetopt::GETOPT_FLAG_REQUIRED>())
1093 : // , advgetopt::Help("output filename.")
1094 : // ),
1095 : // advgetopt::end_options()
1096 : // };
1097 : //
1098 : // advgetopt::options_environment environment_options;
1099 : // environment_options.f_project_name = "unittest";
1100 : // environment_options.f_options = options;
1101 : // environment_options.f_environment_flags = advgetopt::GETOPT_ENVIRONMENT_FLAG_SYSTEM_PARAMETERS;
1102 : // environment_options.f_configuration_filename = "snapwatchdog.conf";
1103 : // environment_options.f_help_header = "Usage: test --config-dir";
1104 : //
1105 : // advgetopt::getopt opt(environment_options);
1106 : //
1107 : // CATCH_REQUIRE(opt.get_option("config-dir") != nullptr);
1108 : // CATCH_REQUIRE_THROWS_MATCHES(
1109 : // opt.set_short_name("config-dir", advgetopt::NO_SHORT_NAME)
1110 : // , advgetopt::getopt_exception_logic
1111 : // , Catch::Matchers::ExceptionMessage(
1112 : // "The short name of option \"config-dir\" cannot be set to NO_SHORT_NAME."));
1113 : // }
1114 : // CATCH_END_SECTION()
1115 : //
1116 : // CATCH_START_SECTION("Trying to change short name of '--version'")
1117 : // {
1118 : // advgetopt::option const options[] =
1119 : // {
1120 : // advgetopt::define_option(
1121 : // advgetopt::Name("out")
1122 : // , advgetopt::ShortName('o')
1123 : // , advgetopt::Flags(advgetopt::command_flags<advgetopt::GETOPT_FLAG_REQUIRED>())
1124 : // , advgetopt::Help("output filename.")
1125 : // ),
1126 : // advgetopt::end_options()
1127 : // };
1128 : //
1129 : // advgetopt::options_environment environment_options;
1130 : // environment_options.f_project_name = "unittest";
1131 : // environment_options.f_options = options;
1132 : // environment_options.f_environment_flags = advgetopt::GETOPT_ENVIRONMENT_FLAG_SYSTEM_PARAMETERS;
1133 : // environment_options.f_configuration_filename = "";
1134 : // environment_options.f_help_header = "Usage: test --config-dir";
1135 : //
1136 : // advgetopt::getopt opt(environment_options);
1137 : //
1138 : // CATCH_REQUIRE(opt.get_option("version") != nullptr);
1139 : // CATCH_REQUIRE_THROWS_MATCHES(
1140 : // opt.set_short_name("version", U'v') // set to lowercase...
1141 : // , advgetopt::getopt_exception_logic
1142 : // , Catch::Matchers::ExceptionMessage(
1143 : // "The short name of option \"version\" cannot be changed from 'V' to 'v'."));
1144 : // }
1145 : // CATCH_END_SECTION()
1146 8 : }
1147 :
1148 :
1149 :
1150 :
1151 :
1152 :
1153 :
1154 :
1155 :
1156 : // vim: ts=4 sw=4 et
|