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