Line data Source code
1 : // Copyright (c) 2006-2021 Made to Order Software Corp. All Rights Reserved
2 : //
3 : // https://snapwebsites.org/project/advgetopt
4 : // contact@m2osw.com
5 : //
6 : // This program is free software; you can redistribute it and/or modify
7 : // it under the terms of the GNU General Public License as published by
8 : // the Free Software Foundation; either version 2 of the License, or
9 : // (at your option) any later version.
10 : //
11 : // This program is distributed in the hope that it will be useful,
12 : // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 : // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 : // GNU General Public License for more details.
15 : //
16 : // You should have received a copy of the GNU General Public License along
17 : // with this program; if not, write to the Free Software Foundation, Inc.,
18 : // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 :
20 : // self
21 : //
22 : #include "main.h"
23 :
24 :
25 : // advgetopt lib
26 : //
27 : #include <advgetopt/exception.h>
28 :
29 :
30 : // snapdev lib
31 : //
32 : #include <snapdev/safe_setenv.h>
33 :
34 :
35 : // booost lib
36 : //
37 : #include <boost/preprocessor/stringize.hpp>
38 :
39 :
40 : // C++ lib
41 : //
42 : #include <fstream>
43 :
44 :
45 : // last include
46 : //
47 : #include <snapdev/poison.h>
48 :
49 :
50 :
51 :
52 :
53 4 : CATCH_TEST_CASE("string_access", "[arguments][valid][getopt]")
54 : {
55 4 : CATCH_START_SECTION("Verify a string in a long argument")
56 1 : advgetopt::option const options[] =
57 : {
58 : advgetopt::define_option(
59 : advgetopt::Name("user-name")
60 : , advgetopt::ShortName('u')
61 : , advgetopt::Flags(advgetopt::command_flags<advgetopt::GETOPT_FLAG_REQUIRED>())
62 : , advgetopt::Help("check specified user.")
63 : ),
64 : advgetopt::end_options()
65 : };
66 :
67 1 : advgetopt::options_environment environment_options;
68 1 : environment_options.f_project_name = "unittest";
69 1 : environment_options.f_options = options;
70 1 : environment_options.f_help_header = "Usage: user name as a string";
71 :
72 1 : char const * cargv[] =
73 : {
74 : "/usr/bin/arguments",
75 : "--user-name",
76 : "alexis",
77 : nullptr
78 : };
79 1 : int const argc(sizeof(cargv) / sizeof(cargv[0]) - 1);
80 1 : char ** argv = const_cast<char **>(cargv);
81 :
82 2 : advgetopt::getopt opt(environment_options, argc, argv);
83 :
84 : // check that the result is valid
85 :
86 : // an invalid parameter, MUST NOT EXIST
87 1 : CATCH_REQUIRE(opt.get_option("invalid-parameter") == nullptr);
88 1 : CATCH_REQUIRE(opt.get_option('Z') == nullptr);
89 1 : CATCH_REQUIRE_FALSE(opt.is_defined("invalid-parameter"));
90 1 : CATCH_REQUIRE_FALSE(opt.has_default("invalid-parameter"));
91 1 : CATCH_REQUIRE(opt.get_default("invalid-parameter").empty());
92 1 : CATCH_REQUIRE(opt.size("invalid-parameter") == 0);
93 :
94 : // no default
95 1 : CATCH_REQUIRE(opt.get_option("--") == nullptr);
96 1 : CATCH_REQUIRE_FALSE(opt.is_defined("--"));
97 1 : CATCH_REQUIRE_FALSE(opt.has_default("--"));
98 1 : CATCH_REQUIRE(opt.get_default("--").empty());
99 1 : CATCH_REQUIRE(opt.size("--") == 0);
100 :
101 : // the valid parameter
102 1 : CATCH_REQUIRE(opt.get_option("user-name") != nullptr);
103 1 : CATCH_REQUIRE(opt.get_option('u') != nullptr);
104 1 : CATCH_REQUIRE(opt.get_string("user-name") == "alexis");
105 1 : CATCH_REQUIRE(opt.get_string("user-name", 0) == "alexis");
106 1 : CATCH_REQUIRE(opt["user-name"] == "alexis");
107 1 : CATCH_REQUIRE(opt.is_defined("user-name"));
108 1 : CATCH_REQUIRE_FALSE(opt.has_default("user-name"));
109 1 : CATCH_REQUIRE(opt.get_default("user-name").empty());
110 1 : CATCH_REQUIRE(opt.size("user-name") == 1);
111 :
112 : // other parameters
113 1 : CATCH_REQUIRE(opt.get_program_name() == "arguments");
114 1 : CATCH_REQUIRE(opt.get_program_fullname() == "/usr/bin/arguments");
115 : CATCH_END_SECTION()
116 :
117 4 : CATCH_START_SECTION("Verify a string in a short argument")
118 1 : advgetopt::option const options[] =
119 : {
120 : advgetopt::define_option(
121 : advgetopt::Name("user-name")
122 : , advgetopt::ShortName('u')
123 : , advgetopt::Flags(advgetopt::command_flags<advgetopt::GETOPT_FLAG_REQUIRED>())
124 : , advgetopt::Help("check specified user.")
125 : ),
126 : advgetopt::end_options()
127 : };
128 :
129 1 : advgetopt::options_environment environment_options;
130 1 : environment_options.f_project_name = "unittest";
131 1 : environment_options.f_options = options;
132 1 : environment_options.f_help_header = "Usage: user name as a string";
133 :
134 1 : char const * cargv[] =
135 : {
136 : "/usr/bin/arguments",
137 : "-u",
138 : "alexis",
139 : nullptr
140 : };
141 1 : int const argc(sizeof(cargv) / sizeof(cargv[0]) - 1);
142 1 : char ** argv = const_cast<char **>(cargv);
143 :
144 2 : advgetopt::getopt opt(environment_options, argc, argv);
145 :
146 : // check that the result is valid
147 :
148 : // an invalid parameter, MUST NOT EXIST
149 1 : CATCH_REQUIRE(opt.get_option("invalid-parameter") == nullptr);
150 1 : CATCH_REQUIRE(opt.get_option('Z') == nullptr);
151 1 : CATCH_REQUIRE_FALSE(opt.is_defined("invalid-parameter"));
152 1 : CATCH_REQUIRE(opt.get_default("invalid-parameter").empty());
153 1 : CATCH_REQUIRE(opt.size("invalid-parameter") == 0);
154 :
155 : // no default
156 1 : CATCH_REQUIRE(opt.get_option("--") == nullptr);
157 1 : CATCH_REQUIRE_FALSE(opt.is_defined("--"));
158 1 : CATCH_REQUIRE(opt.get_default("--").empty());
159 1 : CATCH_REQUIRE(opt.size("--") == 0);
160 :
161 : // the valid parameter
162 1 : CATCH_REQUIRE(opt.get_option("user-name") != nullptr);
163 1 : CATCH_REQUIRE(opt.get_option('u') != nullptr);
164 1 : CATCH_REQUIRE(opt.get_string("user-name") == "alexis");
165 1 : CATCH_REQUIRE(opt.get_string("user-name", 0) == "alexis");
166 1 : CATCH_REQUIRE(opt["user-name"] == "alexis");
167 1 : CATCH_REQUIRE(opt.is_defined("user-name"));
168 1 : CATCH_REQUIRE(opt.get_default("user-name").empty());
169 1 : CATCH_REQUIRE(opt.size("user-name") == 1);
170 :
171 : // other parameters
172 1 : CATCH_REQUIRE(opt.get_program_name() == "arguments");
173 1 : CATCH_REQUIRE(opt.get_program_fullname() == "/usr/bin/arguments");
174 : CATCH_END_SECTION()
175 2 : }
176 :
177 :
178 5 : CATCH_TEST_CASE("long_access", "[arguments][valid][getopt]")
179 : {
180 6 : CATCH_START_SECTION("Verify an integer (long) value in a long argument")
181 1 : long const default_value(rand());
182 2 : std::string const default_value_str(std::to_string(default_value));
183 1 : char const * const default_val(default_value_str.c_str());
184 :
185 1 : advgetopt::option const options[] =
186 : {
187 : advgetopt::define_option(
188 : advgetopt::Name("size")
189 : , advgetopt::ShortName('s')
190 : , advgetopt::Flags(advgetopt::command_flags<advgetopt::GETOPT_FLAG_REQUIRED>())
191 : , advgetopt::Help("define the size.")
192 : , advgetopt::DefaultValue(default_val)
193 6 : ),
194 : advgetopt::end_options()
195 7 : };
196 :
197 1 : advgetopt::options_environment environment_options;
198 1 : environment_options.f_project_name = "unittest";
199 1 : environment_options.f_options = options;
200 1 : environment_options.f_help_header = "Usage: test get_long() functions";
201 :
202 1 : char const * cargv[] =
203 : {
204 : "/usr/bin/arguments",
205 : "--size",
206 : "9821",
207 : nullptr
208 : };
209 1 : int const argc(sizeof(cargv) / sizeof(cargv[0]) - 1);
210 1 : char ** argv = const_cast<char **>(cargv);
211 :
212 2 : advgetopt::getopt opt(environment_options, argc, argv);
213 :
214 : // check that the result is valid
215 :
216 : // an invalid parameter, MUST NOT EXIST
217 1 : CATCH_REQUIRE(opt.get_option("invalid-parameter") == nullptr);
218 1 : CATCH_REQUIRE(opt.get_option('Z') == nullptr);
219 1 : CATCH_REQUIRE_FALSE(opt.is_defined("invalid-parameter"));
220 1 : CATCH_REQUIRE(opt.get_default("invalid-parameter").empty());
221 1 : CATCH_REQUIRE(opt.size("invalid-parameter") == 0);
222 :
223 : // no default
224 1 : CATCH_REQUIRE(opt.get_option("--") == nullptr);
225 1 : CATCH_REQUIRE_FALSE(opt.is_defined("--"));
226 1 : CATCH_REQUIRE(opt.get_default("--").empty());
227 1 : CATCH_REQUIRE(opt.size("--") == 0);
228 :
229 : // the valid parameter
230 1 : CATCH_REQUIRE(opt.get_option("size") != nullptr);
231 1 : CATCH_REQUIRE(opt.get_option('s') != nullptr);
232 1 : CATCH_REQUIRE(opt.is_defined("size"));
233 1 : CATCH_REQUIRE(opt.get_string("size") == "9821");
234 1 : CATCH_REQUIRE(opt.get_string("size", 0) == "9821");
235 1 : CATCH_REQUIRE(opt["size"] == "9821");
236 1 : CATCH_REQUIRE(opt.get_long("size") == 9821);
237 1 : CATCH_REQUIRE(opt.get_long("size", 0) == 9821);
238 1 : CATCH_REQUIRE(opt.has_default("size"));
239 1 : CATCH_REQUIRE(opt.get_default("size") == default_value_str);
240 1 : CATCH_REQUIRE(opt.size("size") == 1);
241 :
242 : // other parameters
243 1 : CATCH_REQUIRE(opt.get_program_name() == "arguments");
244 1 : CATCH_REQUIRE(opt.get_program_fullname() == "/usr/bin/arguments");
245 : CATCH_END_SECTION()
246 :
247 6 : CATCH_START_SECTION("Verify an integer (long) value in a short argument")
248 1 : advgetopt::option const options[] =
249 : {
250 : advgetopt::define_option(
251 : advgetopt::Name("size")
252 : , advgetopt::ShortName('s')
253 : , advgetopt::Flags(advgetopt::command_flags<advgetopt::GETOPT_FLAG_REQUIRED>())
254 : , advgetopt::Help("define the size.")
255 : , advgetopt::DefaultValue("")
256 : ),
257 : advgetopt::end_options()
258 : };
259 :
260 1 : advgetopt::options_environment environment_options;
261 1 : environment_options.f_project_name = "unittest";
262 1 : environment_options.f_options = options;
263 1 : environment_options.f_help_header = "Usage: test get_long() functions";
264 :
265 1 : char const * cargv[] =
266 : {
267 : "/usr/bin/arguments",
268 : "-s",
269 : "9821",
270 : nullptr
271 : };
272 1 : int const argc(sizeof(cargv) / sizeof(cargv[0]) - 1);
273 1 : char ** argv = const_cast<char **>(cargv);
274 :
275 2 : advgetopt::getopt opt(environment_options, argc, argv);
276 :
277 : // check that the result is valid
278 :
279 : // an invalid parameter, MUST NOT EXIST
280 1 : CATCH_REQUIRE(opt.get_option("invalid-parameter") == nullptr);
281 1 : CATCH_REQUIRE(opt.get_option('Z') == nullptr);
282 1 : CATCH_REQUIRE_FALSE(opt.is_defined("invalid-parameter"));
283 1 : CATCH_REQUIRE(opt.get_default("invalid-parameter").empty());
284 1 : CATCH_REQUIRE(opt.size("invalid-parameter") == 0);
285 :
286 : // to reach the constant version `opt` has to be constant
287 2 : std::string const array_syntax_invalid_parameter(static_cast<advgetopt::getopt const &>(opt)["invalid-parameter"]);
288 1 : CATCH_REQUIRE(array_syntax_invalid_parameter == std::string());
289 :
290 : // no default
291 1 : CATCH_REQUIRE(opt.get_option("--") == nullptr);
292 1 : CATCH_REQUIRE_FALSE(opt.is_defined("--"));
293 1 : CATCH_REQUIRE(opt.get_default("--").empty());
294 1 : CATCH_REQUIRE(opt.size("--") == 0);
295 :
296 : // the valid parameter
297 1 : CATCH_REQUIRE(opt.get_option("size") != nullptr);
298 1 : CATCH_REQUIRE(opt.get_option('s') != nullptr);
299 1 : CATCH_REQUIRE(opt.is_defined("size"));
300 1 : CATCH_REQUIRE(opt.get_string("size") == "9821");
301 1 : CATCH_REQUIRE(opt.get_string("size", 0) == "9821");
302 1 : CATCH_REQUIRE(opt["size"] == "9821");
303 1 : CATCH_REQUIRE(opt.get_long("size") == 9821);
304 1 : CATCH_REQUIRE(opt.get_long("size", 0) == 9821);
305 1 : CATCH_REQUIRE(opt.has_default("size"));
306 1 : CATCH_REQUIRE(opt.get_default("size").empty());
307 1 : CATCH_REQUIRE(opt.size("size") == 1);
308 :
309 : // to access the constant reference we need a constant `opt`...
310 2 : std::string const array_syntax1(static_cast<advgetopt::getopt const &>(opt)["size"]);
311 1 : CATCH_REQUIRE(array_syntax1 == "9821");
312 1 : bool const array_syntax2(static_cast<advgetopt::getopt const &>(opt)["size"] == std::string("9821"));
313 1 : CATCH_REQUIRE(array_syntax2);
314 :
315 : // other parameters
316 1 : CATCH_REQUIRE(opt.get_program_name() == "arguments");
317 1 : CATCH_REQUIRE(opt.get_program_fullname() == "/usr/bin/arguments");
318 : CATCH_END_SECTION()
319 :
320 6 : CATCH_START_SECTION("Verify an integer (long) value in no arguments")
321 1 : advgetopt::option const options[] =
322 : {
323 : advgetopt::define_option(
324 : advgetopt::Name("size")
325 : , advgetopt::ShortName('s')
326 : , advgetopt::Flags(advgetopt::command_flags<advgetopt::GETOPT_FLAG_REQUIRED>())
327 : , advgetopt::Help("define the size.")
328 : , advgetopt::DefaultValue("839")
329 : ),
330 : advgetopt::end_options()
331 : };
332 :
333 1 : advgetopt::options_environment environment_options;
334 1 : environment_options.f_project_name = "unittest";
335 1 : environment_options.f_options = options;
336 1 : environment_options.f_help_header = "Usage: test get_long() functions";
337 :
338 1 : char const * cargv[] =
339 : {
340 : "/usr/bin/arguments",
341 : nullptr
342 : };
343 1 : int const argc(sizeof(cargv) / sizeof(cargv[0]) - 1);
344 1 : char ** argv = const_cast<char **>(cargv);
345 :
346 2 : advgetopt::getopt opt(environment_options, argc, argv);
347 :
348 : // check that the result is valid
349 :
350 : // an invalid parameter, MUST NOT EXIST
351 1 : CATCH_REQUIRE(opt.get_option("invalid-parameter") == nullptr);
352 1 : CATCH_REQUIRE(opt.get_option('Z') == nullptr);
353 1 : CATCH_REQUIRE(opt["invalid-parameter"] == std::string());
354 1 : CATCH_REQUIRE_FALSE(opt.is_defined("invalid-parameter"));
355 1 : CATCH_REQUIRE(opt.get_default("invalid-parameter").empty());
356 1 : CATCH_REQUIRE(opt.size("invalid-parameter") == 0);
357 :
358 : // no default
359 1 : CATCH_REQUIRE(opt.get_option("--") == nullptr);
360 1 : CATCH_REQUIRE_FALSE(opt.is_defined("--"));
361 1 : CATCH_REQUIRE(opt.get_default("--").empty());
362 1 : CATCH_REQUIRE(opt.size("--") == 0);
363 :
364 : // the valid parameter
365 1 : CATCH_REQUIRE(opt.get_option("size") != nullptr);
366 1 : CATCH_REQUIRE(opt.get_option('s') != nullptr);
367 1 : CATCH_REQUIRE_FALSE(opt.is_defined("size"));
368 1 : CATCH_REQUIRE(opt.get_string("size") == "839");
369 1 : CATCH_REQUIRE(opt.get_string("size", 0) == "839");
370 1 : CATCH_REQUIRE(opt.get_long("size") == 839);
371 1 : CATCH_REQUIRE(opt.get_long("size", 0) == 839);
372 1 : CATCH_REQUIRE(opt.has_default("size"));
373 1 : CATCH_REQUIRE(opt.get_default("size") == "839");
374 1 : CATCH_REQUIRE(opt.size("size") == 0);
375 :
376 : // with a constant opt, the array syntax returns the default string
377 1 : CATCH_REQUIRE(static_cast<advgetopt::getopt const &>(opt)["size"] == "839");
378 :
379 : // other parameters
380 1 : CATCH_REQUIRE(opt.get_program_name() == "arguments");
381 1 : CATCH_REQUIRE(opt.get_program_fullname() == "/usr/bin/arguments");
382 : CATCH_END_SECTION()
383 3 : }
384 :
385 :
386 :
387 4 : CATCH_TEST_CASE("system_flags_version", "[arguments][valid][getopt][system_flags]")
388 : {
389 4 : CATCH_START_SECTION("Check with the --version system flag")
390 1 : long const major_version(rand());
391 1 : long const minor_version(rand());
392 1 : long const patch_version(rand());
393 1 : long const build_version(rand());
394 3 : std::string const version(std::to_string(major_version)
395 3 : + "."
396 4 : + std::to_string(minor_version)
397 3 : + "."
398 4 : + std::to_string(patch_version)
399 3 : + "."
400 4 : + std::to_string(build_version));
401 :
402 1 : long const default_value(rand());
403 2 : std::string const default_val(std::to_string(default_value));
404 1 : advgetopt::option const options[] =
405 : {
406 : advgetopt::define_option(
407 : advgetopt::Name("size")
408 : , advgetopt::ShortName('s')
409 : , advgetopt::Flags(advgetopt::command_flags<advgetopt::GETOPT_FLAG_REQUIRED>())
410 : , advgetopt::Help("define the size.")
411 : , advgetopt::DefaultValue(default_val.c_str())
412 6 : ),
413 : advgetopt::end_options()
414 7 : };
415 :
416 1 : advgetopt::options_environment environment_options;
417 1 : environment_options.f_project_name = "unittest";
418 1 : environment_options.f_options = options;
419 1 : environment_options.f_environment_flags = advgetopt::GETOPT_ENVIRONMENT_FLAG_SYSTEM_PARAMETERS;
420 1 : environment_options.f_help_header = "Usage: test system commands";
421 1 : environment_options.f_version = version.c_str();
422 :
423 1 : char const * cargv[] =
424 : {
425 : "/usr/bin/arguments",
426 : "--version",
427 : nullptr
428 : };
429 1 : int const argc(sizeof(cargv) / sizeof(cargv[0]) - 1);
430 1 : char ** argv = const_cast<char **>(cargv);
431 :
432 2 : advgetopt::getopt opt(environment_options, argc, argv);
433 :
434 : // check that the result is valid
435 :
436 : // an invalid parameter, MUST NOT EXIST
437 1 : CATCH_REQUIRE(opt.get_option("invalid-parameter") == nullptr);
438 1 : CATCH_REQUIRE(opt.get_option('Z') == nullptr);
439 1 : CATCH_REQUIRE_FALSE(opt.is_defined("invalid-parameter"));
440 1 : CATCH_REQUIRE(opt.get_default("invalid-parameter").empty());
441 1 : CATCH_REQUIRE(opt.size("invalid-parameter") == 0);
442 :
443 : // no default
444 1 : CATCH_REQUIRE(opt.get_option("--") == nullptr);
445 1 : CATCH_REQUIRE_FALSE(opt.is_defined("--"));
446 1 : CATCH_REQUIRE(opt.get_default("--").empty());
447 1 : CATCH_REQUIRE(opt.size("--") == 0);
448 :
449 : // valid parameter
450 1 : CATCH_REQUIRE(opt.get_option("size") != nullptr);
451 1 : CATCH_REQUIRE(opt.get_option('s') == opt.get_option("size"));
452 1 : CATCH_REQUIRE_FALSE(opt.is_defined("size"));
453 1 : CATCH_REQUIRE(opt.get_string("size") == default_val);
454 1 : CATCH_REQUIRE(opt.get_string("size", 0) == default_val);
455 1 : CATCH_REQUIRE(opt["size"] == default_val);
456 1 : CATCH_REQUIRE(opt.get_long("size") == default_value);
457 1 : CATCH_REQUIRE(opt.get_long("size", 0) == default_value);
458 1 : CATCH_REQUIRE(opt.has_default("size"));
459 1 : CATCH_REQUIRE(opt.get_default("size") == default_val);
460 1 : CATCH_REQUIRE(opt.size("size") == 0);
461 :
462 : // version parameter
463 1 : CATCH_REQUIRE(opt.get_option("version") != nullptr);
464 1 : CATCH_REQUIRE(opt.get_option('V') == opt.get_option("version"));
465 1 : CATCH_REQUIRE(opt.is_defined("version"));
466 1 : CATCH_REQUIRE(opt.get_string("version") == "");
467 1 : CATCH_REQUIRE(opt.get_string("version", 0) == "");
468 1 : CATCH_REQUIRE(opt["version"] == "");
469 1 : CATCH_REQUIRE_FALSE(opt.has_default("version"));
470 1 : CATCH_REQUIRE(opt.get_default("version").empty());
471 1 : CATCH_REQUIRE(opt.size("version") == 1);
472 :
473 : // other parameters
474 1 : CATCH_REQUIRE(opt.get_program_name() == "arguments");
475 1 : CATCH_REQUIRE(opt.get_program_fullname() == "/usr/bin/arguments");
476 :
477 : // process system options now
478 2 : std::stringstream ss;
479 1 : advgetopt::flag_t const result(opt.process_system_options(ss));
480 1 : CATCH_REQUIRE(result == advgetopt::SYSTEM_OPTION_VERSION);
481 1 : CATCH_REQUIRE(ss.str() == version + '\n');
482 : CATCH_END_SECTION()
483 :
484 4 : CATCH_START_SECTION("Check with the --version system flag, without a --version on the command line")
485 1 : long const major_version(rand());
486 1 : long const minor_version(rand());
487 1 : long const patch_version(rand());
488 1 : long const build_version(rand());
489 3 : std::string const version(std::to_string(major_version)
490 3 : + "."
491 4 : + std::to_string(minor_version)
492 3 : + "."
493 4 : + std::to_string(patch_version)
494 3 : + "."
495 4 : + std::to_string(build_version));
496 :
497 1 : long const default_value(rand());
498 2 : std::string const default_val(std::to_string(default_value));
499 1 : advgetopt::option const options[] =
500 : {
501 : advgetopt::define_option(
502 : advgetopt::Name("size")
503 : , advgetopt::ShortName('s')
504 : , advgetopt::Flags(advgetopt::command_flags<advgetopt::GETOPT_FLAG_REQUIRED>())
505 : , advgetopt::Help("define the size.")
506 : , advgetopt::DefaultValue(default_val.c_str())
507 6 : ),
508 : advgetopt::end_options()
509 7 : };
510 :
511 1 : advgetopt::options_environment environment_options;
512 1 : environment_options.f_project_name = "unittest";
513 1 : environment_options.f_options = options;
514 1 : environment_options.f_environment_flags = advgetopt::GETOPT_ENVIRONMENT_FLAG_SYSTEM_PARAMETERS;
515 1 : environment_options.f_help_header = "Usage: test system commands";
516 1 : environment_options.f_version = version.c_str();
517 :
518 1 : char const * cargv[] =
519 : {
520 : "/usr/bin/arguments",
521 : "--size",
522 : "1221",
523 : nullptr
524 : };
525 1 : int const argc(sizeof(cargv) / sizeof(cargv[0]) - 1);
526 1 : char ** argv = const_cast<char **>(cargv);
527 :
528 2 : advgetopt::getopt opt(environment_options, argc, argv);
529 :
530 : // check that the result is valid
531 :
532 : // an invalid parameter, MUST NOT EXIST
533 1 : CATCH_REQUIRE(opt.get_option("invalid-parameter") == nullptr);
534 1 : CATCH_REQUIRE(opt.get_option('Z') == nullptr);
535 1 : CATCH_REQUIRE_FALSE(opt.is_defined("invalid-parameter"));
536 1 : CATCH_REQUIRE(opt.get_default("invalid-parameter").empty());
537 1 : CATCH_REQUIRE(opt.size("invalid-parameter") == 0);
538 :
539 : // no default
540 1 : CATCH_REQUIRE(opt.get_option("--") == nullptr);
541 1 : CATCH_REQUIRE_FALSE(opt.is_defined("--"));
542 1 : CATCH_REQUIRE(opt.get_default("--").empty());
543 1 : CATCH_REQUIRE(opt.size("--") == 0);
544 :
545 : // valid parameter
546 1 : CATCH_REQUIRE(opt.get_option("size") != nullptr);
547 1 : CATCH_REQUIRE(opt.get_option('s') == opt.get_option("size"));
548 1 : CATCH_REQUIRE(opt.is_defined("size"));
549 1 : CATCH_REQUIRE(opt.get_string("size") == "1221");
550 1 : CATCH_REQUIRE(opt.get_string("size", 0) == "1221");
551 1 : CATCH_REQUIRE(opt["size"] == "1221");
552 1 : CATCH_REQUIRE(opt.get_long("size") == 1221);
553 1 : CATCH_REQUIRE(opt.get_long("size", 0) == 1221);
554 1 : CATCH_REQUIRE(opt.has_default("size"));
555 1 : CATCH_REQUIRE(opt.get_default("size") == default_val);
556 1 : CATCH_REQUIRE(opt.size("size") == 1);
557 :
558 : // version parameter
559 1 : CATCH_REQUIRE(opt.get_option("version") != nullptr);
560 1 : CATCH_REQUIRE(opt.get_option('V') == opt.get_option("version"));
561 1 : CATCH_REQUIRE_FALSE(opt.is_defined("version"));
562 1 : CATCH_REQUIRE_FALSE(opt.has_default("version"));
563 1 : CATCH_REQUIRE(opt.get_default("version").empty());
564 1 : CATCH_REQUIRE(opt.size("version") == 0);
565 :
566 : // other parameters
567 1 : CATCH_REQUIRE(opt.get_program_name() == "arguments");
568 1 : CATCH_REQUIRE(opt.get_program_fullname() == "/usr/bin/arguments");
569 :
570 : // process system options now
571 2 : std::stringstream ss;
572 1 : advgetopt::flag_t const result(opt.process_system_options(ss));
573 1 : CATCH_REQUIRE(result == advgetopt::SYSTEM_OPTION_NONE);
574 1 : CATCH_REQUIRE(ss.str().empty());
575 : CATCH_END_SECTION()
576 2 : }
577 :
578 :
579 :
580 7 : CATCH_TEST_CASE("system_flags_help", "[arguments][valid][getopt][system_flags]")
581 : {
582 10 : CATCH_START_SECTION("Check with the --help system flag")
583 : {
584 1 : advgetopt::option const options[] =
585 : {
586 : advgetopt::define_option(
587 : advgetopt::Name("size")
588 : , advgetopt::ShortName('s')
589 : , advgetopt::Flags(advgetopt::command_flags<advgetopt::GETOPT_FLAG_REQUIRED>())
590 : , advgetopt::Help("define the size.")
591 : , advgetopt::DefaultValue("33")
592 : ),
593 : advgetopt::define_option(
594 : advgetopt::Name("obscure")
595 : , advgetopt::ShortName('o')
596 : , advgetopt::Flags(advgetopt::command_flags<advgetopt::GETOPT_FLAG_REQUIRED
597 : , advgetopt::GETOPT_FLAG_SHOW_GROUP1>())
598 : , advgetopt::Help("obscure command, hidden by default.")
599 : ),
600 : advgetopt::define_option(
601 : advgetopt::Name("secret")
602 : , advgetopt::ShortName('S')
603 : , advgetopt::Flags(advgetopt::command_flags<advgetopt::GETOPT_FLAG_REQUIRED
604 : , advgetopt::GETOPT_FLAG_SHOW_GROUP2>())
605 : , advgetopt::Help("even more secret command, hidden by default.")
606 : ),
607 : advgetopt::end_options()
608 : };
609 :
610 1 : advgetopt::options_environment environment_options;
611 1 : environment_options.f_project_name = "unittest";
612 1 : environment_options.f_options = options;
613 1 : environment_options.f_environment_flags = advgetopt::GETOPT_ENVIRONMENT_FLAG_SYSTEM_PARAMETERS;
614 1 : environment_options.f_help_header = "Usage: test system commands";
615 1 : environment_options.f_help_footer = "Copyright matters";
616 :
617 1 : char const * cargv[] =
618 : {
619 : "/usr/bin/arguments",
620 : "--help",
621 : nullptr
622 : };
623 1 : int const argc(sizeof(cargv) / sizeof(cargv[0]) - 1);
624 1 : char ** argv = const_cast<char **>(cargv);
625 :
626 2 : advgetopt::getopt opt(environment_options, argc, argv);
627 :
628 : // check that the result is valid
629 :
630 : // check the list of options
631 1 : advgetopt::option_info::map_by_name_t const & list_of_options(opt.get_options());
632 1 : CATCH_REQUIRE(list_of_options.size() == 3 + 11 + 1);
633 :
634 : // user options
635 1 : CATCH_REQUIRE(list_of_options.find("size") != list_of_options.end());
636 1 : CATCH_REQUIRE(list_of_options.find("obscure") != list_of_options.end());
637 1 : CATCH_REQUIRE(list_of_options.find("secret") != list_of_options.end());
638 :
639 : // system options
640 1 : CATCH_REQUIRE(list_of_options.find("help") != list_of_options.end());
641 1 : CATCH_REQUIRE(list_of_options.find("long-help") != list_of_options.end());
642 1 : CATCH_REQUIRE(list_of_options.find("version") != list_of_options.end());
643 1 : CATCH_REQUIRE(list_of_options.find("copyright") != list_of_options.end());
644 1 : CATCH_REQUIRE(list_of_options.find("license") != list_of_options.end());
645 1 : CATCH_REQUIRE(list_of_options.find("build-date") != list_of_options.end());
646 1 : CATCH_REQUIRE(list_of_options.find("environment-variable-name") != list_of_options.end());
647 1 : CATCH_REQUIRE(list_of_options.find("configuration-filenames") != list_of_options.end());
648 1 : CATCH_REQUIRE(list_of_options.find("path-to-option-definitions") != list_of_options.end());
649 :
650 : // an invalid parameter, MUST NOT EXIST
651 1 : CATCH_REQUIRE(opt.get_option("invalid-parameter") == nullptr);
652 1 : CATCH_REQUIRE(opt.get_option('Z') == nullptr);
653 1 : CATCH_REQUIRE_FALSE(opt.is_defined("invalid-parameter"));
654 1 : CATCH_REQUIRE(opt.get_default("invalid-parameter").empty());
655 1 : CATCH_REQUIRE(opt.size("invalid-parameter") == 0);
656 :
657 : // no default
658 1 : CATCH_REQUIRE(opt.get_option("--") == nullptr);
659 1 : CATCH_REQUIRE_FALSE(opt.is_defined("--"));
660 1 : CATCH_REQUIRE(opt.get_default("--").empty());
661 1 : CATCH_REQUIRE(opt.size("--") == 0);
662 :
663 : // valid parameter
664 1 : CATCH_REQUIRE(opt.get_option("size") != nullptr);
665 1 : CATCH_REQUIRE(opt.get_option('s') == opt.get_option("size"));
666 1 : CATCH_REQUIRE_FALSE(opt.is_defined("size"));
667 1 : CATCH_REQUIRE(opt.get_string("size") == "33");
668 1 : CATCH_REQUIRE(opt.get_string("size", 0) == "33");
669 1 : CATCH_REQUIRE(opt["size"] == "33");
670 1 : CATCH_REQUIRE(opt.get_long("size") == 33);
671 1 : CATCH_REQUIRE(opt.get_long("size", 0) == 33);
672 1 : CATCH_REQUIRE(opt.has_default("size"));
673 1 : CATCH_REQUIRE(opt.get_default("size") == "33");
674 1 : CATCH_REQUIRE(opt.size("size") == 0);
675 :
676 : // help parameter
677 1 : CATCH_REQUIRE(opt.get_option("help") != nullptr);
678 1 : CATCH_REQUIRE(opt.get_option('h') == opt.get_option("help"));
679 1 : CATCH_REQUIRE(opt.is_defined("help"));
680 1 : CATCH_REQUIRE(opt.get_string("help") == "");
681 1 : CATCH_REQUIRE(opt.get_string("help", 0) == "");
682 1 : CATCH_REQUIRE(opt["help"] == "");
683 1 : CATCH_REQUIRE_FALSE(opt.has_default("help"));
684 1 : CATCH_REQUIRE(opt.get_default("help").empty());
685 1 : CATCH_REQUIRE(opt.size("help") == 1);
686 :
687 : // other parameters
688 1 : CATCH_REQUIRE(opt.get_program_name() == "arguments");
689 1 : CATCH_REQUIRE(opt.get_program_fullname() == "/usr/bin/arguments");
690 :
691 : // process system options now
692 2 : std::stringstream ss;
693 1 : advgetopt::flag_t const result(opt.process_system_options(ss));
694 1 : CATCH_REQUIRE(result == advgetopt::SYSTEM_OPTION_HELP);
695 1 : CATCH_REQUIRE_LONG_STRING(ss.str(),
696 : advgetopt::getopt::breakup_line(
697 : "Usage: test system commands"
698 : , 0
699 : , advgetopt::getopt::get_line_width())
700 : + advgetopt::getopt::format_usage_string(
701 : "--build-date"
702 : , "print out the time and date when arguments was built and exit."
703 : , 30
704 : , advgetopt::getopt::get_line_width())
705 : + advgetopt::getopt::format_usage_string(
706 : "--compiler-version"
707 : , "print the version of the compiler used to compile the advgetopt library."
708 : , 30
709 : , advgetopt::getopt::get_line_width())
710 : + advgetopt::getopt::format_usage_string(
711 : "--configuration-filenames"
712 : , "print out the list of configuration files checked out by this"
713 : " tool."
714 : , 30
715 : , advgetopt::getopt::get_line_width())
716 : + advgetopt::getopt::format_usage_string(
717 : "--copyright or -C"
718 : , "print out the copyright of arguments and exit."
719 : , 30
720 : , advgetopt::getopt::get_line_width())
721 : + advgetopt::getopt::format_usage_string(
722 : "--environment-variable-name"
723 : , "print out the name of the environment variable supported by"
724 : " arguments (if any.)"
725 : , 30
726 : , advgetopt::getopt::get_line_width())
727 : + advgetopt::getopt::format_usage_string(
728 : "--has-sanitizer"
729 : , "print whether the advgetopt was compiled with the sanitizer extension."
730 : , 30
731 : , advgetopt::getopt::get_line_width())
732 : + advgetopt::getopt::format_usage_string(
733 : "--help or -h"
734 : , "print out this help screen and exit."
735 : , 30
736 : , advgetopt::getopt::get_line_width())
737 : + advgetopt::getopt::format_usage_string(
738 : "--license or -L"
739 : , "print out the license of arguments and exit."
740 : , 30
741 : , advgetopt::getopt::get_line_width())
742 : + advgetopt::getopt::format_usage_string(
743 : "--long-help or -?"
744 : , "show all the help from all the available options."
745 : , 30
746 : , advgetopt::getopt::get_line_width())
747 : + advgetopt::getopt::format_usage_string(
748 : "--path-to-option-definitions"
749 : , "print out the path to the option definitons."
750 : , 30
751 : , advgetopt::getopt::get_line_width())
752 : + advgetopt::getopt::format_usage_string(
753 : "--show-option-sources"
754 : , "parse all the options and then print out the source of each"
755 : " value and each override."
756 : , 30
757 : , advgetopt::getopt::get_line_width())
758 : + advgetopt::getopt::format_usage_string(
759 : "--size or -s <arg> (default is \"33\")"
760 : , "define the size."
761 : , 30
762 : , advgetopt::getopt::get_line_width())
763 : + advgetopt::getopt::format_usage_string(
764 : "--version or -V"
765 : , "print out the version of arguments and exit."
766 : , 30
767 : , advgetopt::getopt::get_line_width())
768 : + "\n"
769 : "Copyright matters\n"
770 : "\n"
771 : );
772 : }
773 : CATCH_END_SECTION()
774 :
775 10 : CATCH_START_SECTION("Check with the --long-help system flag")
776 : {
777 1 : advgetopt::option const options[] =
778 : {
779 : advgetopt::define_option(
780 : advgetopt::Name("size")
781 : , advgetopt::ShortName('s')
782 : , advgetopt::Flags(advgetopt::command_flags<advgetopt::GETOPT_FLAG_REQUIRED>())
783 : , advgetopt::Help("define the size.")
784 : , advgetopt::DefaultValue("33")
785 : ),
786 : advgetopt::define_option(
787 : advgetopt::Name("obscure")
788 : , advgetopt::ShortName('o')
789 : , advgetopt::Flags(advgetopt::command_flags<advgetopt::GETOPT_FLAG_REQUIRED
790 : , advgetopt::GETOPT_FLAG_SHOW_GROUP1>())
791 : , advgetopt::Help("obscure command, hidden by default.")
792 : ),
793 : advgetopt::define_option(
794 : advgetopt::Name("secret")
795 : , advgetopt::ShortName('S')
796 : , advgetopt::Flags(advgetopt::command_flags<advgetopt::GETOPT_FLAG_REQUIRED
797 : , advgetopt::GETOPT_FLAG_SHOW_GROUP2>())
798 : , advgetopt::Help("even more secret command, hidden by default.")
799 : ),
800 : advgetopt::end_options()
801 : };
802 :
803 1 : advgetopt::options_environment environment_options;
804 1 : environment_options.f_project_name = "unittest";
805 1 : environment_options.f_options = options;
806 1 : environment_options.f_environment_flags = advgetopt::GETOPT_ENVIRONMENT_FLAG_SYSTEM_PARAMETERS;
807 1 : environment_options.f_help_header = "Usage: test system commands";
808 1 : environment_options.f_help_footer = "Copyright matters";
809 :
810 1 : char const * cargv[] =
811 : {
812 : "/usr/bin/arguments",
813 : "--long-help",
814 : nullptr
815 : };
816 1 : int const argc(sizeof(cargv) / sizeof(cargv[0]) - 1);
817 1 : char ** argv = const_cast<char **>(cargv);
818 :
819 2 : advgetopt::getopt opt(environment_options, argc, argv);
820 :
821 1 : CATCH_REQUIRE(opt.get_group_name() == std::string());
822 :
823 : // check that the result is valid
824 :
825 : // an invalid parameter, MUST NOT EXIST
826 1 : CATCH_REQUIRE(opt.get_option("invalid-parameter") == nullptr);
827 1 : CATCH_REQUIRE(opt.get_option('Z') == nullptr);
828 1 : CATCH_REQUIRE_FALSE(opt.is_defined("invalid-parameter"));
829 1 : CATCH_REQUIRE(opt.get_default("invalid-parameter").empty());
830 1 : CATCH_REQUIRE(opt.size("invalid-parameter") == 0);
831 :
832 : // no default
833 1 : CATCH_REQUIRE(opt.get_option("--") == nullptr);
834 1 : CATCH_REQUIRE_FALSE(opt.is_defined("--"));
835 1 : CATCH_REQUIRE(opt.get_default("--").empty());
836 1 : CATCH_REQUIRE(opt.size("--") == 0);
837 :
838 : // valid parameter
839 1 : CATCH_REQUIRE(opt.get_option("size") != nullptr);
840 1 : CATCH_REQUIRE(opt.get_option('s') == opt.get_option("size"));
841 1 : CATCH_REQUIRE_FALSE(opt.is_defined("size"));
842 1 : CATCH_REQUIRE(opt.get_string("size") == "33");
843 1 : CATCH_REQUIRE(opt.get_string("size", 0) == "33");
844 1 : CATCH_REQUIRE(opt["size"] == "33");
845 1 : CATCH_REQUIRE(opt.get_long("size") == 33);
846 1 : CATCH_REQUIRE(opt.get_long("size", 0) == 33);
847 1 : CATCH_REQUIRE(opt.has_default("size"));
848 1 : CATCH_REQUIRE(opt.get_default("size") == "33");
849 1 : CATCH_REQUIRE(opt.size("size") == 0);
850 :
851 : // help parameter
852 1 : CATCH_REQUIRE(opt.get_option("long-help") != nullptr);
853 1 : CATCH_REQUIRE(opt.get_option('?') == opt.get_option("long-help"));
854 1 : CATCH_REQUIRE(opt.is_defined("long-help"));
855 1 : CATCH_REQUIRE(opt.get_string("long-help") == "");
856 1 : CATCH_REQUIRE(opt.get_string("long-help", 0) == "");
857 1 : CATCH_REQUIRE(opt["long-help"] == "");
858 1 : CATCH_REQUIRE_FALSE(opt.has_default("long-help"));
859 1 : CATCH_REQUIRE(opt.get_default("long-help").empty());
860 1 : CATCH_REQUIRE(opt.size("long-help") == 1);
861 :
862 : // other parameters
863 1 : CATCH_REQUIRE(opt.get_program_name() == "arguments");
864 1 : CATCH_REQUIRE(opt.get_program_fullname() == "/usr/bin/arguments");
865 :
866 : // process system options now
867 2 : std::stringstream ss;
868 1 : advgetopt::flag_t const result(opt.process_system_options(ss));
869 1 : CATCH_REQUIRE(result == advgetopt::SYSTEM_OPTION_HELP);
870 1 : CATCH_REQUIRE_LONG_STRING(ss.str(),
871 : advgetopt::getopt::breakup_line(
872 : "Usage: test system commands"
873 : , 0
874 : , advgetopt::getopt::get_line_width())
875 : + advgetopt::getopt::format_usage_string(
876 : "--build-date"
877 : , "print out the time and date when arguments was built and exit."
878 : , 30
879 : , advgetopt::getopt::get_line_width())
880 : + advgetopt::getopt::format_usage_string(
881 : "--compiler-version"
882 : , "print the version of the compiler used to compile the advgetopt library."
883 : , 30
884 : , advgetopt::getopt::get_line_width())
885 : + advgetopt::getopt::format_usage_string(
886 : "--configuration-filenames"
887 : , "print out the list of configuration files checked out by this"
888 : " tool."
889 : , 30
890 : , advgetopt::getopt::get_line_width())
891 : + advgetopt::getopt::format_usage_string(
892 : "--copyright or -C"
893 : , "print out the copyright of arguments and exit."
894 : , 30
895 : , advgetopt::getopt::get_line_width())
896 : + advgetopt::getopt::format_usage_string(
897 : "--environment-variable-name"
898 : , "print out the name of the environment variable supported by"
899 : " arguments (if any.)"
900 : , 30
901 : , advgetopt::getopt::get_line_width())
902 : + advgetopt::getopt::format_usage_string(
903 : "--has-sanitizer"
904 : , "print whether the advgetopt was compiled with the sanitizer extension."
905 : , 30
906 : , advgetopt::getopt::get_line_width())
907 : + advgetopt::getopt::format_usage_string(
908 : "--help or -h"
909 : , "print out this help screen and exit."
910 : , 30
911 : , advgetopt::getopt::get_line_width())
912 : + advgetopt::getopt::format_usage_string(
913 : "--license or -L"
914 : , "print out the license of arguments and exit."
915 : , 30
916 : , advgetopt::getopt::get_line_width())
917 : + advgetopt::getopt::format_usage_string(
918 : "--long-help or -?"
919 : , "show all the help from all the available options."
920 : , 30
921 : , advgetopt::getopt::get_line_width())
922 : + advgetopt::getopt::format_usage_string(
923 : "--obscure or -o <arg>"
924 : , "obscure command, hidden by default."
925 : , 30
926 : , advgetopt::getopt::get_line_width())
927 : + advgetopt::getopt::format_usage_string(
928 : "--path-to-option-definitions"
929 : , "print out the path to the option definitons."
930 : , 30
931 : , advgetopt::getopt::get_line_width())
932 : + advgetopt::getopt::format_usage_string(
933 : "--secret or -S <arg>"
934 : , "even more secret command, hidden by default."
935 : , 30
936 : , advgetopt::getopt::get_line_width())
937 : + advgetopt::getopt::format_usage_string(
938 : "--show-option-sources"
939 : , "parse all the options and then print out the source of each"
940 : " value and each override."
941 : , 30
942 : , advgetopt::getopt::get_line_width())
943 : + advgetopt::getopt::format_usage_string(
944 : "--size or -s <arg> (default is \"33\")"
945 : , "define the size."
946 : , 30
947 : , advgetopt::getopt::get_line_width())
948 : + advgetopt::getopt::format_usage_string(
949 : "--version or -V"
950 : , "print out the version of arguments and exit."
951 : , 30
952 : , advgetopt::getopt::get_line_width())
953 : + "\n"
954 : "Copyright matters\n"
955 : "\n"
956 : );
957 : }
958 : CATCH_END_SECTION()
959 :
960 10 : CATCH_START_SECTION("Check with the --help system flag, without a --help on the command line")
961 1 : advgetopt::option const options[] =
962 : {
963 : advgetopt::define_option(
964 : advgetopt::Name("size")
965 : , advgetopt::ShortName('s')
966 : , advgetopt::Flags(advgetopt::command_flags<advgetopt::GETOPT_FLAG_REQUIRED>())
967 : , advgetopt::Help("define the size.")
968 : , advgetopt::DefaultValue("33")
969 : ),
970 : advgetopt::end_options()
971 : };
972 :
973 1 : advgetopt::options_environment environment_options;
974 1 : environment_options.f_project_name = "unittest";
975 1 : environment_options.f_options = options;
976 1 : environment_options.f_environment_flags = advgetopt::GETOPT_ENVIRONMENT_FLAG_SYSTEM_PARAMETERS;
977 1 : environment_options.f_help_header = "Usage: test system commands";
978 1 : environment_options.f_help_footer = "Copyright matters";
979 :
980 1 : char const * cargv[] =
981 : {
982 : "/usr/bin/arguments",
983 : "--size",
984 : "1221",
985 : nullptr
986 : };
987 1 : int const argc(sizeof(cargv) / sizeof(cargv[0]) - 1);
988 1 : char ** argv = const_cast<char **>(cargv);
989 :
990 2 : advgetopt::getopt opt(environment_options, argc, argv);
991 :
992 : // check that the result is valid
993 :
994 : // an invalid parameter, MUST NOT EXIST
995 1 : CATCH_REQUIRE(opt.get_option("invalid-parameter") == nullptr);
996 1 : CATCH_REQUIRE(opt.get_option('Z') == nullptr);
997 1 : CATCH_REQUIRE_FALSE(opt.is_defined("invalid-parameter"));
998 1 : CATCH_REQUIRE(opt.get_default("invalid-parameter").empty());
999 1 : CATCH_REQUIRE(opt.size("invalid-parameter") == 0);
1000 :
1001 : // no default
1002 1 : CATCH_REQUIRE(opt.get_option("--") == nullptr);
1003 1 : CATCH_REQUIRE_FALSE(opt.is_defined("--"));
1004 1 : CATCH_REQUIRE(opt.get_default("--").empty());
1005 1 : CATCH_REQUIRE(opt.size("--") == 0);
1006 :
1007 : // valid parameter
1008 1 : CATCH_REQUIRE(opt.get_option("size") != nullptr);
1009 1 : CATCH_REQUIRE(opt.get_option('s') == opt.get_option("size"));
1010 1 : CATCH_REQUIRE(opt.is_defined("size"));
1011 1 : CATCH_REQUIRE(opt.get_string("size") == "1221");
1012 1 : CATCH_REQUIRE(opt.get_string("size", 0) == "1221");
1013 1 : CATCH_REQUIRE(opt["size"] == "1221");
1014 1 : CATCH_REQUIRE(opt.get_long("size") == 1221);
1015 1 : CATCH_REQUIRE(opt.get_long("size", 0) == 1221);
1016 1 : CATCH_REQUIRE(opt.has_default("size"));
1017 1 : CATCH_REQUIRE(opt.get_default("size") == "33");
1018 1 : CATCH_REQUIRE(opt.size("size") == 1);
1019 :
1020 : // help parameter
1021 1 : CATCH_REQUIRE(opt.get_option("help") != nullptr);
1022 1 : CATCH_REQUIRE(opt.get_option('h') == opt.get_option("help"));
1023 1 : CATCH_REQUIRE_FALSE(opt.is_defined("help"));
1024 1 : CATCH_REQUIRE_FALSE(opt.has_default("help"));
1025 1 : CATCH_REQUIRE(opt.get_default("help").empty());
1026 1 : CATCH_REQUIRE(opt.size("help") == 0);
1027 :
1028 : // other parameters
1029 1 : CATCH_REQUIRE(opt.get_program_name() == "arguments");
1030 1 : CATCH_REQUIRE(opt.get_program_fullname() == "/usr/bin/arguments");
1031 :
1032 : // process system options now
1033 2 : std::stringstream ss;
1034 1 : advgetopt::flag_t const result(opt.process_system_options(ss));
1035 1 : CATCH_REQUIRE(result == advgetopt::SYSTEM_OPTION_NONE);
1036 1 : CATCH_REQUIRE(ss.str().empty());
1037 : CATCH_END_SECTION()
1038 :
1039 10 : CATCH_START_SECTION("Check with the --commmands-help system flag")
1040 : {
1041 1 : advgetopt::option const options[] =
1042 : {
1043 : advgetopt::define_option(
1044 : advgetopt::Name("size")
1045 : , advgetopt::ShortName('s')
1046 : , advgetopt::Flags(advgetopt::command_flags<advgetopt::GETOPT_FLAG_REQUIRED
1047 : , advgetopt::GETOPT_FLAG_GROUP_COMMANDS>())
1048 : , advgetopt::Help("define the size.")
1049 : , advgetopt::DefaultValue("33")
1050 : ),
1051 : advgetopt::define_option(
1052 : advgetopt::Name("obscure")
1053 : , advgetopt::ShortName('o')
1054 : , advgetopt::Flags(advgetopt::command_flags<advgetopt::GETOPT_FLAG_REQUIRED
1055 : , advgetopt::GETOPT_FLAG_GROUP_COMMANDS>())
1056 : , advgetopt::Help("obscure command, hidden by default.")
1057 : ),
1058 : advgetopt::define_option(
1059 : advgetopt::Name("secret")
1060 : , advgetopt::ShortName('S')
1061 : , advgetopt::Flags(advgetopt::command_flags<advgetopt::GETOPT_FLAG_REQUIRED
1062 : , advgetopt::GETOPT_FLAG_GROUP_OPTIONS>())
1063 : , advgetopt::Help("even more secret command, hidden by default.")
1064 : ),
1065 : advgetopt::end_options()
1066 : };
1067 :
1068 1 : advgetopt::group_description const groups[] =
1069 : {
1070 : advgetopt::define_group(
1071 : advgetopt::GroupNumber(advgetopt::GETOPT_FLAG_GROUP_COMMANDS)
1072 : , advgetopt::GroupName("commands")
1073 : , advgetopt::GroupDescription("Commands:")
1074 : ),
1075 : advgetopt::define_group(
1076 : advgetopt::GroupNumber(advgetopt::GETOPT_FLAG_GROUP_OPTIONS)
1077 : , advgetopt::GroupName("option")
1078 : , advgetopt::GroupDescription("Options:")
1079 : ),
1080 : advgetopt::end_groups()
1081 : };
1082 :
1083 1 : advgetopt::options_environment environment_options;
1084 1 : environment_options.f_project_name = "unittest";
1085 1 : environment_options.f_options = options;
1086 1 : environment_options.f_environment_flags = advgetopt::GETOPT_ENVIRONMENT_FLAG_SYSTEM_PARAMETERS;
1087 1 : environment_options.f_help_header = "Usage: test system commands";
1088 1 : environment_options.f_help_footer = "Copyright matters";
1089 1 : environment_options.f_groups = groups;
1090 :
1091 1 : char const * cargv[] =
1092 : {
1093 : "/usr/bin/arguments",
1094 : "--commands-help",
1095 : nullptr
1096 : };
1097 1 : int const argc(sizeof(cargv) / sizeof(cargv[0]) - 1);
1098 1 : char ** argv = const_cast<char **>(cargv);
1099 :
1100 2 : advgetopt::getopt opt(environment_options, argc, argv);
1101 :
1102 : // check that the result is valid
1103 :
1104 : // an invalid parameter, MUST NOT EXIST
1105 1 : CATCH_REQUIRE(opt.get_option("invalid-parameter") == nullptr);
1106 1 : CATCH_REQUIRE(opt.get_option('Z') == nullptr);
1107 1 : CATCH_REQUIRE_FALSE(opt.is_defined("invalid-parameter"));
1108 1 : CATCH_REQUIRE(opt.get_default("invalid-parameter").empty());
1109 1 : CATCH_REQUIRE(opt.size("invalid-parameter") == 0);
1110 :
1111 : // no default
1112 1 : CATCH_REQUIRE(opt.get_option("--") == nullptr);
1113 1 : CATCH_REQUIRE_FALSE(opt.is_defined("--"));
1114 1 : CATCH_REQUIRE(opt.get_default("--").empty());
1115 1 : CATCH_REQUIRE(opt.size("--") == 0);
1116 :
1117 : // valid parameter
1118 1 : CATCH_REQUIRE(opt.get_option("size") != nullptr);
1119 1 : CATCH_REQUIRE(opt.get_option('s') == opt.get_option("size"));
1120 1 : CATCH_REQUIRE_FALSE(opt.is_defined("size"));
1121 1 : CATCH_REQUIRE(opt.get_string("size") == "33");
1122 1 : CATCH_REQUIRE(opt.get_string("size", 0) == "33");
1123 1 : CATCH_REQUIRE(opt["size"] == "33");
1124 1 : CATCH_REQUIRE(opt.get_long("size") == 33);
1125 1 : CATCH_REQUIRE(opt.get_long("size", 0) == 33);
1126 1 : CATCH_REQUIRE(opt.has_default("size"));
1127 1 : CATCH_REQUIRE(opt.get_default("size") == "33");
1128 1 : CATCH_REQUIRE(opt.size("size") == 0);
1129 :
1130 : // help parameter
1131 1 : CATCH_REQUIRE(opt.get_option("commands-help") != nullptr);
1132 1 : CATCH_REQUIRE(opt.is_defined("commands-help"));
1133 1 : CATCH_REQUIRE(opt.get_string("commands-help") == "");
1134 1 : CATCH_REQUIRE(opt.get_string("commands-help", 0) == "");
1135 1 : CATCH_REQUIRE(opt["commands-help"] == "");
1136 1 : CATCH_REQUIRE_FALSE(opt.has_default("commands-help"));
1137 1 : CATCH_REQUIRE(opt.get_default("commands-help").empty());
1138 1 : CATCH_REQUIRE(opt.size("commands-help") == 1);
1139 :
1140 : // other parameters
1141 1 : CATCH_REQUIRE(opt.get_program_name() == "arguments");
1142 1 : CATCH_REQUIRE(opt.get_program_fullname() == "/usr/bin/arguments");
1143 :
1144 : // process system options now
1145 2 : std::stringstream ss;
1146 1 : advgetopt::flag_t const result(opt.process_system_options(ss));
1147 1 : CATCH_REQUIRE(result == advgetopt::SYSTEM_OPTION_HELP);
1148 1 : CATCH_REQUIRE_LONG_STRING(ss.str(),
1149 : advgetopt::getopt::breakup_line(
1150 : "Usage: test system commands"
1151 : , 0
1152 : , advgetopt::getopt::get_line_width())
1153 : + "\n"
1154 : "Commands:\n"
1155 : + advgetopt::getopt::format_usage_string(
1156 : "--build-date"
1157 : , "print out the time and date when arguments was built and exit."
1158 : , 30
1159 : , advgetopt::getopt::get_line_width())
1160 : + advgetopt::getopt::format_usage_string(
1161 : "--commands-help"
1162 : , "show help from the \"commands\" group of options."
1163 : , 30
1164 : , advgetopt::getopt::get_line_width())
1165 : + advgetopt::getopt::format_usage_string(
1166 : "--compiler-version"
1167 : , "print the version of the compiler used to compile the advgetopt library."
1168 : , 30
1169 : , advgetopt::getopt::get_line_width())
1170 : + advgetopt::getopt::format_usage_string(
1171 : "--configuration-filenames"
1172 : , "print out the list of configuration files checked out by this"
1173 : " tool."
1174 : , 30
1175 : , advgetopt::getopt::get_line_width())
1176 : + advgetopt::getopt::format_usage_string(
1177 : "--copyright or -C"
1178 : , "print out the copyright of arguments and exit."
1179 : , 30
1180 : , advgetopt::getopt::get_line_width())
1181 : + advgetopt::getopt::format_usage_string(
1182 : "--environment-variable-name"
1183 : , "print out the name of the environment variable supported by"
1184 : " arguments (if any.)"
1185 : , 30
1186 : , advgetopt::getopt::get_line_width())
1187 : + advgetopt::getopt::format_usage_string(
1188 : "--has-sanitizer"
1189 : , "print whether the advgetopt was compiled with the sanitizer extension."
1190 : , 30
1191 : , advgetopt::getopt::get_line_width())
1192 : + advgetopt::getopt::format_usage_string(
1193 : "--help or -h"
1194 : , "print out this help screen and exit."
1195 : , 30
1196 : , advgetopt::getopt::get_line_width())
1197 : + advgetopt::getopt::format_usage_string(
1198 : "--license or -L"
1199 : , "print out the license of arguments and exit."
1200 : , 30
1201 : , advgetopt::getopt::get_line_width())
1202 : + advgetopt::getopt::format_usage_string(
1203 : "--obscure or -o <arg>"
1204 : , "obscure command, hidden by default."
1205 : , 30
1206 : , advgetopt::getopt::get_line_width())
1207 : + advgetopt::getopt::format_usage_string(
1208 : "--option-help"
1209 : , "show help from the \"option\" group of options."
1210 : , 30
1211 : , advgetopt::getopt::get_line_width())
1212 : + advgetopt::getopt::format_usage_string(
1213 : "--path-to-option-definitions"
1214 : , "print out the path to the option definitons."
1215 : , 30
1216 : , advgetopt::getopt::get_line_width())
1217 : + advgetopt::getopt::format_usage_string(
1218 : "--show-option-sources"
1219 : , "parse all the options and then print out the source of each"
1220 : " value and each override."
1221 : , 30
1222 : , advgetopt::getopt::get_line_width())
1223 : + advgetopt::getopt::format_usage_string(
1224 : "--size or -s <arg> (default is \"33\")"
1225 : , "define the size."
1226 : , 30
1227 : , advgetopt::getopt::get_line_width())
1228 : + advgetopt::getopt::format_usage_string(
1229 : "--version or -V"
1230 : , "print out the version of arguments and exit."
1231 : , 30
1232 : , advgetopt::getopt::get_line_width())
1233 : + "\n"
1234 : "Copyright matters\n"
1235 : "\n"
1236 : );
1237 : }
1238 : CATCH_END_SECTION()
1239 :
1240 10 : CATCH_START_SECTION("Check with the --options-help system flag")
1241 : {
1242 1 : advgetopt::option const options[] =
1243 : {
1244 : advgetopt::define_option(
1245 : advgetopt::Name("size")
1246 : , advgetopt::ShortName('s')
1247 : , advgetopt::Flags(advgetopt::command_flags<advgetopt::GETOPT_FLAG_REQUIRED
1248 : , advgetopt::GETOPT_FLAG_GROUP_COMMANDS>())
1249 : , advgetopt::Help("define the size.")
1250 : , advgetopt::DefaultValue("33")
1251 : ),
1252 : advgetopt::define_option(
1253 : advgetopt::Name("obscure")
1254 : , advgetopt::ShortName('o')
1255 : , advgetopt::Flags(advgetopt::command_flags<advgetopt::GETOPT_FLAG_REQUIRED
1256 : , advgetopt::GETOPT_FLAG_GROUP_COMMANDS>())
1257 : , advgetopt::Help("obscure command, hidden by default.")
1258 : ),
1259 : advgetopt::define_option(
1260 : advgetopt::Name("secret")
1261 : , advgetopt::ShortName('S')
1262 : , advgetopt::Flags(advgetopt::command_flags<advgetopt::GETOPT_FLAG_REQUIRED
1263 : , advgetopt::GETOPT_FLAG_GROUP_OPTIONS>())
1264 : , advgetopt::Help("even more secret command, hidden by default.")
1265 : ),
1266 : advgetopt::end_options()
1267 : };
1268 :
1269 1 : advgetopt::group_description const groups[] =
1270 : {
1271 : advgetopt::define_group(
1272 : advgetopt::GroupNumber(advgetopt::GETOPT_FLAG_GROUP_COMMANDS)
1273 : , advgetopt::GroupName("commands")
1274 : , advgetopt::GroupDescription("Commands:")
1275 : ),
1276 : advgetopt::define_group(
1277 : advgetopt::GroupNumber(advgetopt::GETOPT_FLAG_GROUP_OPTIONS)
1278 : , advgetopt::GroupName("options")
1279 : , advgetopt::GroupDescription("Options:")
1280 : ),
1281 : advgetopt::end_groups()
1282 : };
1283 :
1284 1 : advgetopt::options_environment environment_options;
1285 1 : environment_options.f_project_name = "unittest";
1286 1 : environment_options.f_options = options;
1287 1 : environment_options.f_environment_flags = advgetopt::GETOPT_ENVIRONMENT_FLAG_SYSTEM_PARAMETERS;
1288 1 : environment_options.f_help_header = "Usage: test system commands";
1289 1 : environment_options.f_help_footer = "Copyright matters";
1290 1 : environment_options.f_groups = groups;
1291 :
1292 1 : char const * cargv[] =
1293 : {
1294 : "/usr/bin/arguments",
1295 : "--options-help",
1296 : nullptr
1297 : };
1298 1 : int const argc(sizeof(cargv) / sizeof(cargv[0]) - 1);
1299 1 : char ** argv = const_cast<char **>(cargv);
1300 :
1301 2 : advgetopt::getopt opt(environment_options, argc, argv);
1302 :
1303 : // check that the result is valid
1304 :
1305 : // an invalid parameter, MUST NOT EXIST
1306 1 : CATCH_REQUIRE(opt.get_option("invalid-parameter") == nullptr);
1307 1 : CATCH_REQUIRE(opt.get_option('Z') == nullptr);
1308 1 : CATCH_REQUIRE_FALSE(opt.is_defined("invalid-parameter"));
1309 1 : CATCH_REQUIRE(opt.get_default("invalid-parameter").empty());
1310 1 : CATCH_REQUIRE(opt.size("invalid-parameter") == 0);
1311 :
1312 : // no default
1313 1 : CATCH_REQUIRE(opt.get_option("--") == nullptr);
1314 1 : CATCH_REQUIRE_FALSE(opt.is_defined("--"));
1315 1 : CATCH_REQUIRE(opt.get_default("--").empty());
1316 1 : CATCH_REQUIRE(opt.size("--") == 0);
1317 :
1318 : // valid parameter
1319 1 : CATCH_REQUIRE(opt.get_option("size") != nullptr);
1320 1 : CATCH_REQUIRE(opt.get_option('s') == opt.get_option("size"));
1321 1 : CATCH_REQUIRE_FALSE(opt.is_defined("size"));
1322 1 : CATCH_REQUIRE(opt.get_string("size") == "33");
1323 1 : CATCH_REQUIRE(opt.get_string("size", 0) == "33");
1324 1 : CATCH_REQUIRE(opt["size"] == "33");
1325 1 : CATCH_REQUIRE(opt.get_long("size") == 33);
1326 1 : CATCH_REQUIRE(opt.get_long("size", 0) == 33);
1327 1 : CATCH_REQUIRE(opt.has_default("size"));
1328 1 : CATCH_REQUIRE(opt.get_default("size") == "33");
1329 1 : CATCH_REQUIRE(opt.size("size") == 0);
1330 :
1331 : // help parameter
1332 1 : CATCH_REQUIRE(opt.get_option("options-help") != nullptr);
1333 1 : CATCH_REQUIRE(opt.is_defined("options-help"));
1334 1 : CATCH_REQUIRE(opt.get_string("options-help") == "");
1335 1 : CATCH_REQUIRE(opt.get_string("options-help", 0) == "");
1336 1 : CATCH_REQUIRE(opt["options-help"] == "");
1337 1 : CATCH_REQUIRE_FALSE(opt.has_default("options-help"));
1338 1 : CATCH_REQUIRE(opt.get_default("options-help").empty());
1339 1 : CATCH_REQUIRE(opt.size("options-help") == 1);
1340 :
1341 : // other parameters
1342 1 : CATCH_REQUIRE(opt.get_program_name() == "arguments");
1343 1 : CATCH_REQUIRE(opt.get_program_fullname() == "/usr/bin/arguments");
1344 :
1345 : // process system options now
1346 2 : std::stringstream ss;
1347 1 : advgetopt::flag_t const result(opt.process_system_options(ss));
1348 1 : CATCH_REQUIRE(result == advgetopt::SYSTEM_OPTION_HELP);
1349 1 : CATCH_REQUIRE_LONG_STRING(ss.str(),
1350 : "Usage: test system commands\n"
1351 : "\n"
1352 : "Options:\n"
1353 : " --secret or -S <arg> even more secret command, hidden by default.\n"
1354 : "\n"
1355 : "Copyright matters\n"
1356 : "\n"
1357 : );
1358 : }
1359 : CATCH_END_SECTION()
1360 5 : }
1361 :
1362 :
1363 :
1364 4 : CATCH_TEST_CASE("system_flags_copyright", "[arguments][valid][getopt][system_flags]")
1365 : {
1366 4 : CATCH_START_SECTION("Check with the --copyright system flag")
1367 : {
1368 1 : advgetopt::option const options[] =
1369 : {
1370 : advgetopt::define_option(
1371 : advgetopt::Name("size")
1372 : , advgetopt::ShortName('s')
1373 : , advgetopt::Flags(advgetopt::command_flags<advgetopt::GETOPT_FLAG_REQUIRED>())
1374 : , advgetopt::Help("define the size.")
1375 : , advgetopt::DefaultValue("23")
1376 : ),
1377 : advgetopt::end_options()
1378 : };
1379 :
1380 1 : advgetopt::options_environment environment_options;
1381 1 : environment_options.f_project_name = "unittest";
1382 1 : environment_options.f_options = options;
1383 1 : environment_options.f_environment_flags = advgetopt::GETOPT_ENVIRONMENT_FLAG_SYSTEM_PARAMETERS;
1384 1 : environment_options.f_copyright = "Copyright (c) " BOOST_PP_STRINGIZE(UTC_BUILD_YEAR) " Made to Order Software Corporation";
1385 :
1386 1 : char const * cargv[] =
1387 : {
1388 : "/usr/bin/arguments",
1389 : "--copyright",
1390 : nullptr
1391 : };
1392 1 : int const argc(sizeof(cargv) / sizeof(cargv[0]) - 1);
1393 1 : char ** argv = const_cast<char **>(cargv);
1394 :
1395 2 : advgetopt::getopt opt(environment_options, argc, argv);
1396 :
1397 : // check that the result is valid
1398 :
1399 : // an invalid parameter, MUST NOT EXIST
1400 1 : CATCH_REQUIRE(opt.get_option("invalid-parameter") == nullptr);
1401 1 : CATCH_REQUIRE(opt.get_option('Z') == nullptr);
1402 1 : CATCH_REQUIRE_FALSE(opt.is_defined("invalid-parameter"));
1403 1 : CATCH_REQUIRE(opt.get_default("invalid-parameter").empty());
1404 1 : CATCH_REQUIRE(opt.size("invalid-parameter") == 0);
1405 :
1406 : // no default
1407 1 : CATCH_REQUIRE(opt.get_option("--") == nullptr);
1408 1 : CATCH_REQUIRE_FALSE(opt.is_defined("--"));
1409 1 : CATCH_REQUIRE(opt.get_default("--").empty());
1410 1 : CATCH_REQUIRE(opt.size("--") == 0);
1411 :
1412 : // valid parameter
1413 1 : CATCH_REQUIRE(opt.get_option("size") != nullptr);
1414 1 : CATCH_REQUIRE(opt.get_option('s') == opt.get_option("size"));
1415 1 : CATCH_REQUIRE_FALSE(opt.is_defined("size"));
1416 1 : CATCH_REQUIRE(opt.get_string("size") == "23");
1417 1 : CATCH_REQUIRE(opt.get_string("size", 0) == "23");
1418 1 : CATCH_REQUIRE(opt["size"] == "23");
1419 1 : CATCH_REQUIRE(opt.get_long("size") == 23);
1420 1 : CATCH_REQUIRE(opt.get_long("size", 0) == 23);
1421 1 : CATCH_REQUIRE(opt.has_default("size"));
1422 1 : CATCH_REQUIRE(opt.get_default("size") == "23");
1423 1 : CATCH_REQUIRE(opt.size("size") == 0);
1424 :
1425 : // copyright parameter
1426 1 : CATCH_REQUIRE(opt.get_option("copyright") != nullptr);
1427 1 : CATCH_REQUIRE(opt.get_option('C') == opt.get_option("copyright"));
1428 1 : CATCH_REQUIRE(opt.is_defined("copyright"));
1429 1 : CATCH_REQUIRE(opt.get_string("copyright") == "");
1430 1 : CATCH_REQUIRE(opt.get_string("copyright", 0) == "");
1431 1 : CATCH_REQUIRE(opt["copyright"] == "");
1432 1 : CATCH_REQUIRE_FALSE(opt.has_default("copyright"));
1433 1 : CATCH_REQUIRE(opt.get_default("copyright").empty());
1434 1 : CATCH_REQUIRE(opt.size("copyright") == 1);
1435 :
1436 : // other parameters
1437 1 : CATCH_REQUIRE(opt.get_program_name() == "arguments");
1438 1 : CATCH_REQUIRE(opt.get_program_fullname() == "/usr/bin/arguments");
1439 :
1440 : // process system options now
1441 2 : std::stringstream ss;
1442 1 : advgetopt::flag_t const result(opt.process_system_options(ss));
1443 1 : CATCH_REQUIRE(result == advgetopt::SYSTEM_OPTION_COPYRIGHT);
1444 1 : CATCH_REQUIRE(ss.str() == "Copyright (c) " BOOST_PP_STRINGIZE(UTC_BUILD_YEAR) " Made to Order Software Corporation\n");
1445 : }
1446 : CATCH_END_SECTION()
1447 :
1448 4 : CATCH_START_SECTION("Check with the --copyright system flag, without a --copyright on the command line")
1449 1 : advgetopt::option const options[] =
1450 : {
1451 : advgetopt::define_option(
1452 : advgetopt::Name("size")
1453 : , advgetopt::ShortName('s')
1454 : , advgetopt::Flags(advgetopt::command_flags<advgetopt::GETOPT_FLAG_REQUIRED>())
1455 : , advgetopt::Help("define the size.")
1456 : , advgetopt::DefaultValue("53")
1457 : ),
1458 : advgetopt::end_options()
1459 : };
1460 :
1461 1 : advgetopt::options_environment environment_options;
1462 1 : environment_options.f_project_name = "unittest";
1463 1 : environment_options.f_options = options;
1464 1 : environment_options.f_environment_flags = advgetopt::GETOPT_ENVIRONMENT_FLAG_SYSTEM_PARAMETERS;
1465 1 : environment_options.f_help_header = "Usage: test system commands";
1466 1 : environment_options.f_help_footer = "Copyright matters";
1467 :
1468 1 : char const * cargv[] =
1469 : {
1470 : "/usr/bin/arguments",
1471 : "--size",
1472 : "1221",
1473 : nullptr
1474 : };
1475 1 : int const argc(sizeof(cargv) / sizeof(cargv[0]) - 1);
1476 1 : char ** argv = const_cast<char **>(cargv);
1477 :
1478 2 : advgetopt::getopt opt(environment_options, argc, argv);
1479 :
1480 : // check that the result is valid
1481 :
1482 : // an invalid parameter, MUST NOT EXIST
1483 1 : CATCH_REQUIRE(opt.get_option("invalid-parameter") == nullptr);
1484 1 : CATCH_REQUIRE(opt.get_option('Z') == nullptr);
1485 1 : CATCH_REQUIRE_FALSE(opt.is_defined("invalid-parameter"));
1486 1 : CATCH_REQUIRE(opt.get_default("invalid-parameter").empty());
1487 1 : CATCH_REQUIRE(opt.size("invalid-parameter") == 0);
1488 :
1489 : // no default
1490 1 : CATCH_REQUIRE(opt.get_option("--") == nullptr);
1491 1 : CATCH_REQUIRE_FALSE(opt.is_defined("--"));
1492 1 : CATCH_REQUIRE(opt.get_default("--").empty());
1493 1 : CATCH_REQUIRE(opt.size("--") == 0);
1494 :
1495 : // valid parameter
1496 1 : CATCH_REQUIRE(opt.get_option("size") != nullptr);
1497 1 : CATCH_REQUIRE(opt.get_option('s') == opt.get_option("size"));
1498 1 : CATCH_REQUIRE(opt.is_defined("size"));
1499 1 : CATCH_REQUIRE(opt.get_string("size") == "1221");
1500 1 : CATCH_REQUIRE(opt.get_string("size", 0) == "1221");
1501 1 : CATCH_REQUIRE(opt["size"] == "1221");
1502 1 : CATCH_REQUIRE(opt.get_long("size") == 1221);
1503 1 : CATCH_REQUIRE(opt.get_long("size", 0) == 1221);
1504 1 : CATCH_REQUIRE(opt.has_default("size"));
1505 1 : CATCH_REQUIRE(opt.get_default("size") == "53");
1506 1 : CATCH_REQUIRE(opt.size("size") == 1);
1507 :
1508 : // copyright parameter
1509 1 : CATCH_REQUIRE(opt.get_option("copyright") != nullptr);
1510 1 : CATCH_REQUIRE(opt.get_option('C') == opt.get_option("copyright"));
1511 1 : CATCH_REQUIRE_FALSE(opt.is_defined("copyright"));
1512 1 : CATCH_REQUIRE_FALSE(opt.has_default("copyright"));
1513 1 : CATCH_REQUIRE(opt.get_default("copyright").empty());
1514 1 : CATCH_REQUIRE(opt.size("copyright") == 0);
1515 :
1516 : // other parameters
1517 1 : CATCH_REQUIRE(opt.get_program_name() == "arguments");
1518 1 : CATCH_REQUIRE(opt.get_program_fullname() == "/usr/bin/arguments");
1519 :
1520 : // process system options now
1521 2 : std::stringstream ss;
1522 1 : advgetopt::flag_t const result(opt.process_system_options(ss));
1523 1 : CATCH_REQUIRE(result == advgetopt::SYSTEM_OPTION_NONE);
1524 1 : CATCH_REQUIRE(ss.str().empty());
1525 : CATCH_END_SECTION()
1526 2 : }
1527 :
1528 :
1529 :
1530 4 : CATCH_TEST_CASE("system_flags_license", "[arguments][valid][getopt][system_flags]")
1531 : {
1532 4 : CATCH_START_SECTION("Check with the --license system flag")
1533 : {
1534 1 : advgetopt::option const options[] =
1535 : {
1536 : advgetopt::define_option(
1537 : advgetopt::Name("size")
1538 : , advgetopt::ShortName('s')
1539 : , advgetopt::Flags(advgetopt::command_flags<advgetopt::GETOPT_FLAG_REQUIRED>())
1540 : , advgetopt::Help("define the size.")
1541 : , advgetopt::DefaultValue("73")
1542 : ),
1543 : advgetopt::end_options()
1544 : };
1545 :
1546 1 : advgetopt::options_environment environment_options;
1547 1 : environment_options.f_project_name = "unittest";
1548 1 : environment_options.f_options = options;
1549 1 : environment_options.f_environment_flags = advgetopt::GETOPT_ENVIRONMENT_FLAG_SYSTEM_PARAMETERS;
1550 1 : environment_options.f_license = "GPL v2";
1551 :
1552 1 : char const * cargv[] =
1553 : {
1554 : "/usr/bin/arguments",
1555 : "--license",
1556 : nullptr
1557 : };
1558 1 : int const argc(sizeof(cargv) / sizeof(cargv[0]) - 1);
1559 1 : char ** argv = const_cast<char **>(cargv);
1560 :
1561 2 : advgetopt::getopt opt(environment_options, argc, argv);
1562 :
1563 : // check that the result is valid
1564 :
1565 : // an invalid parameter, MUST NOT EXIST
1566 1 : CATCH_REQUIRE(opt.get_option("invalid-parameter") == nullptr);
1567 1 : CATCH_REQUIRE(opt.get_option('Z') == nullptr);
1568 1 : CATCH_REQUIRE_FALSE(opt.is_defined("invalid-parameter"));
1569 1 : CATCH_REQUIRE(opt.get_default("invalid-parameter").empty());
1570 1 : CATCH_REQUIRE(opt.size("invalid-parameter") == 0);
1571 :
1572 : // no default
1573 1 : CATCH_REQUIRE(opt.get_option("--") == nullptr);
1574 1 : CATCH_REQUIRE_FALSE(opt.is_defined("--"));
1575 1 : CATCH_REQUIRE(opt.get_default("--").empty());
1576 1 : CATCH_REQUIRE(opt.size("--") == 0);
1577 :
1578 : // valid parameter
1579 1 : CATCH_REQUIRE(opt.get_option("size") != nullptr);
1580 1 : CATCH_REQUIRE(opt.get_option('s') == opt.get_option("size"));
1581 1 : CATCH_REQUIRE_FALSE(opt.is_defined("size"));
1582 1 : CATCH_REQUIRE(opt.get_string("size") == "73");
1583 1 : CATCH_REQUIRE(opt.get_string("size", 0) == "73");
1584 1 : CATCH_REQUIRE(opt["size"] == "73");
1585 1 : CATCH_REQUIRE(opt.get_long("size") == 73);
1586 1 : CATCH_REQUIRE(opt.get_long("size", 0) == 73);
1587 1 : CATCH_REQUIRE(opt.has_default("size"));
1588 1 : CATCH_REQUIRE(opt.get_default("size") == "73");
1589 1 : CATCH_REQUIRE(opt.size("size") == 0);
1590 :
1591 : // license parameter
1592 1 : CATCH_REQUIRE(opt.get_option("license") != nullptr);
1593 1 : CATCH_REQUIRE(opt.get_option('L') == opt.get_option("license"));
1594 1 : CATCH_REQUIRE(opt.is_defined("license"));
1595 1 : CATCH_REQUIRE(opt.get_string("license") == "");
1596 1 : CATCH_REQUIRE(opt.get_string("license", 0) == "");
1597 1 : CATCH_REQUIRE(opt["license"] == "");
1598 1 : CATCH_REQUIRE_FALSE(opt.has_default("license"));
1599 1 : CATCH_REQUIRE(opt.get_default("license").empty());
1600 1 : CATCH_REQUIRE(opt.size("license") == 1);
1601 :
1602 : // other parameters
1603 1 : CATCH_REQUIRE(opt.get_program_name() == "arguments");
1604 1 : CATCH_REQUIRE(opt.get_program_fullname() == "/usr/bin/arguments");
1605 :
1606 : // process system options now
1607 2 : std::stringstream ss;
1608 1 : advgetopt::flag_t const result(opt.process_system_options(ss));
1609 1 : CATCH_REQUIRE(result == advgetopt::SYSTEM_OPTION_LICENSE);
1610 1 : CATCH_REQUIRE(ss.str() == "GPL v2\n");
1611 : }
1612 : CATCH_END_SECTION()
1613 :
1614 4 : CATCH_START_SECTION("Check with the --license system flag, without a --license on the command line")
1615 1 : advgetopt::option const options[] =
1616 : {
1617 : advgetopt::define_option(
1618 : advgetopt::Name("size")
1619 : , advgetopt::ShortName('s')
1620 : , advgetopt::Flags(advgetopt::command_flags<advgetopt::GETOPT_FLAG_REQUIRED>())
1621 : , advgetopt::Help("define the size.")
1622 : , advgetopt::DefaultValue("103")
1623 : ),
1624 : advgetopt::end_options()
1625 : };
1626 :
1627 1 : advgetopt::options_environment environment_options;
1628 1 : environment_options.f_project_name = "unittest";
1629 1 : environment_options.f_options = options;
1630 1 : environment_options.f_environment_flags = advgetopt::GETOPT_ENVIRONMENT_FLAG_SYSTEM_PARAMETERS;
1631 1 : environment_options.f_help_header = "Usage: test system commands";
1632 1 : environment_options.f_help_footer = "Copyright matters";
1633 :
1634 1 : char const * cargv[] =
1635 : {
1636 : "/usr/bin/arguments",
1637 : "--size",
1638 : "1221",
1639 : nullptr
1640 : };
1641 1 : int const argc(sizeof(cargv) / sizeof(cargv[0]) - 1);
1642 1 : char ** argv = const_cast<char **>(cargv);
1643 :
1644 2 : advgetopt::getopt opt(environment_options, argc, argv);
1645 :
1646 : // check that the result is valid
1647 :
1648 : // an invalid parameter, MUST NOT EXIST
1649 1 : CATCH_REQUIRE(opt.get_option("invalid-parameter") == nullptr);
1650 1 : CATCH_REQUIRE(opt.get_option('Z') == nullptr);
1651 1 : CATCH_REQUIRE_FALSE(opt.is_defined("invalid-parameter"));
1652 1 : CATCH_REQUIRE(opt.get_default("invalid-parameter").empty());
1653 1 : CATCH_REQUIRE(opt.size("invalid-parameter") == 0);
1654 :
1655 : // no default
1656 1 : CATCH_REQUIRE(opt.get_option("--") == nullptr);
1657 1 : CATCH_REQUIRE_FALSE(opt.is_defined("--"));
1658 1 : CATCH_REQUIRE(opt.get_default("--").empty());
1659 1 : CATCH_REQUIRE(opt.size("--") == 0);
1660 :
1661 : // valid parameter
1662 1 : CATCH_REQUIRE(opt.get_option("size") != nullptr);
1663 1 : CATCH_REQUIRE(opt.get_option('s') == opt.get_option("size"));
1664 1 : CATCH_REQUIRE(opt.is_defined("size"));
1665 1 : CATCH_REQUIRE(opt.get_string("size") == "1221");
1666 1 : CATCH_REQUIRE(opt.get_string("size", 0) == "1221");
1667 1 : CATCH_REQUIRE(opt["size"] == "1221");
1668 1 : CATCH_REQUIRE(opt.get_long("size") == 1221);
1669 1 : CATCH_REQUIRE(opt.get_long("size", 0) == 1221);
1670 1 : CATCH_REQUIRE(opt.has_default("size"));
1671 1 : CATCH_REQUIRE(opt.get_default("size") == "103");
1672 1 : CATCH_REQUIRE(opt.size("size") == 1);
1673 :
1674 : // license parameter
1675 1 : CATCH_REQUIRE(opt.get_option("license") != nullptr);
1676 1 : CATCH_REQUIRE(opt.get_option('L') == opt.get_option("license"));
1677 1 : CATCH_REQUIRE_FALSE(opt.is_defined("license"));
1678 1 : CATCH_REQUIRE_FALSE(opt.has_default("license"));
1679 1 : CATCH_REQUIRE(opt.get_default("license").empty());
1680 1 : CATCH_REQUIRE(opt.size("license") == 0);
1681 :
1682 : // other parameters
1683 1 : CATCH_REQUIRE(opt.get_program_name() == "arguments");
1684 1 : CATCH_REQUIRE(opt.get_program_fullname() == "/usr/bin/arguments");
1685 :
1686 : // process system options now
1687 2 : std::stringstream ss;
1688 1 : advgetopt::flag_t const result(opt.process_system_options(ss));
1689 1 : CATCH_REQUIRE(result == advgetopt::SYSTEM_OPTION_NONE);
1690 1 : CATCH_REQUIRE(ss.str().empty());
1691 : CATCH_END_SECTION()
1692 2 : }
1693 :
1694 :
1695 :
1696 4 : CATCH_TEST_CASE("system_flags_build_date", "[arguments][valid][getopt][system_flags]")
1697 : {
1698 4 : CATCH_START_SECTION("Check with the --build-date system flag")
1699 : {
1700 1 : advgetopt::option const options[] =
1701 : {
1702 : advgetopt::define_option(
1703 : advgetopt::Name("size")
1704 : , advgetopt::ShortName('s')
1705 : , advgetopt::Flags(advgetopt::command_flags<advgetopt::GETOPT_FLAG_REQUIRED>())
1706 : , advgetopt::Help("define the size.")
1707 : , advgetopt::DefaultValue("7301")
1708 : ),
1709 : advgetopt::end_options()
1710 : };
1711 :
1712 1 : advgetopt::options_environment environment_options;
1713 1 : environment_options.f_project_name = "unittest";
1714 1 : environment_options.f_options = options;
1715 1 : environment_options.f_environment_flags = advgetopt::GETOPT_ENVIRONMENT_FLAG_SYSTEM_PARAMETERS;
1716 :
1717 1 : char const * cargv[] =
1718 : {
1719 : "/usr/bin/arguments",
1720 : "--build-date",
1721 : nullptr
1722 : };
1723 1 : int const argc(sizeof(cargv) / sizeof(cargv[0]) - 1);
1724 1 : char ** argv = const_cast<char **>(cargv);
1725 :
1726 2 : advgetopt::getopt opt(environment_options, argc, argv);
1727 :
1728 : // check that the result is valid
1729 :
1730 : // an invalid parameter, MUST NOT EXIST
1731 1 : CATCH_REQUIRE(opt.get_option("invalid-parameter") == nullptr);
1732 1 : CATCH_REQUIRE(opt.get_option('Z') == nullptr);
1733 1 : CATCH_REQUIRE_FALSE(opt.is_defined("invalid-parameter"));
1734 1 : CATCH_REQUIRE(opt.get_default("invalid-parameter").empty());
1735 1 : CATCH_REQUIRE(opt.size("invalid-parameter") == 0);
1736 :
1737 : // no default
1738 1 : CATCH_REQUIRE(opt.get_option("--") == nullptr);
1739 1 : CATCH_REQUIRE_FALSE(opt.is_defined("--"));
1740 1 : CATCH_REQUIRE(opt.get_default("--").empty());
1741 1 : CATCH_REQUIRE(opt.size("--") == 0);
1742 :
1743 : // valid parameter
1744 1 : CATCH_REQUIRE(opt.get_option("size") != nullptr);
1745 1 : CATCH_REQUIRE(opt.get_option('s') == opt.get_option("size"));
1746 1 : CATCH_REQUIRE_FALSE(opt.is_defined("size"));
1747 1 : CATCH_REQUIRE(opt.get_string("size") == "7301");
1748 1 : CATCH_REQUIRE(opt.get_string("size", 0) == "7301");
1749 1 : CATCH_REQUIRE(opt["size"] == "7301");
1750 1 : CATCH_REQUIRE(opt.get_long("size") == 7301);
1751 1 : CATCH_REQUIRE(opt.get_long("size", 0) == 7301);
1752 1 : CATCH_REQUIRE(opt.has_default("size"));
1753 1 : CATCH_REQUIRE(opt.get_default("size") == "7301");
1754 1 : CATCH_REQUIRE(opt.size("size") == 0);
1755 :
1756 : // build-date parameter
1757 1 : CATCH_REQUIRE(opt.get_option("build-date") != nullptr);
1758 1 : CATCH_REQUIRE(opt.is_defined("build-date"));
1759 1 : CATCH_REQUIRE(opt.get_string("build-date") == "");
1760 1 : CATCH_REQUIRE(opt.get_string("build-date", 0) == "");
1761 1 : CATCH_REQUIRE(opt["build-date"] == "");
1762 1 : CATCH_REQUIRE_FALSE(opt.has_default("build-date"));
1763 1 : CATCH_REQUIRE(opt.get_default("build-date").empty());
1764 1 : CATCH_REQUIRE(opt.size("build-date") == 1);
1765 :
1766 : // other parameters
1767 1 : CATCH_REQUIRE(opt.get_program_name() == "arguments");
1768 1 : CATCH_REQUIRE(opt.get_program_fullname() == "/usr/bin/arguments");
1769 :
1770 : // process system options now
1771 2 : std::stringstream ss;
1772 1 : advgetopt::flag_t const result(opt.process_system_options(ss));
1773 1 : CATCH_REQUIRE(result == advgetopt::SYSTEM_OPTION_BUILD_DATE);
1774 1 : CATCH_REQUIRE(ss.str() == "Built on "
1775 : + std::string(environment_options.f_build_date)
1776 : + " at "
1777 : + environment_options.f_build_time
1778 : + "\n");
1779 : }
1780 : CATCH_END_SECTION()
1781 :
1782 4 : CATCH_START_SECTION("Check with the --build-date system flag, without a --build-date on the command line")
1783 1 : advgetopt::option const options[] =
1784 : {
1785 : advgetopt::define_option(
1786 : advgetopt::Name("size")
1787 : , advgetopt::ShortName('s')
1788 : , advgetopt::Flags(advgetopt::command_flags<advgetopt::GETOPT_FLAG_REQUIRED>())
1789 : , advgetopt::Help("define the size.")
1790 : , advgetopt::DefaultValue("103")
1791 : ),
1792 : advgetopt::end_options()
1793 : };
1794 :
1795 1 : advgetopt::options_environment environment_options;
1796 1 : environment_options.f_project_name = "unittest";
1797 1 : environment_options.f_options = options;
1798 1 : environment_options.f_environment_flags = advgetopt::GETOPT_ENVIRONMENT_FLAG_SYSTEM_PARAMETERS;
1799 1 : environment_options.f_help_header = "Usage: test system commands";
1800 1 : environment_options.f_help_footer = "Copyright matters";
1801 :
1802 1 : char const * cargv[] =
1803 : {
1804 : "/usr/bin/arguments",
1805 : "--size",
1806 : "1221",
1807 : nullptr
1808 : };
1809 1 : int const argc(sizeof(cargv) / sizeof(cargv[0]) - 1);
1810 1 : char ** argv = const_cast<char **>(cargv);
1811 :
1812 2 : advgetopt::getopt opt(environment_options, argc, argv);
1813 :
1814 : // check that the result is valid
1815 :
1816 : // an invalid parameter, MUST NOT EXIST
1817 1 : CATCH_REQUIRE(opt.get_option("invalid-parameter") == nullptr);
1818 1 : CATCH_REQUIRE(opt.get_option('Z') == nullptr);
1819 1 : CATCH_REQUIRE_FALSE(opt.is_defined("invalid-parameter"));
1820 1 : CATCH_REQUIRE(opt.get_default("invalid-parameter").empty());
1821 1 : CATCH_REQUIRE(opt.size("invalid-parameter") == 0);
1822 :
1823 : // no default
1824 1 : CATCH_REQUIRE(opt.get_option("--") == nullptr);
1825 1 : CATCH_REQUIRE_FALSE(opt.is_defined("--"));
1826 1 : CATCH_REQUIRE(opt.get_default("--").empty());
1827 1 : CATCH_REQUIRE(opt.size("--") == 0);
1828 :
1829 : // valid parameter
1830 1 : CATCH_REQUIRE(opt.get_option("size") != nullptr);
1831 1 : CATCH_REQUIRE(opt.get_option('s') == opt.get_option("size"));
1832 1 : CATCH_REQUIRE(opt.is_defined("size"));
1833 1 : CATCH_REQUIRE(opt.get_string("size") == "1221");
1834 1 : CATCH_REQUIRE(opt.get_string("size", 0) == "1221");
1835 1 : CATCH_REQUIRE(opt["size"] == "1221");
1836 1 : CATCH_REQUIRE(opt.get_long("size") == 1221);
1837 1 : CATCH_REQUIRE(opt.get_long("size", 0) == 1221);
1838 1 : CATCH_REQUIRE(opt.has_default("size"));
1839 1 : CATCH_REQUIRE(opt.get_default("size") == "103");
1840 1 : CATCH_REQUIRE(opt.size("size") == 1);
1841 :
1842 : // build-date parameter
1843 1 : CATCH_REQUIRE(opt.get_option("build-date") != nullptr);
1844 1 : CATCH_REQUIRE_FALSE(opt.is_defined("build-date"));
1845 1 : CATCH_REQUIRE_FALSE(opt.has_default("build-date"));
1846 1 : CATCH_REQUIRE(opt.get_default("build-date").empty());
1847 1 : CATCH_REQUIRE(opt.size("build-date") == 0);
1848 :
1849 : // other parameters
1850 1 : CATCH_REQUIRE(opt.get_program_name() == "arguments");
1851 1 : CATCH_REQUIRE(opt.get_program_fullname() == "/usr/bin/arguments");
1852 :
1853 : // process system options now
1854 2 : std::stringstream ss;
1855 1 : advgetopt::flag_t const result(opt.process_system_options(ss));
1856 1 : CATCH_REQUIRE(result == advgetopt::SYSTEM_OPTION_NONE);
1857 1 : CATCH_REQUIRE(ss.str().empty());
1858 : CATCH_END_SECTION()
1859 2 : }
1860 :
1861 :
1862 :
1863 6 : CATCH_TEST_CASE("system_flags_environment_variable_name", "[arguments][valid][getopt][system_flags]")
1864 : {
1865 8 : CATCH_START_SECTION("Check with the --environment-variable-name system flag")
1866 : {
1867 1 : advgetopt::option const options[] =
1868 : {
1869 : advgetopt::define_option(
1870 : advgetopt::Name("size")
1871 : , advgetopt::ShortName('s')
1872 : , advgetopt::Flags(advgetopt::command_flags<advgetopt::GETOPT_FLAG_REQUIRED>())
1873 : , advgetopt::Help("define the size.")
1874 : , advgetopt::DefaultValue("7301")
1875 : ),
1876 : advgetopt::end_options()
1877 : };
1878 :
1879 1 : advgetopt::options_environment environment_options;
1880 1 : environment_options.f_project_name = "unittest";
1881 1 : environment_options.f_options = options;
1882 1 : environment_options.f_environment_flags = advgetopt::GETOPT_ENVIRONMENT_FLAG_SYSTEM_PARAMETERS;
1883 1 : environment_options.f_environment_variable_name = "ADVGETOPT_OPTIONS";
1884 :
1885 1 : char const * cargv[] =
1886 : {
1887 : "/usr/bin/arguments",
1888 : "--environment-variable-name",
1889 : nullptr
1890 : };
1891 1 : int const argc(sizeof(cargv) / sizeof(cargv[0]) - 1);
1892 1 : char ** argv = const_cast<char **>(cargv);
1893 :
1894 2 : advgetopt::getopt opt(environment_options, argc, argv);
1895 :
1896 : // check that the result is valid
1897 :
1898 : // an invalid parameter, MUST NOT EXIST
1899 1 : CATCH_REQUIRE(opt.get_option("invalid-parameter") == nullptr);
1900 1 : CATCH_REQUIRE(opt.get_option('Z') == nullptr);
1901 1 : CATCH_REQUIRE_FALSE(opt.is_defined("invalid-parameter"));
1902 1 : CATCH_REQUIRE(opt.get_default("invalid-parameter").empty());
1903 1 : CATCH_REQUIRE(opt.size("invalid-parameter") == 0);
1904 :
1905 : // no default
1906 1 : CATCH_REQUIRE(opt.get_option("--") == nullptr);
1907 1 : CATCH_REQUIRE_FALSE(opt.is_defined("--"));
1908 1 : CATCH_REQUIRE(opt.get_default("--").empty());
1909 1 : CATCH_REQUIRE(opt.size("--") == 0);
1910 :
1911 : // valid parameter
1912 1 : CATCH_REQUIRE(opt.get_option("size") != nullptr);
1913 1 : CATCH_REQUIRE(opt.get_option('s') == opt.get_option("size"));
1914 1 : CATCH_REQUIRE_FALSE(opt.is_defined("size"));
1915 1 : CATCH_REQUIRE(opt.get_string("size") == "7301");
1916 1 : CATCH_REQUIRE(opt.get_string("size", 0) == "7301");
1917 1 : CATCH_REQUIRE(opt["size"] == "7301");
1918 1 : CATCH_REQUIRE(opt.get_long("size") == 7301);
1919 1 : CATCH_REQUIRE(opt.get_long("size", 0) == 7301);
1920 1 : CATCH_REQUIRE(opt.has_default("size"));
1921 1 : CATCH_REQUIRE(opt.get_default("size") == "7301");
1922 1 : CATCH_REQUIRE(opt.size("size") == 0);
1923 :
1924 : // environment-variable-name parameter
1925 1 : CATCH_REQUIRE(opt.get_option("environment-variable-name") != nullptr);
1926 1 : CATCH_REQUIRE(opt.is_defined("environment-variable-name"));
1927 1 : CATCH_REQUIRE(opt.get_string("environment-variable-name") == "");
1928 1 : CATCH_REQUIRE(opt.get_string("environment-variable-name", 0) == "");
1929 1 : CATCH_REQUIRE(opt["environment-variable-name"] == "");
1930 1 : CATCH_REQUIRE_FALSE(opt.has_default("environment-variable-name"));
1931 1 : CATCH_REQUIRE(opt.get_default("environment-variable-name").empty());
1932 1 : CATCH_REQUIRE(opt.size("environment-variable-name") == 1);
1933 :
1934 : // other parameters
1935 1 : CATCH_REQUIRE(opt.get_program_name() == "arguments");
1936 1 : CATCH_REQUIRE(opt.get_program_fullname() == "/usr/bin/arguments");
1937 :
1938 : // process system options now
1939 2 : std::stringstream ss;
1940 1 : advgetopt::flag_t const result(opt.process_system_options(ss));
1941 1 : CATCH_REQUIRE(result == advgetopt::SYSTEM_OPTION_ENVIRONMENT_VARIABLE_NAME);
1942 1 : CATCH_REQUIRE(ss.str() == "ADVGETOPT_OPTIONS\n");
1943 : }
1944 : CATCH_END_SECTION()
1945 :
1946 8 : CATCH_START_SECTION("Check with the --environment-variable-name system flag with nullptr")
1947 : {
1948 1 : advgetopt::option const options[] =
1949 : {
1950 : advgetopt::define_option(
1951 : advgetopt::Name("size")
1952 : , advgetopt::ShortName('s')
1953 : , advgetopt::Flags(advgetopt::command_flags<advgetopt::GETOPT_FLAG_REQUIRED>())
1954 : , advgetopt::Help("define the size.")
1955 : , advgetopt::DefaultValue("7301")
1956 : ),
1957 : advgetopt::end_options()
1958 : };
1959 :
1960 1 : advgetopt::options_environment environment_options;
1961 1 : environment_options.f_project_name = "unittest";
1962 1 : environment_options.f_options = options;
1963 1 : environment_options.f_environment_flags = advgetopt::GETOPT_ENVIRONMENT_FLAG_SYSTEM_PARAMETERS;
1964 1 : environment_options.f_environment_variable_name = nullptr;
1965 :
1966 1 : char const * cargv[] =
1967 : {
1968 : "/usr/bin/arguments",
1969 : "--environment-variable-name",
1970 : nullptr
1971 : };
1972 1 : int const argc(sizeof(cargv) / sizeof(cargv[0]) - 1);
1973 1 : char ** argv = const_cast<char **>(cargv);
1974 :
1975 2 : advgetopt::getopt opt(environment_options, argc, argv);
1976 :
1977 : // check that the result is valid
1978 :
1979 : // an invalid parameter, MUST NOT EXIST
1980 1 : CATCH_REQUIRE(opt.get_option("invalid-parameter") == nullptr);
1981 1 : CATCH_REQUIRE(opt.get_option('Z') == nullptr);
1982 1 : CATCH_REQUIRE_FALSE(opt.is_defined("invalid-parameter"));
1983 1 : CATCH_REQUIRE(opt.get_default("invalid-parameter").empty());
1984 1 : CATCH_REQUIRE(opt.size("invalid-parameter") == 0);
1985 :
1986 : // no default
1987 1 : CATCH_REQUIRE(opt.get_option("--") == nullptr);
1988 1 : CATCH_REQUIRE_FALSE(opt.is_defined("--"));
1989 1 : CATCH_REQUIRE(opt.get_default("--").empty());
1990 1 : CATCH_REQUIRE(opt.size("--") == 0);
1991 :
1992 : // valid parameter
1993 1 : CATCH_REQUIRE(opt.get_option("size") != nullptr);
1994 1 : CATCH_REQUIRE(opt.get_option('s') == opt.get_option("size"));
1995 1 : CATCH_REQUIRE_FALSE(opt.is_defined("size"));
1996 1 : CATCH_REQUIRE(opt.get_string("size") == "7301");
1997 1 : CATCH_REQUIRE(opt.get_string("size", 0) == "7301");
1998 1 : CATCH_REQUIRE(opt["size"] == "7301");
1999 1 : CATCH_REQUIRE(opt.get_long("size") == 7301);
2000 1 : CATCH_REQUIRE(opt.get_long("size", 0) == 7301);
2001 1 : CATCH_REQUIRE(opt.has_default("size"));
2002 1 : CATCH_REQUIRE(opt.get_default("size") == "7301");
2003 1 : CATCH_REQUIRE(opt.size("size") == 0);
2004 :
2005 : // environment-variable-name parameter
2006 1 : CATCH_REQUIRE(opt.get_option("environment-variable-name") != nullptr);
2007 1 : CATCH_REQUIRE(opt.is_defined("environment-variable-name"));
2008 1 : CATCH_REQUIRE(opt.get_string("environment-variable-name") == "");
2009 1 : CATCH_REQUIRE(opt.get_string("environment-variable-name", 0) == "");
2010 1 : CATCH_REQUIRE(opt["environment-variable-name"] == "");
2011 1 : CATCH_REQUIRE_FALSE(opt.has_default("environment-variable-name"));
2012 1 : CATCH_REQUIRE(opt.get_default("environment-variable-name").empty());
2013 1 : CATCH_REQUIRE(opt.size("environment-variable-name") == 1);
2014 :
2015 : // other parameters
2016 1 : CATCH_REQUIRE(opt.get_program_name() == "arguments");
2017 1 : CATCH_REQUIRE(opt.get_program_fullname() == "/usr/bin/arguments");
2018 :
2019 : // process system options now
2020 2 : std::stringstream ss;
2021 1 : advgetopt::flag_t const result(opt.process_system_options(ss));
2022 1 : CATCH_REQUIRE(result == advgetopt::SYSTEM_OPTION_ENVIRONMENT_VARIABLE_NAME);
2023 1 : CATCH_REQUIRE(ss.str() == "unittest does not support an environment variable.\n");
2024 : }
2025 : CATCH_END_SECTION()
2026 :
2027 8 : CATCH_START_SECTION("Check with the --environment-variable-name system flag with \"\"")
2028 : {
2029 1 : advgetopt::option const options[] =
2030 : {
2031 : advgetopt::define_option(
2032 : advgetopt::Name("size")
2033 : , advgetopt::ShortName('s')
2034 : , advgetopt::Flags(advgetopt::command_flags<advgetopt::GETOPT_FLAG_REQUIRED>())
2035 : , advgetopt::Help("define the size.")
2036 : , advgetopt::DefaultValue("7301")
2037 : ),
2038 : advgetopt::end_options()
2039 : };
2040 :
2041 1 : advgetopt::options_environment environment_options;
2042 1 : environment_options.f_project_name = "unittest";
2043 1 : environment_options.f_options = options;
2044 1 : environment_options.f_environment_flags = advgetopt::GETOPT_ENVIRONMENT_FLAG_SYSTEM_PARAMETERS;
2045 1 : environment_options.f_environment_variable_name = "";
2046 :
2047 1 : char const * cargv[] =
2048 : {
2049 : "/usr/bin/arguments",
2050 : "--environment-variable-name",
2051 : nullptr
2052 : };
2053 1 : int const argc(sizeof(cargv) / sizeof(cargv[0]) - 1);
2054 1 : char ** argv = const_cast<char **>(cargv);
2055 :
2056 2 : advgetopt::getopt opt(environment_options, argc, argv);
2057 :
2058 : // check that the result is valid
2059 :
2060 : // an invalid parameter, MUST NOT EXIST
2061 1 : CATCH_REQUIRE(opt.get_option("invalid-parameter") == nullptr);
2062 1 : CATCH_REQUIRE(opt.get_option('Z') == nullptr);
2063 1 : CATCH_REQUIRE_FALSE(opt.is_defined("invalid-parameter"));
2064 1 : CATCH_REQUIRE(opt.get_default("invalid-parameter").empty());
2065 1 : CATCH_REQUIRE(opt.size("invalid-parameter") == 0);
2066 :
2067 : // no default
2068 1 : CATCH_REQUIRE(opt.get_option("--") == nullptr);
2069 1 : CATCH_REQUIRE_FALSE(opt.is_defined("--"));
2070 1 : CATCH_REQUIRE(opt.get_default("--").empty());
2071 1 : CATCH_REQUIRE(opt.size("--") == 0);
2072 :
2073 : // valid parameter
2074 1 : CATCH_REQUIRE(opt.get_option("size") != nullptr);
2075 1 : CATCH_REQUIRE(opt.get_option('s') == opt.get_option("size"));
2076 1 : CATCH_REQUIRE_FALSE(opt.is_defined("size"));
2077 1 : CATCH_REQUIRE(opt.get_string("size") == "7301");
2078 1 : CATCH_REQUIRE(opt.get_string("size", 0) == "7301");
2079 1 : CATCH_REQUIRE(opt["size"] == "7301");
2080 1 : CATCH_REQUIRE(opt.get_long("size") == 7301);
2081 1 : CATCH_REQUIRE(opt.get_long("size", 0) == 7301);
2082 1 : CATCH_REQUIRE(opt.has_default("size"));
2083 1 : CATCH_REQUIRE(opt.get_default("size") == "7301");
2084 1 : CATCH_REQUIRE(opt.size("size") == 0);
2085 :
2086 : // environment-variable-name parameter
2087 1 : CATCH_REQUIRE(opt.get_option("environment-variable-name") != nullptr);
2088 1 : CATCH_REQUIRE(opt.is_defined("environment-variable-name"));
2089 1 : CATCH_REQUIRE(opt.get_string("environment-variable-name") == "");
2090 1 : CATCH_REQUIRE(opt.get_string("environment-variable-name", 0) == "");
2091 1 : CATCH_REQUIRE(opt["environment-variable-name"] == "");
2092 1 : CATCH_REQUIRE_FALSE(opt.has_default("environment-variable-name"));
2093 1 : CATCH_REQUIRE(opt.get_default("environment-variable-name").empty());
2094 1 : CATCH_REQUIRE(opt.size("environment-variable-name") == 1);
2095 :
2096 : // other parameters
2097 1 : CATCH_REQUIRE(opt.get_program_name() == "arguments");
2098 1 : CATCH_REQUIRE(opt.get_program_fullname() == "/usr/bin/arguments");
2099 :
2100 : // process system options now
2101 2 : std::stringstream ss;
2102 1 : advgetopt::flag_t const result(opt.process_system_options(ss));
2103 1 : CATCH_REQUIRE(result == advgetopt::SYSTEM_OPTION_ENVIRONMENT_VARIABLE_NAME);
2104 1 : CATCH_REQUIRE(ss.str() == "unittest does not support an environment variable.\n");
2105 : }
2106 : CATCH_END_SECTION()
2107 :
2108 8 : CATCH_START_SECTION("Check with the --environment-variable-name system flag, without a --environment-variable-name on the command line")
2109 1 : advgetopt::option const options[] =
2110 : {
2111 : advgetopt::define_option(
2112 : advgetopt::Name("size")
2113 : , advgetopt::ShortName('s')
2114 : , advgetopt::Flags(advgetopt::command_flags<advgetopt::GETOPT_FLAG_REQUIRED>())
2115 : , advgetopt::Help("define the size.")
2116 : , advgetopt::DefaultValue("103")
2117 : ),
2118 : advgetopt::end_options()
2119 : };
2120 :
2121 1 : advgetopt::options_environment environment_options;
2122 1 : environment_options.f_project_name = "unittest";
2123 1 : environment_options.f_options = options;
2124 1 : environment_options.f_environment_flags = advgetopt::GETOPT_ENVIRONMENT_FLAG_SYSTEM_PARAMETERS;
2125 1 : environment_options.f_help_header = "Usage: test system commands";
2126 1 : environment_options.f_help_footer = "Copyright matters";
2127 :
2128 1 : char const * cargv[] =
2129 : {
2130 : "/usr/bin/arguments",
2131 : "--size",
2132 : "1221",
2133 : nullptr
2134 : };
2135 1 : int const argc(sizeof(cargv) / sizeof(cargv[0]) - 1);
2136 1 : char ** argv = const_cast<char **>(cargv);
2137 :
2138 2 : advgetopt::getopt opt(environment_options, argc, argv);
2139 :
2140 : // check that the result is valid
2141 :
2142 : // an invalid parameter, MUST NOT EXIST
2143 1 : CATCH_REQUIRE(opt.get_option("invalid-parameter") == nullptr);
2144 1 : CATCH_REQUIRE(opt.get_option('Z') == nullptr);
2145 1 : CATCH_REQUIRE_FALSE(opt.is_defined("invalid-parameter"));
2146 1 : CATCH_REQUIRE(opt.get_default("invalid-parameter").empty());
2147 1 : CATCH_REQUIRE(opt.size("invalid-parameter") == 0);
2148 :
2149 : // no default
2150 1 : CATCH_REQUIRE(opt.get_option("--") == nullptr);
2151 1 : CATCH_REQUIRE_FALSE(opt.is_defined("--"));
2152 1 : CATCH_REQUIRE(opt.get_default("--").empty());
2153 1 : CATCH_REQUIRE(opt.size("--") == 0);
2154 :
2155 : // valid parameter
2156 1 : CATCH_REQUIRE(opt.get_option("size") != nullptr);
2157 1 : CATCH_REQUIRE(opt.get_option('s') == opt.get_option("size"));
2158 1 : CATCH_REQUIRE(opt.is_defined("size"));
2159 1 : CATCH_REQUIRE(opt.get_string("size") == "1221");
2160 1 : CATCH_REQUIRE(opt.get_string("size", 0) == "1221");
2161 1 : CATCH_REQUIRE(opt["size"] == "1221");
2162 1 : CATCH_REQUIRE(opt.get_long("size") == 1221);
2163 1 : CATCH_REQUIRE(opt.get_long("size", 0) == 1221);
2164 1 : CATCH_REQUIRE(opt.has_default("size"));
2165 1 : CATCH_REQUIRE(opt.get_default("size") == "103");
2166 1 : CATCH_REQUIRE(opt.size("size") == 1);
2167 :
2168 : // environment-variable-name parameter
2169 1 : CATCH_REQUIRE(opt.get_option("environment-variable-name") != nullptr);
2170 1 : CATCH_REQUIRE_FALSE(opt.is_defined("environment-variable-name"));
2171 1 : CATCH_REQUIRE_FALSE(opt.has_default("environment-variable-name"));
2172 1 : CATCH_REQUIRE(opt.get_default("environment-variable-name").empty());
2173 1 : CATCH_REQUIRE(opt.size("environment-variable-name") == 0);
2174 :
2175 : // other parameters
2176 1 : CATCH_REQUIRE(opt.get_program_name() == "arguments");
2177 1 : CATCH_REQUIRE(opt.get_program_fullname() == "/usr/bin/arguments");
2178 :
2179 : // process system options now
2180 2 : std::stringstream ss;
2181 1 : advgetopt::flag_t const result(opt.process_system_options(ss));
2182 1 : CATCH_REQUIRE(result == advgetopt::SYSTEM_OPTION_NONE);
2183 1 : CATCH_REQUIRE(ss.str().empty());
2184 : CATCH_END_SECTION()
2185 4 : }
2186 :
2187 :
2188 :
2189 6 : CATCH_TEST_CASE("system_flags_configuration_filenames", "[arguments][valid][getopt][system_flags]")
2190 : {
2191 8 : CATCH_START_SECTION("Check with the --configuration-filenames system flag")
2192 : {
2193 2 : snap::safe_setenv env("HOME", "/home/advgetopt");
2194 :
2195 1 : advgetopt::option const options[] =
2196 : {
2197 : advgetopt::define_option(
2198 : advgetopt::Name("size")
2199 : , advgetopt::ShortName('s')
2200 : , advgetopt::Flags(advgetopt::command_flags<advgetopt::GETOPT_FLAG_REQUIRED>())
2201 : , advgetopt::Help("define the size.")
2202 : , advgetopt::DefaultValue("3101")
2203 : ),
2204 : advgetopt::end_options()
2205 : };
2206 :
2207 1 : char const * confs[] =
2208 : {
2209 : ".config/file.mdi",
2210 : "/etc/snapwebsites/server.conf",
2211 : "~/.config/advgetopt/snap.conf",
2212 : nullptr
2213 : };
2214 1 : char const * dirs[] =
2215 : {
2216 : ".config",
2217 : "/etc/secret",
2218 : "~/.config/snapwebsites",
2219 : nullptr
2220 : };
2221 :
2222 1 : advgetopt::options_environment environment_options;
2223 1 : environment_options.f_project_name = "unittest";
2224 1 : environment_options.f_options = options;
2225 1 : environment_options.f_environment_flags = advgetopt::GETOPT_ENVIRONMENT_FLAG_SYSTEM_PARAMETERS;
2226 1 : environment_options.f_configuration_files = confs;
2227 1 : environment_options.f_configuration_filename = "snapdb.conf";
2228 1 : environment_options.f_configuration_directories = dirs;
2229 :
2230 1 : char const * cargv[] =
2231 : {
2232 : "/usr/bin/arguments",
2233 : "--configuration-filenames",
2234 : nullptr
2235 : };
2236 1 : int const argc(sizeof(cargv) / sizeof(cargv[0]) - 1);
2237 1 : char ** argv = const_cast<char **>(cargv);
2238 :
2239 2 : advgetopt::getopt opt(environment_options, argc, argv);
2240 :
2241 : // check that the result is valid
2242 :
2243 : // an invalid parameter, MUST NOT EXIST
2244 1 : CATCH_REQUIRE(opt.get_option("invalid-parameter") == nullptr);
2245 1 : CATCH_REQUIRE(opt.get_option('Z') == nullptr);
2246 1 : CATCH_REQUIRE_FALSE(opt.is_defined("invalid-parameter"));
2247 1 : CATCH_REQUIRE(opt.get_default("invalid-parameter").empty());
2248 1 : CATCH_REQUIRE(opt.size("invalid-parameter") == 0);
2249 :
2250 : // no default
2251 1 : CATCH_REQUIRE(opt.get_option("--") == nullptr);
2252 1 : CATCH_REQUIRE_FALSE(opt.is_defined("--"));
2253 1 : CATCH_REQUIRE(opt.get_default("--").empty());
2254 1 : CATCH_REQUIRE(opt.size("--") == 0);
2255 :
2256 : // valid parameter
2257 1 : CATCH_REQUIRE(opt.get_option("size") != nullptr);
2258 1 : CATCH_REQUIRE(opt.get_option('s') == opt.get_option("size"));
2259 1 : CATCH_REQUIRE_FALSE(opt.is_defined("size"));
2260 1 : CATCH_REQUIRE(opt.get_string("size") == "3101");
2261 1 : CATCH_REQUIRE(opt.get_string("size", 0) == "3101");
2262 1 : CATCH_REQUIRE(opt["size"] == "3101");
2263 1 : CATCH_REQUIRE(opt.get_long("size") == 3101);
2264 1 : CATCH_REQUIRE(opt.get_long("size", 0) == 3101);
2265 1 : CATCH_REQUIRE(opt.has_default("size"));
2266 1 : CATCH_REQUIRE(opt.get_default("size") == "3101");
2267 1 : CATCH_REQUIRE(opt.size("size") == 0);
2268 :
2269 : // configuration-filenames parameter
2270 1 : CATCH_REQUIRE(opt.get_option("configuration-filenames") != nullptr);
2271 1 : CATCH_REQUIRE(opt.is_defined("configuration-filenames"));
2272 1 : CATCH_REQUIRE(opt.get_string("configuration-filenames") == "");
2273 1 : CATCH_REQUIRE(opt.get_string("configuration-filenames", 0) == "");
2274 1 : CATCH_REQUIRE(opt["configuration-filenames"] == "");
2275 1 : CATCH_REQUIRE_FALSE(opt.has_default("configuration-filenames"));
2276 1 : CATCH_REQUIRE(opt.get_default("configuration-filenames").empty());
2277 1 : CATCH_REQUIRE(opt.size("configuration-filenames") == 1);
2278 :
2279 : // other parameters
2280 1 : CATCH_REQUIRE(opt.get_program_name() == "arguments");
2281 1 : CATCH_REQUIRE(opt.get_program_fullname() == "/usr/bin/arguments");
2282 :
2283 : // process system options now
2284 2 : std::stringstream ss;
2285 1 : advgetopt::flag_t const result(opt.process_system_options(ss));
2286 1 : CATCH_REQUIRE(result == advgetopt::SYSTEM_OPTION_CONFIGURATION_FILENAMES);
2287 1 : CATCH_REQUIRE(getenv("HOME") != nullptr);
2288 2 : std::string const home(getenv("HOME"));
2289 1 : CATCH_REQUIRE(ss.str() ==
2290 : "Configuration filenames:\n"
2291 : " . .config/file.mdi\n"
2292 : " . .config/unittest.d/50-file.mdi\n"
2293 : " . /etc/snapwebsites/server.conf\n"
2294 : " . /etc/snapwebsites/unittest.d/50-server.conf\n"
2295 : " . " + home + "/.config/advgetopt/snap.conf\n"
2296 : " . .config/snapdb.conf\n"
2297 : " . .config/unittest.d/50-snapdb.conf\n"
2298 : " . /etc/secret/snapdb.conf\n"
2299 : " . /etc/secret/unittest.d/50-snapdb.conf\n"
2300 : " . " + home + "/.config/snapwebsites/snapdb.conf\n"
2301 : );
2302 : }
2303 : CATCH_END_SECTION()
2304 :
2305 8 : CATCH_START_SECTION("Check with the --configuration-filenames system flag with --config-dir too")
2306 : {
2307 2 : snap::safe_setenv env("HOME", "/home/advgetopt");
2308 :
2309 1 : advgetopt::option const options[] =
2310 : {
2311 : advgetopt::define_option(
2312 : advgetopt::Name("size")
2313 : , advgetopt::ShortName('s')
2314 : , advgetopt::Flags(advgetopt::command_flags<advgetopt::GETOPT_FLAG_REQUIRED>())
2315 : , advgetopt::Help("define the size.")
2316 : , advgetopt::DefaultValue("3101")
2317 : ),
2318 : advgetopt::end_options()
2319 : };
2320 :
2321 1 : char const * confs[] =
2322 : {
2323 : ".config/file.mdi",
2324 : "/etc/snapwebsites/server.conf",
2325 : "~/.config/advgetopt/snap.conf",
2326 : nullptr
2327 : };
2328 1 : char const * dirs[] =
2329 : {
2330 : ".config",
2331 : "/etc/secret",
2332 : "~/.config/snapwebsites",
2333 : nullptr
2334 : };
2335 :
2336 1 : advgetopt::options_environment environment_options;
2337 1 : environment_options.f_project_name = "unittest";
2338 1 : environment_options.f_options = options;
2339 1 : environment_options.f_environment_flags = advgetopt::GETOPT_ENVIRONMENT_FLAG_SYSTEM_PARAMETERS;
2340 1 : environment_options.f_configuration_files = confs;
2341 1 : environment_options.f_configuration_filename = "snapdb.conf";
2342 1 : environment_options.f_configuration_directories = dirs;
2343 :
2344 1 : char const * cargv[] =
2345 : {
2346 : "/usr/bin/arguments",
2347 : "--config-dir",
2348 : "/var/lib/advgetopt",
2349 : "--configuration-filenames",
2350 : "--config-dir",
2351 : "/opt/config",
2352 : nullptr
2353 : };
2354 1 : int const argc(sizeof(cargv) / sizeof(cargv[0]) - 1);
2355 1 : char ** argv = const_cast<char **>(cargv);
2356 :
2357 2 : advgetopt::getopt opt(environment_options, argc, argv);
2358 :
2359 : // check that the result is valid
2360 :
2361 : // an invalid parameter, MUST NOT EXIST
2362 1 : CATCH_REQUIRE(opt.get_option("invalid-parameter") == nullptr);
2363 1 : CATCH_REQUIRE(opt.get_option('Z') == nullptr);
2364 1 : CATCH_REQUIRE_FALSE(opt.is_defined("invalid-parameter"));
2365 1 : CATCH_REQUIRE(opt.get_default("invalid-parameter").empty());
2366 1 : CATCH_REQUIRE(opt.size("invalid-parameter") == 0);
2367 :
2368 : // no default
2369 1 : CATCH_REQUIRE(opt.get_option("--") == nullptr);
2370 1 : CATCH_REQUIRE_FALSE(opt.is_defined("--"));
2371 1 : CATCH_REQUIRE(opt.get_default("--").empty());
2372 1 : CATCH_REQUIRE(opt.size("--") == 0);
2373 :
2374 : // valid parameter
2375 1 : CATCH_REQUIRE(opt.get_option("size") != nullptr);
2376 1 : CATCH_REQUIRE(opt.get_option('s') == opt.get_option("size"));
2377 1 : CATCH_REQUIRE_FALSE(opt.is_defined("size"));
2378 1 : CATCH_REQUIRE(opt.get_string("size") == "3101");
2379 1 : CATCH_REQUIRE(opt.get_string("size", 0) == "3101");
2380 1 : CATCH_REQUIRE(opt["size"] == "3101");
2381 1 : CATCH_REQUIRE(opt.get_long("size") == 3101);
2382 1 : CATCH_REQUIRE(opt.get_long("size", 0) == 3101);
2383 1 : CATCH_REQUIRE(opt.has_default("size"));
2384 1 : CATCH_REQUIRE(opt.get_default("size") == "3101");
2385 1 : CATCH_REQUIRE(opt.size("size") == 0);
2386 :
2387 : // configuration-filenames parameter
2388 1 : CATCH_REQUIRE(opt.get_option("configuration-filenames") != nullptr);
2389 1 : CATCH_REQUIRE(opt.is_defined("configuration-filenames"));
2390 1 : CATCH_REQUIRE(opt.get_string("configuration-filenames") == "");
2391 1 : CATCH_REQUIRE(opt.get_string("configuration-filenames", 0) == "");
2392 1 : CATCH_REQUIRE(opt["configuration-filenames"] == "");
2393 1 : CATCH_REQUIRE_FALSE(opt.has_default("configuration-filenames"));
2394 1 : CATCH_REQUIRE(opt.get_default("configuration-filenames").empty());
2395 1 : CATCH_REQUIRE(opt.size("configuration-filenames") == 1);
2396 :
2397 : // other parameters
2398 1 : CATCH_REQUIRE(opt.get_program_name() == "arguments");
2399 1 : CATCH_REQUIRE(opt.get_program_fullname() == "/usr/bin/arguments");
2400 :
2401 : // process system options now
2402 2 : std::stringstream ss;
2403 1 : advgetopt::flag_t const result(opt.process_system_options(ss));
2404 1 : CATCH_REQUIRE(result == (advgetopt::SYSTEM_OPTION_CONFIGURATION_FILENAMES
2405 : | advgetopt::SYSTEM_OPTION_CONFIG_DIR));
2406 1 : CATCH_REQUIRE(getenv("HOME") != nullptr);
2407 2 : std::string const home(getenv("HOME"));
2408 1 : CATCH_REQUIRE(ss.str() ==
2409 : "Configuration filenames:\n"
2410 : " . .config/file.mdi\n"
2411 : " . .config/unittest.d/50-file.mdi\n"
2412 : " . /etc/snapwebsites/server.conf\n"
2413 : " . /etc/snapwebsites/unittest.d/50-server.conf\n"
2414 : " . " + home + "/.config/advgetopt/snap.conf\n"
2415 : " . /var/lib/advgetopt/snapdb.conf\n"
2416 : " . /var/lib/advgetopt/unittest.d/50-snapdb.conf\n"
2417 : " . /opt/config/snapdb.conf\n"
2418 : " . /opt/config/unittest.d/50-snapdb.conf\n"
2419 : " . .config/snapdb.conf\n"
2420 : " . .config/unittest.d/50-snapdb.conf\n"
2421 : " . /etc/secret/snapdb.conf\n"
2422 : " . /etc/secret/unittest.d/50-snapdb.conf\n"
2423 : " . " + home + "/.config/snapwebsites/snapdb.conf\n"
2424 : );
2425 : }
2426 : CATCH_END_SECTION()
2427 :
2428 8 : CATCH_START_SECTION("Check with the --configuration-filenames system flag without any configuration files")
2429 : {
2430 1 : advgetopt::option const options[] =
2431 : {
2432 : advgetopt::define_option(
2433 : advgetopt::Name("size")
2434 : , advgetopt::ShortName('s')
2435 : , advgetopt::Flags(advgetopt::command_flags<advgetopt::GETOPT_FLAG_REQUIRED>())
2436 : , advgetopt::Help("define the size.")
2437 : , advgetopt::DefaultValue("3101")
2438 : ),
2439 : advgetopt::end_options()
2440 : };
2441 :
2442 1 : advgetopt::options_environment environment_options;
2443 1 : environment_options.f_project_name = "unittest";
2444 1 : environment_options.f_options = options;
2445 1 : environment_options.f_environment_flags = advgetopt::GETOPT_ENVIRONMENT_FLAG_SYSTEM_PARAMETERS;
2446 :
2447 1 : char const * cargv[] =
2448 : {
2449 : "/usr/bin/arguments",
2450 : "--configuration-filenames",
2451 : nullptr
2452 : };
2453 1 : int const argc(sizeof(cargv) / sizeof(cargv[0]) - 1);
2454 1 : char ** argv = const_cast<char **>(cargv);
2455 :
2456 2 : advgetopt::getopt opt(environment_options, argc, argv);
2457 :
2458 : // check that the result is valid
2459 :
2460 : // an invalid parameter, MUST NOT EXIST
2461 1 : CATCH_REQUIRE(opt.get_option("invalid-parameter") == nullptr);
2462 1 : CATCH_REQUIRE(opt.get_option('Z') == nullptr);
2463 1 : CATCH_REQUIRE_FALSE(opt.is_defined("invalid-parameter"));
2464 1 : CATCH_REQUIRE(opt.get_default("invalid-parameter").empty());
2465 1 : CATCH_REQUIRE(opt.size("invalid-parameter") == 0);
2466 :
2467 : // no default
2468 1 : CATCH_REQUIRE(opt.get_option("--") == nullptr);
2469 1 : CATCH_REQUIRE_FALSE(opt.is_defined("--"));
2470 1 : CATCH_REQUIRE(opt.get_default("--").empty());
2471 1 : CATCH_REQUIRE(opt.size("--") == 0);
2472 :
2473 : // valid parameter
2474 1 : CATCH_REQUIRE(opt.get_option("size") != nullptr);
2475 1 : CATCH_REQUIRE(opt.get_option('s') == opt.get_option("size"));
2476 1 : CATCH_REQUIRE_FALSE(opt.is_defined("size"));
2477 1 : CATCH_REQUIRE(opt.get_string("size") == "3101");
2478 1 : CATCH_REQUIRE(opt.get_string("size", 0) == "3101");
2479 1 : CATCH_REQUIRE(opt["size"] == "3101");
2480 1 : CATCH_REQUIRE(opt.get_long("size") == 3101);
2481 1 : CATCH_REQUIRE(opt.get_long("size", 0) == 3101);
2482 1 : CATCH_REQUIRE(opt.has_default("size"));
2483 1 : CATCH_REQUIRE(opt.get_default("size") == "3101");
2484 1 : CATCH_REQUIRE(opt.size("size") == 0);
2485 :
2486 : // configuration-filenames parameter
2487 1 : CATCH_REQUIRE(opt.get_option("configuration-filenames") != nullptr);
2488 1 : CATCH_REQUIRE(opt.is_defined("configuration-filenames"));
2489 1 : CATCH_REQUIRE(opt.get_string("configuration-filenames") == "");
2490 1 : CATCH_REQUIRE(opt.get_string("configuration-filenames", 0) == "");
2491 1 : CATCH_REQUIRE(opt["configuration-filenames"] == "");
2492 1 : CATCH_REQUIRE_FALSE(opt.has_default("configuration-filenames"));
2493 1 : CATCH_REQUIRE(opt.get_default("configuration-filenames").empty());
2494 1 : CATCH_REQUIRE(opt.size("configuration-filenames") == 1);
2495 :
2496 : // other parameters
2497 1 : CATCH_REQUIRE(opt.get_program_name() == "arguments");
2498 1 : CATCH_REQUIRE(opt.get_program_fullname() == "/usr/bin/arguments");
2499 :
2500 : // process system options now
2501 2 : std::stringstream ss;
2502 1 : advgetopt::flag_t const result(opt.process_system_options(ss));
2503 1 : CATCH_REQUIRE(result == advgetopt::SYSTEM_OPTION_CONFIGURATION_FILENAMES);
2504 1 : CATCH_REQUIRE(ss.str() == "unittest does not support configuration files.\n");
2505 : }
2506 : CATCH_END_SECTION()
2507 :
2508 8 : CATCH_START_SECTION("Check with the --configuration-filenames system flag, without a --configuration-filenames on the command line")
2509 1 : advgetopt::option const options[] =
2510 : {
2511 : advgetopt::define_option(
2512 : advgetopt::Name("size")
2513 : , advgetopt::ShortName('s')
2514 : , advgetopt::Flags(advgetopt::command_flags<advgetopt::GETOPT_FLAG_REQUIRED>())
2515 : , advgetopt::Help("define the size.")
2516 : , advgetopt::DefaultValue("193")
2517 : ),
2518 : advgetopt::end_options()
2519 : };
2520 :
2521 1 : char const * confs[] =
2522 : {
2523 : ".config/file.mdi",
2524 : "/etc/snapwebsites/server.conf",
2525 : "~/.config/advgetopt/snap.conf",
2526 : nullptr
2527 : };
2528 1 : char const * dirs[] =
2529 : {
2530 : ".config",
2531 : "/etc/secret",
2532 : "~/.config/snapwebsites",
2533 : nullptr
2534 : };
2535 :
2536 1 : advgetopt::options_environment environment_options;
2537 1 : environment_options.f_project_name = "unittest";
2538 1 : environment_options.f_options = options;
2539 1 : environment_options.f_environment_flags = advgetopt::GETOPT_ENVIRONMENT_FLAG_SYSTEM_PARAMETERS;
2540 1 : environment_options.f_help_header = "Usage: test system commands";
2541 1 : environment_options.f_help_footer = "Copyright matters";
2542 1 : environment_options.f_configuration_files = confs;
2543 1 : environment_options.f_configuration_filename = "snapdb.conf";
2544 1 : environment_options.f_configuration_directories = dirs;
2545 :
2546 1 : char const * cargv[] =
2547 : {
2548 : "/usr/bin/arguments",
2549 : "--size",
2550 : "1221",
2551 : nullptr
2552 : };
2553 1 : int const argc(sizeof(cargv) / sizeof(cargv[0]) - 1);
2554 1 : char ** argv = const_cast<char **>(cargv);
2555 :
2556 2 : advgetopt::getopt opt(environment_options, argc, argv);
2557 :
2558 : // check that the result is valid
2559 :
2560 : // an invalid parameter, MUST NOT EXIST
2561 1 : CATCH_REQUIRE(opt.get_option("invalid-parameter") == nullptr);
2562 1 : CATCH_REQUIRE(opt.get_option('Z') == nullptr);
2563 1 : CATCH_REQUIRE_FALSE(opt.is_defined("invalid-parameter"));
2564 1 : CATCH_REQUIRE(opt.get_default("invalid-parameter").empty());
2565 1 : CATCH_REQUIRE(opt.size("invalid-parameter") == 0);
2566 :
2567 : // no default
2568 1 : CATCH_REQUIRE(opt.get_option("--") == nullptr);
2569 1 : CATCH_REQUIRE_FALSE(opt.is_defined("--"));
2570 1 : CATCH_REQUIRE(opt.get_default("--").empty());
2571 1 : CATCH_REQUIRE(opt.size("--") == 0);
2572 :
2573 : // valid parameter
2574 1 : CATCH_REQUIRE(opt.get_option("size") != nullptr);
2575 1 : CATCH_REQUIRE(opt.get_option('s') == opt.get_option("size"));
2576 1 : CATCH_REQUIRE(opt.is_defined("size"));
2577 1 : CATCH_REQUIRE(opt.get_string("size") == "1221");
2578 1 : CATCH_REQUIRE(opt.get_string("size", 0) == "1221");
2579 1 : CATCH_REQUIRE(opt["size"] == "1221");
2580 1 : CATCH_REQUIRE(opt.get_long("size") == 1221);
2581 1 : CATCH_REQUIRE(opt.get_long("size", 0) == 1221);
2582 1 : CATCH_REQUIRE(opt.has_default("size"));
2583 1 : CATCH_REQUIRE(opt.get_default("size") == "193");
2584 1 : CATCH_REQUIRE(opt.size("size") == 1);
2585 :
2586 : // configuration-filenames parameter
2587 1 : CATCH_REQUIRE(opt.get_option("configuration-filenames") != nullptr);
2588 1 : CATCH_REQUIRE_FALSE(opt.is_defined("configuration-filenames"));
2589 1 : CATCH_REQUIRE_FALSE(opt.has_default("configuration-filenames"));
2590 1 : CATCH_REQUIRE(opt.get_default("configuration-filenames").empty());
2591 1 : CATCH_REQUIRE(opt.size("configuration-filenames") == 0);
2592 :
2593 : // other parameters
2594 1 : CATCH_REQUIRE(opt.get_program_name() == "arguments");
2595 1 : CATCH_REQUIRE(opt.get_program_fullname() == "/usr/bin/arguments");
2596 :
2597 : // process system options now
2598 2 : std::stringstream ss;
2599 1 : advgetopt::flag_t const result(opt.process_system_options(ss));
2600 1 : CATCH_REQUIRE(result == advgetopt::SYSTEM_OPTION_NONE);
2601 1 : CATCH_REQUIRE(ss.str().empty());
2602 : CATCH_END_SECTION()
2603 4 : }
2604 :
2605 :
2606 :
2607 5 : CATCH_TEST_CASE("system_flags_path_to_option_definitions", "[arguments][valid][getopt][system_flags]")
2608 : {
2609 6 : CATCH_START_SECTION("Check with the --path-to-option-definitions system flag (Default)")
2610 : {
2611 1 : advgetopt::option const options[] =
2612 : {
2613 : advgetopt::define_option(
2614 : advgetopt::Name("size")
2615 : , advgetopt::ShortName('s')
2616 : , advgetopt::Flags(advgetopt::command_flags<advgetopt::GETOPT_FLAG_REQUIRED>())
2617 : , advgetopt::Help("define the size.")
2618 : , advgetopt::DefaultValue("7301")
2619 : ),
2620 : advgetopt::end_options()
2621 : };
2622 :
2623 1 : advgetopt::options_environment environment_options;
2624 1 : environment_options.f_project_name = "unittest";
2625 1 : environment_options.f_options = options;
2626 1 : environment_options.f_environment_flags = advgetopt::GETOPT_ENVIRONMENT_FLAG_SYSTEM_PARAMETERS;
2627 :
2628 1 : char const * cargv[] =
2629 : {
2630 : "/usr/bin/arguments",
2631 : "--path-to-option-definitions",
2632 : nullptr
2633 : };
2634 1 : int const argc(sizeof(cargv) / sizeof(cargv[0]) - 1);
2635 1 : char ** argv = const_cast<char **>(cargv);
2636 :
2637 2 : advgetopt::getopt opt(environment_options, argc, argv);
2638 :
2639 : // check that the result is valid
2640 :
2641 : // an invalid parameter, MUST NOT EXIST
2642 1 : CATCH_REQUIRE(opt.get_option("invalid-parameter") == nullptr);
2643 1 : CATCH_REQUIRE(opt.get_option('Z') == nullptr);
2644 1 : CATCH_REQUIRE_FALSE(opt.is_defined("invalid-parameter"));
2645 1 : CATCH_REQUIRE(opt.get_default("invalid-parameter").empty());
2646 1 : CATCH_REQUIRE(opt.size("invalid-parameter") == 0);
2647 :
2648 : // no default
2649 1 : CATCH_REQUIRE(opt.get_option("--") == nullptr);
2650 1 : CATCH_REQUIRE_FALSE(opt.is_defined("--"));
2651 1 : CATCH_REQUIRE(opt.get_default("--").empty());
2652 1 : CATCH_REQUIRE(opt.size("--") == 0);
2653 :
2654 : // valid parameter
2655 1 : CATCH_REQUIRE(opt.get_option("size") != nullptr);
2656 1 : CATCH_REQUIRE(opt.get_option('s') == opt.get_option("size"));
2657 1 : CATCH_REQUIRE_FALSE(opt.is_defined("size"));
2658 1 : CATCH_REQUIRE(opt.get_string("size") == "7301");
2659 1 : CATCH_REQUIRE(opt.get_string("size", 0) == "7301");
2660 1 : CATCH_REQUIRE(opt["size"] == "7301");
2661 1 : CATCH_REQUIRE(opt.get_long("size") == 7301);
2662 1 : CATCH_REQUIRE(opt.get_long("size", 0) == 7301);
2663 1 : CATCH_REQUIRE(opt.has_default("size"));
2664 1 : CATCH_REQUIRE(opt.get_default("size") == "7301");
2665 1 : CATCH_REQUIRE(opt.size("size") == 0);
2666 :
2667 : // path-to-option-definitions parameter
2668 1 : CATCH_REQUIRE(opt.get_option("path-to-option-definitions") != nullptr);
2669 1 : CATCH_REQUIRE(opt.is_defined("path-to-option-definitions"));
2670 1 : CATCH_REQUIRE(opt.get_string("path-to-option-definitions") == "");
2671 1 : CATCH_REQUIRE(opt.get_string("path-to-option-definitions", 0) == "");
2672 1 : CATCH_REQUIRE(opt["path-to-option-definitions"] == "");
2673 1 : CATCH_REQUIRE_FALSE(opt.has_default("path-to-option-definitions"));
2674 1 : CATCH_REQUIRE(opt.get_default("path-to-option-definitions").empty());
2675 1 : CATCH_REQUIRE(opt.size("path-to-option-definitions") == 1);
2676 :
2677 : // other parameters
2678 1 : CATCH_REQUIRE(opt.get_program_name() == "arguments");
2679 1 : CATCH_REQUIRE(opt.get_program_fullname() == "/usr/bin/arguments");
2680 :
2681 : // process system options now
2682 2 : std::stringstream ss;
2683 1 : advgetopt::flag_t const result(opt.process_system_options(ss));
2684 1 : CATCH_REQUIRE(result == advgetopt::SYSTEM_OPTION_PATH_TO_OPTION_DEFINITIONS);
2685 1 : CATCH_REQUIRE(ss.str() == "/usr/share/advgetopt/options/\n");
2686 : }
2687 : CATCH_END_SECTION()
2688 :
2689 6 : CATCH_START_SECTION("Check with the --path-to-option-definitions system flag (Specified)")
2690 : {
2691 1 : advgetopt::option const options[] =
2692 : {
2693 : advgetopt::define_option(
2694 : advgetopt::Name("size")
2695 : , advgetopt::ShortName('s')
2696 : , advgetopt::Flags(advgetopt::command_flags<advgetopt::GETOPT_FLAG_REQUIRED>())
2697 : , advgetopt::Help("define the size.")
2698 : , advgetopt::DefaultValue("7301")
2699 : ),
2700 : advgetopt::end_options()
2701 : };
2702 :
2703 1 : advgetopt::options_environment environment_options;
2704 1 : environment_options.f_project_name = "unittest";
2705 1 : environment_options.f_options = options;
2706 1 : environment_options.f_environment_flags = advgetopt::GETOPT_ENVIRONMENT_FLAG_SYSTEM_PARAMETERS;
2707 1 : environment_options.f_options_files_directory = "/opt/advgetopt/configs";
2708 :
2709 1 : char const * cargv[] =
2710 : {
2711 : "/usr/bin/arguments",
2712 : "--path-to-option-definitions",
2713 : nullptr
2714 : };
2715 1 : int const argc(sizeof(cargv) / sizeof(cargv[0]) - 1);
2716 1 : char ** argv = const_cast<char **>(cargv);
2717 :
2718 2 : advgetopt::getopt opt(environment_options, argc, argv);
2719 :
2720 : // check that the result is valid
2721 :
2722 : // an invalid parameter, MUST NOT EXIST
2723 1 : CATCH_REQUIRE(opt.get_option("invalid-parameter") == nullptr);
2724 1 : CATCH_REQUIRE(opt.get_option('Z') == nullptr);
2725 1 : CATCH_REQUIRE_FALSE(opt.is_defined("invalid-parameter"));
2726 1 : CATCH_REQUIRE(opt.get_default("invalid-parameter").empty());
2727 1 : CATCH_REQUIRE(opt.size("invalid-parameter") == 0);
2728 :
2729 : // no default
2730 1 : CATCH_REQUIRE(opt.get_option("--") == nullptr);
2731 1 : CATCH_REQUIRE_FALSE(opt.is_defined("--"));
2732 1 : CATCH_REQUIRE(opt.get_default("--").empty());
2733 1 : CATCH_REQUIRE(opt.size("--") == 0);
2734 :
2735 : // valid parameter
2736 1 : CATCH_REQUIRE(opt.get_option("size") != nullptr);
2737 1 : CATCH_REQUIRE(opt.get_option('s') == opt.get_option("size"));
2738 1 : CATCH_REQUIRE_FALSE(opt.is_defined("size"));
2739 1 : CATCH_REQUIRE(opt.get_string("size") == "7301");
2740 1 : CATCH_REQUIRE(opt.get_string("size", 0) == "7301");
2741 1 : CATCH_REQUIRE(opt["size"] == "7301");
2742 1 : CATCH_REQUIRE(opt.get_long("size") == 7301);
2743 1 : CATCH_REQUIRE(opt.get_long("size", 0) == 7301);
2744 1 : CATCH_REQUIRE(opt.has_default("size"));
2745 1 : CATCH_REQUIRE(opt.get_default("size") == "7301");
2746 1 : CATCH_REQUIRE(opt.size("size") == 0);
2747 :
2748 : // path-to-option-definitions parameter
2749 1 : CATCH_REQUIRE(opt.get_option("path-to-option-definitions") != nullptr);
2750 1 : CATCH_REQUIRE(opt.is_defined("path-to-option-definitions"));
2751 1 : CATCH_REQUIRE(opt.get_string("path-to-option-definitions") == "");
2752 1 : CATCH_REQUIRE(opt.get_string("path-to-option-definitions", 0) == "");
2753 1 : CATCH_REQUIRE(opt["path-to-option-definitions"] == "");
2754 1 : CATCH_REQUIRE_FALSE(opt.has_default("path-to-option-definitions"));
2755 1 : CATCH_REQUIRE(opt.get_default("path-to-option-definitions").empty());
2756 1 : CATCH_REQUIRE(opt.size("path-to-option-definitions") == 1);
2757 :
2758 : // other parameters
2759 1 : CATCH_REQUIRE(opt.get_program_name() == "arguments");
2760 1 : CATCH_REQUIRE(opt.get_program_fullname() == "/usr/bin/arguments");
2761 :
2762 : // process system options now
2763 2 : std::stringstream ss;
2764 1 : advgetopt::flag_t const result(opt.process_system_options(ss));
2765 1 : CATCH_REQUIRE(result == advgetopt::SYSTEM_OPTION_PATH_TO_OPTION_DEFINITIONS);
2766 1 : CATCH_REQUIRE(ss.str() == "/opt/advgetopt/configs/\n");
2767 : }
2768 : CATCH_END_SECTION()
2769 :
2770 6 : CATCH_START_SECTION("Check with the --path-to-option-definitions system flag, without a --path-to-option-definitions on the command line")
2771 1 : advgetopt::option const options[] =
2772 : {
2773 : advgetopt::define_option(
2774 : advgetopt::Name("size")
2775 : , advgetopt::ShortName('s')
2776 : , advgetopt::Flags(advgetopt::command_flags<advgetopt::GETOPT_FLAG_REQUIRED>())
2777 : , advgetopt::Help("define the size.")
2778 : , advgetopt::DefaultValue("303")
2779 : ),
2780 : advgetopt::end_options()
2781 : };
2782 :
2783 1 : advgetopt::options_environment environment_options;
2784 1 : environment_options.f_project_name = "unittest";
2785 1 : environment_options.f_options = options;
2786 1 : environment_options.f_environment_flags = advgetopt::GETOPT_ENVIRONMENT_FLAG_SYSTEM_PARAMETERS;
2787 1 : environment_options.f_help_header = "Usage: test system commands";
2788 1 : environment_options.f_help_footer = "Copyright matters";
2789 :
2790 1 : char const * cargv[] =
2791 : {
2792 : "/usr/bin/arguments",
2793 : "--size",
2794 : "1919",
2795 : nullptr
2796 : };
2797 1 : int const argc(sizeof(cargv) / sizeof(cargv[0]) - 1);
2798 1 : char ** argv = const_cast<char **>(cargv);
2799 :
2800 2 : advgetopt::getopt opt(environment_options, argc, argv);
2801 :
2802 : // check that the result is valid
2803 :
2804 : // an invalid parameter, MUST NOT EXIST
2805 1 : CATCH_REQUIRE(opt.get_option("invalid-parameter") == nullptr);
2806 1 : CATCH_REQUIRE(opt.get_option('Z') == nullptr);
2807 1 : CATCH_REQUIRE_FALSE(opt.is_defined("invalid-parameter"));
2808 1 : CATCH_REQUIRE(opt.get_default("invalid-parameter").empty());
2809 1 : CATCH_REQUIRE(opt.size("invalid-parameter") == 0);
2810 :
2811 : // no default
2812 1 : CATCH_REQUIRE(opt.get_option("--") == nullptr);
2813 1 : CATCH_REQUIRE_FALSE(opt.is_defined("--"));
2814 1 : CATCH_REQUIRE(opt.get_default("--").empty());
2815 1 : CATCH_REQUIRE(opt.size("--") == 0);
2816 :
2817 : // valid parameter
2818 1 : CATCH_REQUIRE(opt.get_option("size") != nullptr);
2819 1 : CATCH_REQUIRE(opt.get_option('s') == opt.get_option("size"));
2820 1 : CATCH_REQUIRE(opt.is_defined("size"));
2821 1 : CATCH_REQUIRE(opt.get_string("size") == "1919");
2822 1 : CATCH_REQUIRE(opt.get_string("size", 0) == "1919");
2823 1 : CATCH_REQUIRE(opt["size"] == "1919");
2824 1 : CATCH_REQUIRE(opt.get_long("size") == 1919);
2825 1 : CATCH_REQUIRE(opt.get_long("size", 0) == 1919);
2826 1 : CATCH_REQUIRE(opt.has_default("size"));
2827 1 : CATCH_REQUIRE(opt.get_default("size") == "303");
2828 1 : CATCH_REQUIRE(opt.size("size") == 1);
2829 :
2830 : // environment-variable-name parameter
2831 1 : CATCH_REQUIRE(opt.get_option("path-to-option-definitions") != nullptr);
2832 1 : CATCH_REQUIRE_FALSE(opt.is_defined("path-to-option-definitions"));
2833 1 : CATCH_REQUIRE_FALSE(opt.has_default("path-to-option-definitions"));
2834 1 : CATCH_REQUIRE(opt.get_default("path-to-option-definitions").empty());
2835 1 : CATCH_REQUIRE(opt.size("path-to-option-definitions") == 0);
2836 :
2837 : // other parameters
2838 1 : CATCH_REQUIRE(opt.get_program_name() == "arguments");
2839 1 : CATCH_REQUIRE(opt.get_program_fullname() == "/usr/bin/arguments");
2840 :
2841 : // process system options now
2842 2 : std::stringstream ss;
2843 1 : advgetopt::flag_t const result(opt.process_system_options(ss));
2844 1 : CATCH_REQUIRE(result == advgetopt::SYSTEM_OPTION_NONE);
2845 1 : CATCH_REQUIRE(ss.str().empty());
2846 : CATCH_END_SECTION()
2847 3 : }
2848 :
2849 :
2850 :
2851 :
2852 :
2853 :
2854 :
2855 :
2856 6 : CATCH_TEST_CASE("invalid_option_name", "[arguments][invalid][getopt]")
2857 : {
2858 8 : CATCH_START_SECTION("Verify that asking for the string of a non-existant option fails")
2859 1 : advgetopt::options_environment environment_options;
2860 1 : environment_options.f_project_name = "unittest";
2861 1 : environment_options.f_options = nullptr;
2862 1 : environment_options.f_environment_flags = advgetopt::GETOPT_ENVIRONMENT_FLAG_SYSTEM_PARAMETERS;
2863 1 : environment_options.f_help_header = "Usage: test get_string() functions";
2864 :
2865 2 : advgetopt::getopt opt(environment_options);
2866 :
2867 1 : char const * cargv[] =
2868 : {
2869 : "tests/options-parser",
2870 : "--license",
2871 : nullptr
2872 : };
2873 1 : int const argc(sizeof(cargv) / sizeof(cargv[0]) - 1);
2874 1 : char ** argv = const_cast<char **>(cargv);
2875 :
2876 1 : opt.finish_parsing(argc, argv);
2877 :
2878 1 : CATCH_REQUIRE_THROWS_MATCHES(
2879 : opt.get_string("non-existant")
2880 : , advgetopt::getopt_logic_error
2881 : , Catch::Matchers::ExceptionMessage(
2882 : "getopt_logic_error: there is no --non-existant option defined."));
2883 :
2884 1 : CATCH_REQUIRE_THROWS_MATCHES(
2885 : opt.get_string("non-existant", 0)
2886 : , advgetopt::getopt_logic_error
2887 : , Catch::Matchers::ExceptionMessage(
2888 : "getopt_logic_error: there is no --non-existant option defined."));
2889 :
2890 1 : CATCH_REQUIRE_THROWS_MATCHES(
2891 : opt.get_string("non-existant", 1)
2892 : , advgetopt::getopt_logic_error
2893 : , Catch::Matchers::ExceptionMessage(
2894 : "getopt_logic_error: there is no --non-existant option defined."));
2895 : CATCH_END_SECTION()
2896 :
2897 8 : CATCH_START_SECTION("Verify that asking for the long of a non-existant option fails")
2898 1 : advgetopt::options_environment environment_options;
2899 1 : environment_options.f_project_name = "unittest";
2900 1 : environment_options.f_options = nullptr;
2901 1 : environment_options.f_environment_flags = advgetopt::GETOPT_ENVIRONMENT_FLAG_SYSTEM_PARAMETERS;
2902 1 : environment_options.f_help_header = "Usage: test get_string() functions";
2903 :
2904 2 : advgetopt::getopt opt(environment_options);
2905 :
2906 1 : char const * cargv[] =
2907 : {
2908 : "tests/options-parser",
2909 : "--license",
2910 : nullptr
2911 : };
2912 1 : int const argc(sizeof(cargv) / sizeof(cargv[0]) - 1);
2913 1 : char ** argv = const_cast<char **>(cargv);
2914 :
2915 1 : opt.finish_parsing(argc, argv);
2916 :
2917 1 : CATCH_REQUIRE_THROWS_MATCHES(
2918 : opt.get_long("non-existant")
2919 : , advgetopt::getopt_logic_error
2920 : , Catch::Matchers::ExceptionMessage(
2921 : "getopt_logic_error: there is no --non-existant option defined."));
2922 :
2923 1 : CATCH_REQUIRE_THROWS_MATCHES(
2924 : opt.get_long("non-existant", 0)
2925 : , advgetopt::getopt_logic_error
2926 : , Catch::Matchers::ExceptionMessage(
2927 : "getopt_logic_error: there is no --non-existant option defined."));
2928 :
2929 1 : CATCH_REQUIRE_THROWS_MATCHES(
2930 : opt.get_long("non-existant", 1)
2931 : , advgetopt::getopt_logic_error
2932 : , Catch::Matchers::ExceptionMessage(
2933 : "getopt_logic_error: there is no --non-existant option defined."));
2934 : CATCH_END_SECTION()
2935 :
2936 8 : CATCH_START_SECTION("Verify that asking for a default with an empty string fails")
2937 1 : advgetopt::options_environment environment_options;
2938 1 : environment_options.f_project_name = "unittest";
2939 1 : environment_options.f_options = nullptr;
2940 1 : environment_options.f_help_header = "Usage: test get_default() functions";
2941 :
2942 2 : advgetopt::getopt opt(environment_options);
2943 :
2944 1 : CATCH_REQUIRE_THROWS_MATCHES(
2945 : opt.has_default("")
2946 : , advgetopt::getopt_logic_error
2947 : , Catch::Matchers::ExceptionMessage(
2948 : "getopt_logic_error: argument name cannot be empty."));
2949 :
2950 1 : CATCH_REQUIRE_THROWS_MATCHES(
2951 : opt.has_default(std::string())
2952 : , advgetopt::getopt_logic_error
2953 : , Catch::Matchers::ExceptionMessage(
2954 : "getopt_logic_error: argument name cannot be empty."));
2955 :
2956 1 : CATCH_REQUIRE_THROWS_MATCHES(
2957 : opt.get_default("")
2958 : , advgetopt::getopt_logic_error
2959 : , Catch::Matchers::ExceptionMessage(
2960 : "getopt_logic_error: argument name cannot be empty."));
2961 :
2962 1 : CATCH_REQUIRE_THROWS_MATCHES(
2963 : opt.get_default(std::string())
2964 : , advgetopt::getopt_logic_error
2965 : , Catch::Matchers::ExceptionMessage(
2966 : "getopt_logic_error: argument name cannot be empty."));
2967 : CATCH_END_SECTION()
2968 :
2969 8 : CATCH_START_SECTION("[] operators want a valid name")
2970 1 : advgetopt::options_environment environment_options;
2971 1 : environment_options.f_project_name = "unittest";
2972 1 : environment_options.f_options = nullptr;
2973 1 : environment_options.f_environment_flags = advgetopt::GETOPT_ENVIRONMENT_FLAG_SYSTEM_PARAMETERS;
2974 1 : environment_options.f_help_header = "Usage: test get_default() functions";
2975 :
2976 2 : advgetopt::getopt opt(environment_options);
2977 :
2978 1 : char const * cargv[] =
2979 : {
2980 : "tests/options-parser",
2981 : "--license",
2982 : nullptr
2983 : };
2984 1 : int const argc(sizeof(cargv) / sizeof(cargv[0]) - 1);
2985 1 : char ** argv = const_cast<char **>(cargv);
2986 :
2987 1 : opt.finish_parsing(argc, argv);
2988 :
2989 1 : CATCH_REQUIRE_THROWS_MATCHES(
2990 : opt[""]
2991 : , advgetopt::getopt_logic_error
2992 : , Catch::Matchers::ExceptionMessage(
2993 : "getopt_logic_error: argument name cannot be empty."));
2994 :
2995 1 : CATCH_REQUIRE_THROWS_MATCHES(
2996 : opt[std::string()]
2997 : , advgetopt::getopt_logic_error
2998 : , Catch::Matchers::ExceptionMessage(
2999 : "getopt_logic_error: argument name cannot be empty."));
3000 :
3001 1 : CATCH_REQUIRE_THROWS_MATCHES(
3002 : opt["g"]
3003 : , advgetopt::getopt_logic_error
3004 : , Catch::Matchers::ExceptionMessage(
3005 : "getopt_logic_error: argument name cannot be one letter if it does not exist in operator []."));
3006 :
3007 1 : CATCH_REQUIRE_THROWS_MATCHES(
3008 : opt[std::string("g")]
3009 : , advgetopt::getopt_logic_error
3010 : , Catch::Matchers::ExceptionMessage(
3011 : "getopt_logic_error: argument name cannot be one letter if it does not exist in operator []."));
3012 :
3013 1 : advgetopt::getopt const & const_opt(opt);
3014 :
3015 1 : CATCH_REQUIRE_THROWS_MATCHES(
3016 : const_opt[""]
3017 : , advgetopt::getopt_logic_error
3018 : , Catch::Matchers::ExceptionMessage(
3019 : "getopt_logic_error: argument name cannot be empty."));
3020 :
3021 1 : CATCH_REQUIRE_THROWS_MATCHES(
3022 : const_opt[std::string()]
3023 : , advgetopt::getopt_logic_error
3024 : , Catch::Matchers::ExceptionMessage(
3025 : "getopt_logic_error: argument name cannot be empty."));
3026 : CATCH_END_SECTION()
3027 4 : }
3028 :
3029 :
3030 :
3031 5 : CATCH_TEST_CASE("missing_default_value", "[arguments][invalid][getopt]")
3032 : {
3033 6 : CATCH_START_SECTION("Verify a string value without arguments and no default")
3034 : {
3035 1 : advgetopt::option const options[] =
3036 : {
3037 : advgetopt::define_option(
3038 : advgetopt::Name("size")
3039 : , advgetopt::ShortName('s')
3040 : , advgetopt::Flags(advgetopt::command_flags<
3041 : advgetopt::GETOPT_FLAG_REQUIRED
3042 : , advgetopt::GETOPT_FLAG_DYNAMIC_CONFIGURATION>())
3043 : , advgetopt::Help("define the size.")
3044 : ),
3045 : advgetopt::end_options()
3046 : };
3047 :
3048 1 : advgetopt::options_environment environment_options;
3049 1 : environment_options.f_project_name = "unittest";
3050 1 : environment_options.f_options = options;
3051 1 : environment_options.f_help_header = "Usage: test get_string() functions";
3052 :
3053 1 : char const * cargv[] =
3054 : {
3055 : "/usr/bin/arguments",
3056 : nullptr
3057 : };
3058 1 : int const argc(sizeof(cargv) / sizeof(cargv[0]) - 1);
3059 1 : char ** argv = const_cast<char **>(cargv);
3060 :
3061 2 : advgetopt::getopt opt(environment_options, argc, argv);
3062 :
3063 : // check that the result is valid
3064 :
3065 : // an invalid parameter, MUST NOT EXIST
3066 1 : CATCH_REQUIRE(opt.get_option("invalid-parameter") == nullptr);
3067 1 : CATCH_REQUIRE(opt.get_option('Z') == nullptr);
3068 1 : CATCH_REQUIRE_FALSE(opt.is_defined("invalid-parameter"));
3069 1 : CATCH_REQUIRE(opt.get_default("invalid-parameter").empty());
3070 1 : CATCH_REQUIRE(opt.size("invalid-parameter") == 0);
3071 :
3072 : // no default
3073 1 : CATCH_REQUIRE(opt.get_option("--") == nullptr);
3074 1 : CATCH_REQUIRE_FALSE(opt.is_defined("--"));
3075 1 : CATCH_REQUIRE(opt.get_default("--").empty());
3076 1 : CATCH_REQUIRE(opt.size("--") == 0);
3077 :
3078 : // the valid parameter
3079 1 : CATCH_REQUIRE(opt.get_option("size") != nullptr);
3080 1 : CATCH_REQUIRE(opt.get_option('s') != nullptr);
3081 1 : CATCH_REQUIRE_FALSE(opt.is_defined("size"));
3082 1 : CATCH_REQUIRE(opt.size("size") == 0);
3083 1 : CATCH_REQUIRE(static_cast<advgetopt::getopt const &>(opt)["size"].empty());
3084 :
3085 1 : CATCH_REQUIRE_THROWS_MATCHES(
3086 : opt.get_string("size")
3087 : , advgetopt::getopt_logic_error
3088 : , Catch::Matchers::ExceptionMessage(
3089 : "getopt_logic_error: the --size option was not defined on the command line and it has no default."));
3090 :
3091 1 : CATCH_REQUIRE_THROWS_MATCHES(
3092 : opt.get_string("size", 0)
3093 : , advgetopt::getopt_logic_error
3094 : , Catch::Matchers::ExceptionMessage(
3095 : "getopt_logic_error: the --size option was not defined on the command line and it has no default."));
3096 :
3097 1 : CATCH_REQUIRE_THROWS_MATCHES(
3098 : opt.get_string("size", 1)
3099 : , advgetopt::getopt_logic_error
3100 : , Catch::Matchers::ExceptionMessage(
3101 : "getopt_logic_error: the --size option was not defined on the command line and it has no default."));
3102 :
3103 : // these do not create an entry (even though it looks like it,
3104 : // i.e. it would for an std::map)
3105 : //
3106 1 : CATCH_REQUIRE(opt["size"].empty());
3107 1 : CATCH_REQUIRE(opt["size"].length() == 0);
3108 1 : CATCH_REQUIRE(opt["size"].size() == 0);
3109 :
3110 1 : CATCH_REQUIRE(opt.size("size") == 0);
3111 :
3112 1 : CATCH_REQUIRE_THROWS_MATCHES(
3113 : opt.get_string("size", 0)
3114 : , advgetopt::getopt_logic_error
3115 : , Catch::Matchers::ExceptionMessage(
3116 : "getopt_logic_error: the --size option was not defined on the command line and it has no default."));
3117 :
3118 1 : CATCH_REQUIRE_THROWS_MATCHES(
3119 : opt.get_string("size", 1)
3120 : , advgetopt::getopt_logic_error
3121 : , Catch::Matchers::ExceptionMessage(
3122 : "getopt_logic_error: the --size option was not defined on the command line and it has no default."));
3123 :
3124 : // now this one does create a value
3125 : //
3126 1 : opt["size"] = "45.3";
3127 :
3128 1 : CATCH_REQUIRE(opt.get_string("size") == "45.3");
3129 1 : CATCH_REQUIRE(opt.get_string("size", 0) == "45.3");
3130 :
3131 1 : CATCH_REQUIRE_THROWS_MATCHES(
3132 : opt.get_string("size", 1)
3133 : , advgetopt::getopt_undefined
3134 : , Catch::Matchers::ExceptionMessage(
3135 : "getopt_exception: option_info::get_value(): no value at index 1 (idx >= 1) for --size so you can't get this value."));
3136 :
3137 : // other parameters
3138 1 : CATCH_REQUIRE(opt.get_program_name() == "arguments");
3139 1 : CATCH_REQUIRE(opt.get_program_fullname() == "/usr/bin/arguments");
3140 : }
3141 : CATCH_END_SECTION()
3142 :
3143 6 : CATCH_START_SECTION("Verify an integer (long) value without arguments and no default")
3144 : {
3145 1 : advgetopt::option const options[] =
3146 : {
3147 : advgetopt::define_option(
3148 : advgetopt::Name("size")
3149 : , advgetopt::ShortName('s')
3150 : , advgetopt::Flags(advgetopt::command_flags<
3151 : advgetopt::GETOPT_FLAG_REQUIRED
3152 : , advgetopt::GETOPT_FLAG_DYNAMIC_CONFIGURATION>())
3153 : , advgetopt::Help("define the size.")
3154 : ),
3155 : advgetopt::end_options()
3156 : };
3157 :
3158 1 : advgetopt::options_environment environment_options;
3159 1 : environment_options.f_project_name = "unittest";
3160 1 : environment_options.f_options = options;
3161 1 : environment_options.f_help_header = "Usage: test get_long() functions";
3162 :
3163 1 : char const * cargv[] =
3164 : {
3165 : "/usr/bin/arguments",
3166 : nullptr
3167 : };
3168 1 : int const argc(sizeof(cargv) / sizeof(cargv[0]) - 1);
3169 1 : char ** argv = const_cast<char **>(cargv);
3170 :
3171 2 : advgetopt::getopt opt(environment_options, argc, argv);
3172 :
3173 : // check that the result is valid
3174 :
3175 : // an invalid parameter, MUST NOT EXIST
3176 1 : CATCH_REQUIRE(opt.get_option("invalid-parameter") == nullptr);
3177 1 : CATCH_REQUIRE(opt.get_option('Z') == nullptr);
3178 1 : CATCH_REQUIRE_FALSE(opt.is_defined("invalid-parameter"));
3179 1 : CATCH_REQUIRE(opt.get_default("invalid-parameter").empty());
3180 1 : CATCH_REQUIRE(opt.size("invalid-parameter") == 0);
3181 :
3182 : // no default
3183 1 : CATCH_REQUIRE(opt.get_option("--") == nullptr);
3184 1 : CATCH_REQUIRE_FALSE(opt.is_defined("--"));
3185 1 : CATCH_REQUIRE(opt.get_default("--").empty());
3186 1 : CATCH_REQUIRE(opt.size("--") == 0);
3187 :
3188 : // the valid parameter
3189 1 : CATCH_REQUIRE(opt.get_option("size") != nullptr);
3190 1 : CATCH_REQUIRE(opt.get_option('s') != nullptr);
3191 1 : CATCH_REQUIRE_FALSE(opt.is_defined("size"));
3192 1 : CATCH_REQUIRE_FALSE(opt.has_default("size"));
3193 1 : CATCH_REQUIRE(opt.size("size") == 0);
3194 :
3195 1 : CATCH_REQUIRE_THROWS_MATCHES(
3196 : opt.get_long("size")
3197 : , advgetopt::getopt_logic_error
3198 : , Catch::Matchers::ExceptionMessage(
3199 : "getopt_logic_error: the --size option was not defined on the command line and it has no or an empty default."));
3200 :
3201 1 : CATCH_REQUIRE_THROWS_MATCHES(
3202 : opt.get_long("size", 0)
3203 : , advgetopt::getopt_logic_error
3204 : , Catch::Matchers::ExceptionMessage(
3205 : "getopt_logic_error: the --size option was not defined on the command line and it has no or an empty default."));
3206 :
3207 1 : CATCH_REQUIRE_THROWS_MATCHES(
3208 : opt.get_long("size", 1)
3209 : , advgetopt::getopt_logic_error
3210 : , Catch::Matchers::ExceptionMessage(
3211 : "getopt_logic_error: the --size option was not defined on the command line and it has no or an empty default."));
3212 :
3213 : // other parameters
3214 1 : CATCH_REQUIRE(opt.get_program_name() == "arguments");
3215 1 : CATCH_REQUIRE(opt.get_program_fullname() == "/usr/bin/arguments");
3216 : }
3217 : CATCH_END_SECTION()
3218 :
3219 6 : CATCH_START_SECTION("Verify an integer (long) value without arguments and an empty string as default")
3220 : {
3221 1 : advgetopt::option const options[] =
3222 : {
3223 : advgetopt::define_option(
3224 : advgetopt::Name("size")
3225 : , advgetopt::ShortName('s')
3226 : , advgetopt::Flags(advgetopt::command_flags<
3227 : advgetopt::GETOPT_FLAG_REQUIRED
3228 : , advgetopt::GETOPT_FLAG_DYNAMIC_CONFIGURATION>())
3229 : , advgetopt::Help("define the size.")
3230 : , advgetopt::DefaultValue("")
3231 : ),
3232 : advgetopt::end_options()
3233 : };
3234 :
3235 1 : advgetopt::options_environment environment_options;
3236 1 : environment_options.f_project_name = "unittest";
3237 1 : environment_options.f_options = options;
3238 1 : environment_options.f_help_header = "Usage: test get_long() functions";
3239 :
3240 1 : char const * cargv[] =
3241 : {
3242 : "/usr/bin/arguments",
3243 : nullptr
3244 : };
3245 1 : int const argc(sizeof(cargv) / sizeof(cargv[0]) - 1);
3246 1 : char ** argv = const_cast<char **>(cargv);
3247 :
3248 2 : advgetopt::getopt opt(environment_options, argc, argv);
3249 :
3250 : // check that the result is valid
3251 :
3252 : // an invalid parameter, MUST NOT EXIST
3253 1 : CATCH_REQUIRE(opt.get_option("invalid-parameter") == nullptr);
3254 1 : CATCH_REQUIRE(opt.get_option('Z') == nullptr);
3255 1 : CATCH_REQUIRE_FALSE(opt.is_defined("invalid-parameter"));
3256 1 : CATCH_REQUIRE(opt.get_default("invalid-parameter").empty());
3257 1 : CATCH_REQUIRE(opt.size("invalid-parameter") == 0);
3258 :
3259 : // no default
3260 1 : CATCH_REQUIRE(opt.get_option("--") == nullptr);
3261 1 : CATCH_REQUIRE_FALSE(opt.is_defined("--"));
3262 1 : CATCH_REQUIRE(opt.get_default("--").empty());
3263 1 : CATCH_REQUIRE(opt.size("--") == 0);
3264 :
3265 : // the valid parameter
3266 1 : CATCH_REQUIRE(opt.get_option("size") != nullptr);
3267 1 : CATCH_REQUIRE(opt.get_option('s') != nullptr);
3268 1 : CATCH_REQUIRE_FALSE(opt.is_defined("size"));
3269 1 : CATCH_REQUIRE(opt.has_default("size"));
3270 1 : CATCH_REQUIRE(opt.size("size") == 0);
3271 :
3272 1 : CATCH_REQUIRE_THROWS_MATCHES(
3273 : opt.get_long("size")
3274 : , advgetopt::getopt_logic_error
3275 : , Catch::Matchers::ExceptionMessage(
3276 : "getopt_logic_error: the --size option was not defined on the command line and it has no or an empty default."));
3277 :
3278 1 : CATCH_REQUIRE_THROWS_MATCHES(
3279 : opt.get_long("size", 0)
3280 : , advgetopt::getopt_logic_error
3281 : , Catch::Matchers::ExceptionMessage(
3282 : "getopt_logic_error: the --size option was not defined on the command line and it has no or an empty default."));
3283 :
3284 1 : CATCH_REQUIRE_THROWS_MATCHES(
3285 : opt.get_long("size", 1)
3286 : , advgetopt::getopt_logic_error
3287 : , Catch::Matchers::ExceptionMessage(
3288 : "getopt_logic_error: the --size option was not defined on the command line and it has no or an empty default."));
3289 :
3290 : // other parameters
3291 1 : CATCH_REQUIRE(opt.get_program_name() == "arguments");
3292 1 : CATCH_REQUIRE(opt.get_program_fullname() == "/usr/bin/arguments");
3293 : }
3294 : CATCH_END_SECTION()
3295 3 : }
3296 :
3297 :
3298 :
3299 3 : CATCH_TEST_CASE("incompatible_default_value", "[arguments][invalid][getopt]")
3300 : {
3301 2 : CATCH_START_SECTION("Verify an integer (long) value without arguments and a non-numeric default")
3302 1 : advgetopt::option const options[] =
3303 : {
3304 : advgetopt::define_option(
3305 : advgetopt::Name("size")
3306 : , advgetopt::ShortName('s')
3307 : , advgetopt::Flags(advgetopt::command_flags<advgetopt::GETOPT_FLAG_REQUIRED>())
3308 : , advgetopt::Help("define the size.")
3309 : , advgetopt::DefaultValue("undefined")
3310 : ),
3311 : advgetopt::end_options()
3312 : };
3313 :
3314 1 : advgetopt::options_environment environment_options;
3315 1 : environment_options.f_project_name = "unittest";
3316 1 : environment_options.f_options = options;
3317 1 : environment_options.f_help_header = "Usage: test get_long() functions";
3318 :
3319 1 : char const * cargv[] =
3320 : {
3321 : "/usr/bin/arguments",
3322 : nullptr
3323 : };
3324 1 : int const argc(sizeof(cargv) / sizeof(cargv[0]) - 1);
3325 1 : char ** argv = const_cast<char **>(cargv);
3326 :
3327 2 : advgetopt::getopt opt(environment_options, argc, argv);
3328 :
3329 : // check that the result is valid
3330 :
3331 : // an invalid parameter, MUST NOT EXIST
3332 1 : CATCH_REQUIRE(opt.get_option("invalid-parameter") == nullptr);
3333 1 : CATCH_REQUIRE(opt.get_option('Z') == nullptr);
3334 1 : CATCH_REQUIRE_FALSE(opt.is_defined("invalid-parameter"));
3335 1 : CATCH_REQUIRE(opt.get_default("invalid-parameter").empty());
3336 1 : CATCH_REQUIRE(opt.size("invalid-parameter") == 0);
3337 :
3338 : // no default
3339 1 : CATCH_REQUIRE(opt.get_option("--") == nullptr);
3340 1 : CATCH_REQUIRE_FALSE(opt.is_defined("--"));
3341 1 : CATCH_REQUIRE(opt.get_default("--").empty());
3342 1 : CATCH_REQUIRE(opt.size("--") == 0);
3343 :
3344 : // the valid parameter
3345 1 : CATCH_REQUIRE(opt.get_option("size") != nullptr);
3346 1 : CATCH_REQUIRE(opt.get_option('s') != nullptr);
3347 1 : CATCH_REQUIRE_FALSE(opt.is_defined("size"));
3348 1 : CATCH_REQUIRE(opt.has_default("size"));
3349 1 : CATCH_REQUIRE(opt.get_default("size") == "undefined"); // this works, it fails with get_long() though
3350 1 : CATCH_REQUIRE(opt.size("size") == 0);
3351 :
3352 1 : CATCH_REQUIRE_THROWS_MATCHES(
3353 : opt.get_long("size")
3354 : , advgetopt::getopt_logic_error
3355 : , Catch::Matchers::ExceptionMessage(
3356 : "getopt_logic_error: invalid default number \"undefined\" for option --size"));
3357 :
3358 1 : CATCH_REQUIRE_THROWS_MATCHES(
3359 : opt.get_long("size", 0)
3360 : , advgetopt::getopt_logic_error
3361 : , Catch::Matchers::ExceptionMessage(
3362 : "getopt_logic_error: invalid default number \"undefined\" for option --size"));
3363 :
3364 1 : CATCH_REQUIRE_THROWS_MATCHES(
3365 : opt.get_long("size", 1)
3366 : , advgetopt::getopt_logic_error
3367 : , Catch::Matchers::ExceptionMessage(
3368 : "getopt_logic_error: invalid default number \"undefined\" for option --size"));
3369 :
3370 : // other parameters
3371 1 : CATCH_REQUIRE(opt.get_program_name() == "arguments");
3372 1 : CATCH_REQUIRE(opt.get_program_fullname() == "/usr/bin/arguments");
3373 : CATCH_END_SECTION()
3374 1 : }
3375 :
3376 :
3377 :
3378 4 : CATCH_TEST_CASE("out_of_range_value", "[arguments][invalid][getopt]")
3379 : {
3380 4 : CATCH_START_SECTION("Verify an integer (long) value without arguments and a non-numeric default")
3381 1 : advgetopt::option const options[] =
3382 : {
3383 : advgetopt::define_option(
3384 : advgetopt::Name("size")
3385 : , advgetopt::ShortName('s')
3386 : , advgetopt::Flags(advgetopt::command_flags<advgetopt::GETOPT_FLAG_REQUIRED>())
3387 : , advgetopt::Help("define the size.")
3388 : , advgetopt::DefaultValue("-300")
3389 : ),
3390 : advgetopt::end_options()
3391 : };
3392 :
3393 1 : advgetopt::options_environment environment_options;
3394 1 : environment_options.f_project_name = "unittest";
3395 1 : environment_options.f_options = options;
3396 1 : environment_options.f_help_header = "Usage: test get_long() functions";
3397 :
3398 1 : char const * cargv[] =
3399 : {
3400 : "/usr/bin/arguments",
3401 : "--size",
3402 : "312",
3403 : nullptr
3404 : };
3405 1 : int const argc(sizeof(cargv) / sizeof(cargv[0]) - 1);
3406 1 : char ** argv = const_cast<char **>(cargv);
3407 :
3408 2 : advgetopt::getopt opt(environment_options, argc, argv);
3409 :
3410 : // check that the result is valid
3411 :
3412 : // an invalid parameter, MUST NOT EXIST
3413 1 : CATCH_REQUIRE(opt.get_option("invalid-parameter") == nullptr);
3414 1 : CATCH_REQUIRE(opt.get_option('Z') == nullptr);
3415 1 : CATCH_REQUIRE_FALSE(opt.is_defined("invalid-parameter"));
3416 1 : CATCH_REQUIRE(opt.get_default("invalid-parameter").empty());
3417 1 : CATCH_REQUIRE(opt.size("invalid-parameter") == 0);
3418 :
3419 : // no default
3420 1 : CATCH_REQUIRE(opt.get_option("--") == nullptr);
3421 1 : CATCH_REQUIRE_FALSE(opt.is_defined("--"));
3422 1 : CATCH_REQUIRE(opt.get_default("--").empty());
3423 1 : CATCH_REQUIRE(opt.size("--") == 0);
3424 :
3425 : // the valid parameter
3426 1 : CATCH_REQUIRE(opt.get_option("size") != nullptr);
3427 1 : CATCH_REQUIRE(opt.get_option('s') != nullptr);
3428 1 : CATCH_REQUIRE(opt.is_defined("size"));
3429 1 : CATCH_REQUIRE(opt.get_string("size") == "312");
3430 1 : CATCH_REQUIRE(opt.get_string("size", 0) == "312");
3431 1 : CATCH_REQUIRE(opt["size"] == "312");
3432 1 : CATCH_REQUIRE(opt.get_long("size") == 312);
3433 1 : CATCH_REQUIRE(opt.get_long("size", 0) == 312);
3434 1 : CATCH_REQUIRE(opt.get_default("size") == "-300");
3435 1 : CATCH_REQUIRE(opt.size("size") == 1);
3436 :
3437 1 : SNAP_CATCH2_NAMESPACE::push_expected_log("error: 312 is out of bounds (-100..100 inclusive) in parameter --size.");
3438 1 : CATCH_REQUIRE(opt.get_long("size", 0, -100, 100) == -1);
3439 1 : SNAP_CATCH2_NAMESPACE::expected_logs_stack_is_empty();
3440 :
3441 : // other parameters
3442 1 : CATCH_REQUIRE(opt.get_program_name() == "arguments");
3443 1 : CATCH_REQUIRE(opt.get_program_fullname() == "/usr/bin/arguments");
3444 : CATCH_END_SECTION()
3445 :
3446 4 : CATCH_START_SECTION("Verify an integer (long) value without arguments and a non-numeric default")
3447 1 : advgetopt::option const options[] =
3448 : {
3449 : advgetopt::define_option(
3450 : advgetopt::Name("size")
3451 : , advgetopt::ShortName('s')
3452 : , advgetopt::Flags(advgetopt::command_flags<advgetopt::GETOPT_FLAG_REQUIRED>())
3453 : , advgetopt::Help("define the size.")
3454 : , advgetopt::DefaultValue("-300")
3455 : ),
3456 : advgetopt::end_options()
3457 : };
3458 :
3459 1 : advgetopt::options_environment environment_options;
3460 1 : environment_options.f_project_name = "unittest";
3461 1 : environment_options.f_options = options;
3462 1 : environment_options.f_help_header = "Usage: test get_long() functions";
3463 :
3464 1 : char const * cargv[] =
3465 : {
3466 : "/usr/bin/arguments",
3467 : nullptr
3468 : };
3469 1 : int const argc(sizeof(cargv) / sizeof(cargv[0]) - 1);
3470 1 : char ** argv = const_cast<char **>(cargv);
3471 :
3472 2 : advgetopt::getopt opt(environment_options, argc, argv);
3473 :
3474 : // check that the result is valid
3475 :
3476 : // an invalid parameter, MUST NOT EXIST
3477 1 : CATCH_REQUIRE(opt.get_option("invalid-parameter") == nullptr);
3478 1 : CATCH_REQUIRE(opt.get_option('Z') == nullptr);
3479 1 : CATCH_REQUIRE_FALSE(opt.is_defined("invalid-parameter"));
3480 1 : CATCH_REQUIRE(opt.get_default("invalid-parameter").empty());
3481 1 : CATCH_REQUIRE(opt.size("invalid-parameter") == 0);
3482 :
3483 : // no default
3484 1 : CATCH_REQUIRE(opt.get_option("--") == nullptr);
3485 1 : CATCH_REQUIRE_FALSE(opt.is_defined("--"));
3486 1 : CATCH_REQUIRE(opt.get_default("--").empty());
3487 1 : CATCH_REQUIRE(opt.size("--") == 0);
3488 :
3489 : // the valid parameter
3490 1 : CATCH_REQUIRE(opt.get_option("size") != nullptr);
3491 1 : CATCH_REQUIRE(opt.get_option('s') != nullptr);
3492 1 : CATCH_REQUIRE_FALSE(opt.is_defined("size"));
3493 1 : CATCH_REQUIRE(opt.get_default("size") == "-300");
3494 1 : CATCH_REQUIRE(opt.size("size") == 0);
3495 :
3496 1 : SNAP_CATCH2_NAMESPACE::push_expected_log("error: -300 is out of bounds (-100..100 inclusive) in parameter --size.");
3497 1 : CATCH_REQUIRE(opt.get_long("size", 0, -100, 100) == -1);
3498 1 : SNAP_CATCH2_NAMESPACE::expected_logs_stack_is_empty();
3499 :
3500 : // other parameters
3501 1 : CATCH_REQUIRE(opt.get_program_name() == "arguments");
3502 1 : CATCH_REQUIRE(opt.get_program_fullname() == "/usr/bin/arguments");
3503 : CATCH_END_SECTION()
3504 8 : }
3505 :
3506 :
3507 : // vim: ts=4 sw=4 et
|