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