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