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