Line data Source code
1 : /*
2 : * License:
3 : * Copyright (c) 2006-2019 Made to Order Software Corp. All Rights Reserved
4 : *
5 : * https://snapwebsites.org/
6 : * contact@m2osw.com
7 : *
8 : * This program is free software; you can redistribute it and/or modify
9 : * it under the terms of the GNU General Public License as published by
10 : * the Free Software Foundation; either version 2 of the License, or
11 : * (at your option) any later version.
12 : *
13 : * This program is distributed in the hope that it will be useful,
14 : * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 : * GNU General Public License for more details.
17 : *
18 : * You should have received a copy of the GNU General Public License along
19 : * with this program; if not, write to the Free Software Foundation, Inc.,
20 : * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 : *
22 : * Authors:
23 : * Alexis Wilke alexis@m2osw.com
24 : */
25 :
26 : // self
27 : //
28 : #include "main.h"
29 :
30 :
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 : 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 2 : ),
196 : advgetopt::end_options()
197 3 : };
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 2 : std::string const version(std::to_string(major_version)
397 2 : + "."
398 4 : + std::to_string(minor_version)
399 2 : + "."
400 4 : + std::to_string(patch_version)
401 2 : + "."
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 : 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 2 : ),
415 : advgetopt::end_options()
416 3 : };
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 2 : std::string const version(std::to_string(major_version)
492 2 : + "."
493 4 : + std::to_string(minor_version)
494 2 : + "."
495 4 : + std::to_string(patch_version)
496 2 : + "."
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 : 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 2 : ),
510 : advgetopt::end_options()
511 3 : };
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 + 8 + 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"
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 : "--size or -s <arg> (default is \"33\")"
746 : , "define the size."
747 : , 30
748 : , advgetopt::getopt::get_line_width())
749 : + advgetopt::getopt::format_usage_string(
750 : "--version or -V"
751 : , "print out the version of arguments and exit."
752 : , 30
753 : , advgetopt::getopt::get_line_width())
754 : + "\n"
755 : "Copyright matters\n"
756 : "\n"
757 : );
758 : }
759 : CATCH_END_SECTION()
760 :
761 10 : CATCH_START_SECTION("Check with the --long-help system flag")
762 : {
763 1 : advgetopt::option const options[] =
764 : {
765 : advgetopt::define_option(
766 : advgetopt::Name("size")
767 : , advgetopt::ShortName('s')
768 : , advgetopt::Flags(advgetopt::command_flags<advgetopt::GETOPT_FLAG_REQUIRED>())
769 : , advgetopt::Help("define the size.")
770 : , advgetopt::DefaultValue("33")
771 : ),
772 : advgetopt::define_option(
773 : advgetopt::Name("obscure")
774 : , advgetopt::ShortName('o')
775 : , advgetopt::Flags(advgetopt::command_flags<advgetopt::GETOPT_FLAG_REQUIRED
776 : , advgetopt::GETOPT_FLAG_SHOW_GROUP1>())
777 : , advgetopt::Help("obscure command, hidden by default.")
778 : ),
779 : advgetopt::define_option(
780 : advgetopt::Name("secret")
781 : , advgetopt::ShortName('S')
782 : , advgetopt::Flags(advgetopt::command_flags<advgetopt::GETOPT_FLAG_REQUIRED
783 : , advgetopt::GETOPT_FLAG_SHOW_GROUP2>())
784 : , advgetopt::Help("even more secret command, hidden by default.")
785 : ),
786 : advgetopt::end_options()
787 : };
788 :
789 1 : advgetopt::options_environment environment_options;
790 1 : environment_options.f_project_name = "unittest";
791 1 : environment_options.f_options = options;
792 1 : environment_options.f_environment_flags = advgetopt::GETOPT_ENVIRONMENT_FLAG_SYSTEM_PARAMETERS;
793 1 : environment_options.f_help_header = "Usage: test system commands";
794 1 : environment_options.f_help_footer = "Copyright matters";
795 :
796 1 : char const * cargv[] =
797 : {
798 : "/usr/bin/arguments",
799 : "--long-help",
800 : nullptr
801 : };
802 1 : int const argc(sizeof(cargv) / sizeof(cargv[0]) - 1);
803 1 : char ** argv = const_cast<char **>(cargv);
804 :
805 2 : advgetopt::getopt opt(environment_options, argc, argv);
806 :
807 : // check that the result is valid
808 :
809 : // an invalid parameter, MUST NOT EXIST
810 1 : CATCH_REQUIRE(opt.get_option("invalid-parameter") == nullptr);
811 1 : CATCH_REQUIRE(opt.get_option('Z') == nullptr);
812 1 : CATCH_REQUIRE_FALSE(opt.is_defined("invalid-parameter"));
813 1 : CATCH_REQUIRE(opt.get_default("invalid-parameter").empty());
814 1 : CATCH_REQUIRE(opt.size("invalid-parameter") == 0);
815 :
816 : // no default
817 1 : CATCH_REQUIRE(opt.get_option("--") == nullptr);
818 1 : CATCH_REQUIRE_FALSE(opt.is_defined("--"));
819 1 : CATCH_REQUIRE(opt.get_default("--").empty());
820 1 : CATCH_REQUIRE(opt.size("--") == 0);
821 :
822 : // valid parameter
823 1 : CATCH_REQUIRE(opt.get_option("size") != nullptr);
824 1 : CATCH_REQUIRE(opt.get_option('s') == opt.get_option("size"));
825 1 : CATCH_REQUIRE_FALSE(opt.is_defined("size"));
826 1 : CATCH_REQUIRE(opt.get_string("size") == "33");
827 1 : CATCH_REQUIRE(opt.get_string("size", 0) == "33");
828 1 : CATCH_REQUIRE(opt["size"] == "33");
829 1 : CATCH_REQUIRE(opt.get_long("size") == 33);
830 1 : CATCH_REQUIRE(opt.get_long("size", 0) == 33);
831 1 : CATCH_REQUIRE(opt.has_default("size"));
832 1 : CATCH_REQUIRE(opt.get_default("size") == "33");
833 1 : CATCH_REQUIRE(opt.size("size") == 0);
834 :
835 : // help parameter
836 1 : CATCH_REQUIRE(opt.get_option("long-help") != nullptr);
837 1 : CATCH_REQUIRE(opt.is_defined("long-help"));
838 1 : CATCH_REQUIRE(opt.get_string("long-help") == "");
839 1 : CATCH_REQUIRE(opt.get_string("long-help", 0) == "");
840 1 : CATCH_REQUIRE(opt["long-help"] == "");
841 1 : CATCH_REQUIRE_FALSE(opt.has_default("long-help"));
842 1 : CATCH_REQUIRE(opt.get_default("long-help").empty());
843 1 : CATCH_REQUIRE(opt.size("long-help") == 1);
844 :
845 : // other parameters
846 1 : CATCH_REQUIRE(opt.get_program_name() == "arguments");
847 1 : CATCH_REQUIRE(opt.get_program_fullname() == "/usr/bin/arguments");
848 :
849 : // process system options now
850 2 : std::stringstream ss;
851 1 : advgetopt::flag_t const result(opt.process_system_options(ss));
852 1 : CATCH_REQUIRE(result == advgetopt::SYSTEM_OPTION_HELP);
853 1 : CATCH_REQUIRE_LONG_STRING(ss.str(),
854 : advgetopt::getopt::breakup_line(
855 : "Usage: test system commands"
856 : , 0
857 : , advgetopt::getopt::get_line_width())
858 : + advgetopt::getopt::format_usage_string(
859 : "--build-date"
860 : , "print out the time and date when arguments was built and exit."
861 : , 30
862 : , advgetopt::getopt::get_line_width())
863 : + advgetopt::getopt::format_usage_string(
864 : "--configuration-filenames"
865 : , "print out the list of configuration files checked out by this"
866 : " tool."
867 : , 30
868 : , advgetopt::getopt::get_line_width())
869 : + advgetopt::getopt::format_usage_string(
870 : "--copyright or -C"
871 : , "print out the copyright of arguments and exit."
872 : , 30
873 : , advgetopt::getopt::get_line_width())
874 : + advgetopt::getopt::format_usage_string(
875 : "--environment-variable-name"
876 : , "print out the name of the environment variable supported by"
877 : " arguments (if any.)"
878 : , 30
879 : , advgetopt::getopt::get_line_width())
880 : + advgetopt::getopt::format_usage_string(
881 : "--help or -h"
882 : , "print out this help screen and exit."
883 : , 30
884 : , advgetopt::getopt::get_line_width())
885 : + advgetopt::getopt::format_usage_string(
886 : "--license or -L"
887 : , "print out the license of arguments and exit."
888 : , 30
889 : , advgetopt::getopt::get_line_width())
890 : + advgetopt::getopt::format_usage_string(
891 : "--long-help"
892 : , "show all the help from all the available options."
893 : , 30
894 : , advgetopt::getopt::get_line_width())
895 : + advgetopt::getopt::format_usage_string(
896 : "--obscure or -o <arg>"
897 : , "obscure command, hidden by default."
898 : , 30
899 : , advgetopt::getopt::get_line_width())
900 : + advgetopt::getopt::format_usage_string(
901 : "--path-to-option-definitions"
902 : , "print out the path to the option definitons."
903 : , 30
904 : , advgetopt::getopt::get_line_width())
905 : + advgetopt::getopt::format_usage_string(
906 : "--secret or -S <arg>"
907 : , "even more secret command, hidden by default."
908 : , 30
909 : , advgetopt::getopt::get_line_width())
910 : + advgetopt::getopt::format_usage_string(
911 : "--size or -s <arg> (default is \"33\")"
912 : , "define the size."
913 : , 30
914 : , advgetopt::getopt::get_line_width())
915 : + advgetopt::getopt::format_usage_string(
916 : "--version or -V"
917 : , "print out the version of arguments and exit."
918 : , 30
919 : , advgetopt::getopt::get_line_width())
920 : + "\n"
921 : "Copyright matters\n"
922 : "\n"
923 : );
924 : }
925 : CATCH_END_SECTION()
926 :
927 10 : CATCH_START_SECTION("Check with the --help system flag, without a --help on the command line")
928 1 : advgetopt::option const options[] =
929 : {
930 : advgetopt::define_option(
931 : advgetopt::Name("size")
932 : , advgetopt::ShortName('s')
933 : , advgetopt::Flags(advgetopt::command_flags<advgetopt::GETOPT_FLAG_REQUIRED>())
934 : , advgetopt::Help("define the size.")
935 : , advgetopt::DefaultValue("33")
936 : ),
937 : advgetopt::end_options()
938 : };
939 :
940 1 : advgetopt::options_environment environment_options;
941 1 : environment_options.f_project_name = "unittest";
942 1 : environment_options.f_options = options;
943 1 : environment_options.f_environment_flags = advgetopt::GETOPT_ENVIRONMENT_FLAG_SYSTEM_PARAMETERS;
944 1 : environment_options.f_help_header = "Usage: test system commands";
945 1 : environment_options.f_help_footer = "Copyright matters";
946 :
947 1 : char const * cargv[] =
948 : {
949 : "/usr/bin/arguments",
950 : "--size",
951 : "1221",
952 : nullptr
953 : };
954 1 : int const argc(sizeof(cargv) / sizeof(cargv[0]) - 1);
955 1 : char ** argv = const_cast<char **>(cargv);
956 :
957 2 : advgetopt::getopt opt(environment_options, argc, argv);
958 :
959 : // check that the result is valid
960 :
961 : // an invalid parameter, MUST NOT EXIST
962 1 : CATCH_REQUIRE(opt.get_option("invalid-parameter") == nullptr);
963 1 : CATCH_REQUIRE(opt.get_option('Z') == nullptr);
964 1 : CATCH_REQUIRE_FALSE(opt.is_defined("invalid-parameter"));
965 1 : CATCH_REQUIRE(opt.get_default("invalid-parameter").empty());
966 1 : CATCH_REQUIRE(opt.size("invalid-parameter") == 0);
967 :
968 : // no default
969 1 : CATCH_REQUIRE(opt.get_option("--") == nullptr);
970 1 : CATCH_REQUIRE_FALSE(opt.is_defined("--"));
971 1 : CATCH_REQUIRE(opt.get_default("--").empty());
972 1 : CATCH_REQUIRE(opt.size("--") == 0);
973 :
974 : // valid parameter
975 1 : CATCH_REQUIRE(opt.get_option("size") != nullptr);
976 1 : CATCH_REQUIRE(opt.get_option('s') == opt.get_option("size"));
977 1 : CATCH_REQUIRE(opt.is_defined("size"));
978 1 : CATCH_REQUIRE(opt.get_string("size") == "1221");
979 1 : CATCH_REQUIRE(opt.get_string("size", 0) == "1221");
980 1 : CATCH_REQUIRE(opt["size"] == "1221");
981 1 : CATCH_REQUIRE(opt.get_long("size") == 1221);
982 1 : CATCH_REQUIRE(opt.get_long("size", 0) == 1221);
983 1 : CATCH_REQUIRE(opt.has_default("size"));
984 1 : CATCH_REQUIRE(opt.get_default("size") == "33");
985 1 : CATCH_REQUIRE(opt.size("size") == 1);
986 :
987 : // help parameter
988 1 : CATCH_REQUIRE(opt.get_option("help") != nullptr);
989 1 : CATCH_REQUIRE(opt.get_option('h') == opt.get_option("help"));
990 1 : CATCH_REQUIRE_FALSE(opt.is_defined("help"));
991 1 : CATCH_REQUIRE_FALSE(opt.has_default("help"));
992 1 : CATCH_REQUIRE(opt.get_default("help").empty());
993 1 : CATCH_REQUIRE(opt.size("help") == 0);
994 :
995 : // other parameters
996 1 : CATCH_REQUIRE(opt.get_program_name() == "arguments");
997 1 : CATCH_REQUIRE(opt.get_program_fullname() == "/usr/bin/arguments");
998 :
999 : // process system options now
1000 2 : std::stringstream ss;
1001 1 : advgetopt::flag_t const result(opt.process_system_options(ss));
1002 1 : CATCH_REQUIRE(result == advgetopt::SYSTEM_OPTION_NONE);
1003 1 : CATCH_REQUIRE(ss.str().empty());
1004 : CATCH_END_SECTION()
1005 :
1006 10 : CATCH_START_SECTION("Check with the --commmands-help system flag")
1007 : {
1008 1 : advgetopt::option const options[] =
1009 : {
1010 : advgetopt::define_option(
1011 : advgetopt::Name("size")
1012 : , advgetopt::ShortName('s')
1013 : , advgetopt::Flags(advgetopt::command_flags<advgetopt::GETOPT_FLAG_REQUIRED
1014 : , advgetopt::GETOPT_FLAG_GROUP_COMMANDS>())
1015 : , advgetopt::Help("define the size.")
1016 : , advgetopt::DefaultValue("33")
1017 : ),
1018 : advgetopt::define_option(
1019 : advgetopt::Name("obscure")
1020 : , advgetopt::ShortName('o')
1021 : , advgetopt::Flags(advgetopt::command_flags<advgetopt::GETOPT_FLAG_REQUIRED
1022 : , advgetopt::GETOPT_FLAG_GROUP_COMMANDS>())
1023 : , advgetopt::Help("obscure command, hidden by default.")
1024 : ),
1025 : advgetopt::define_option(
1026 : advgetopt::Name("secret")
1027 : , advgetopt::ShortName('S')
1028 : , advgetopt::Flags(advgetopt::command_flags<advgetopt::GETOPT_FLAG_REQUIRED
1029 : , advgetopt::GETOPT_FLAG_GROUP_OPTIONS>())
1030 : , advgetopt::Help("even more secret command, hidden by default.")
1031 : ),
1032 : advgetopt::end_options()
1033 : };
1034 :
1035 1 : advgetopt::group_description const groups[] =
1036 : {
1037 : advgetopt::define_group(
1038 : advgetopt::GroupNumber(advgetopt::GETOPT_FLAG_GROUP_COMMANDS)
1039 : , advgetopt::GroupName("commands")
1040 : , advgetopt::GroupDescription("Commands:")
1041 : ),
1042 : advgetopt::define_group(
1043 : advgetopt::GroupNumber(advgetopt::GETOPT_FLAG_GROUP_OPTIONS)
1044 : , advgetopt::GroupName("option")
1045 : , advgetopt::GroupDescription("Options:")
1046 : ),
1047 : advgetopt::end_groups()
1048 : };
1049 :
1050 1 : advgetopt::options_environment environment_options;
1051 1 : environment_options.f_project_name = "unittest";
1052 1 : environment_options.f_options = options;
1053 1 : environment_options.f_environment_flags = advgetopt::GETOPT_ENVIRONMENT_FLAG_SYSTEM_PARAMETERS;
1054 1 : environment_options.f_help_header = "Usage: test system commands";
1055 1 : environment_options.f_help_footer = "Copyright matters";
1056 1 : environment_options.f_groups = groups;
1057 :
1058 1 : char const * cargv[] =
1059 : {
1060 : "/usr/bin/arguments",
1061 : "--commands-help",
1062 : nullptr
1063 : };
1064 1 : int const argc(sizeof(cargv) / sizeof(cargv[0]) - 1);
1065 1 : char ** argv = const_cast<char **>(cargv);
1066 :
1067 2 : advgetopt::getopt opt(environment_options, argc, argv);
1068 :
1069 : // check that the result is valid
1070 :
1071 : // an invalid parameter, MUST NOT EXIST
1072 1 : CATCH_REQUIRE(opt.get_option("invalid-parameter") == nullptr);
1073 1 : CATCH_REQUIRE(opt.get_option('Z') == nullptr);
1074 1 : CATCH_REQUIRE_FALSE(opt.is_defined("invalid-parameter"));
1075 1 : CATCH_REQUIRE(opt.get_default("invalid-parameter").empty());
1076 1 : CATCH_REQUIRE(opt.size("invalid-parameter") == 0);
1077 :
1078 : // no default
1079 1 : CATCH_REQUIRE(opt.get_option("--") == nullptr);
1080 1 : CATCH_REQUIRE_FALSE(opt.is_defined("--"));
1081 1 : CATCH_REQUIRE(opt.get_default("--").empty());
1082 1 : CATCH_REQUIRE(opt.size("--") == 0);
1083 :
1084 : // valid parameter
1085 1 : CATCH_REQUIRE(opt.get_option("size") != nullptr);
1086 1 : CATCH_REQUIRE(opt.get_option('s') == opt.get_option("size"));
1087 1 : CATCH_REQUIRE_FALSE(opt.is_defined("size"));
1088 1 : CATCH_REQUIRE(opt.get_string("size") == "33");
1089 1 : CATCH_REQUIRE(opt.get_string("size", 0) == "33");
1090 1 : CATCH_REQUIRE(opt["size"] == "33");
1091 1 : CATCH_REQUIRE(opt.get_long("size") == 33);
1092 1 : CATCH_REQUIRE(opt.get_long("size", 0) == 33);
1093 1 : CATCH_REQUIRE(opt.has_default("size"));
1094 1 : CATCH_REQUIRE(opt.get_default("size") == "33");
1095 1 : CATCH_REQUIRE(opt.size("size") == 0);
1096 :
1097 : // help parameter
1098 1 : CATCH_REQUIRE(opt.get_option("commands-help") != nullptr);
1099 1 : CATCH_REQUIRE(opt.is_defined("commands-help"));
1100 1 : CATCH_REQUIRE(opt.get_string("commands-help") == "");
1101 1 : CATCH_REQUIRE(opt.get_string("commands-help", 0) == "");
1102 1 : CATCH_REQUIRE(opt["commands-help"] == "");
1103 1 : CATCH_REQUIRE_FALSE(opt.has_default("commands-help"));
1104 1 : CATCH_REQUIRE(opt.get_default("commands-help").empty());
1105 1 : CATCH_REQUIRE(opt.size("commands-help") == 1);
1106 :
1107 : // other parameters
1108 1 : CATCH_REQUIRE(opt.get_program_name() == "arguments");
1109 1 : CATCH_REQUIRE(opt.get_program_fullname() == "/usr/bin/arguments");
1110 :
1111 : // process system options now
1112 2 : std::stringstream ss;
1113 1 : advgetopt::flag_t const result(opt.process_system_options(ss));
1114 1 : CATCH_REQUIRE(result == advgetopt::SYSTEM_OPTION_HELP);
1115 1 : CATCH_REQUIRE_LONG_STRING(ss.str(),
1116 : advgetopt::getopt::breakup_line(
1117 : "Usage: test system commands"
1118 : , 0
1119 : , advgetopt::getopt::get_line_width())
1120 : + "\n"
1121 : "Commands:\n"
1122 : + advgetopt::getopt::format_usage_string(
1123 : "--build-date"
1124 : , "print out the time and date when arguments was built and exit."
1125 : , 30
1126 : , advgetopt::getopt::get_line_width())
1127 : + advgetopt::getopt::format_usage_string(
1128 : "--commands-help"
1129 : , "show help from the \"commands\" group of options."
1130 : , 30
1131 : , advgetopt::getopt::get_line_width())
1132 : + advgetopt::getopt::format_usage_string(
1133 : "--configuration-filenames"
1134 : , "print out the list of configuration files checked out by this"
1135 : " tool."
1136 : , 30
1137 : , advgetopt::getopt::get_line_width())
1138 : + advgetopt::getopt::format_usage_string(
1139 : "--copyright or -C"
1140 : , "print out the copyright of arguments and exit."
1141 : , 30
1142 : , advgetopt::getopt::get_line_width())
1143 : + advgetopt::getopt::format_usage_string(
1144 : "--environment-variable-name"
1145 : , "print out the name of the environment variable supported by"
1146 : " arguments (if any.)"
1147 : , 30
1148 : , advgetopt::getopt::get_line_width())
1149 : + advgetopt::getopt::format_usage_string(
1150 : "--help or -h"
1151 : , "print out this help screen and exit."
1152 : , 30
1153 : , advgetopt::getopt::get_line_width())
1154 : + advgetopt::getopt::format_usage_string(
1155 : "--license or -L"
1156 : , "print out the license of arguments and exit."
1157 : , 30
1158 : , advgetopt::getopt::get_line_width())
1159 : + advgetopt::getopt::format_usage_string(
1160 : "--obscure or -o <arg>"
1161 : , "obscure command, hidden by default."
1162 : , 30
1163 : , advgetopt::getopt::get_line_width())
1164 : + advgetopt::getopt::format_usage_string(
1165 : "--option-help"
1166 : , "show help from the \"option\" group of options."
1167 : , 30
1168 : , advgetopt::getopt::get_line_width())
1169 : + advgetopt::getopt::format_usage_string(
1170 : "--path-to-option-definitions"
1171 : , "print out the path to the option definitons."
1172 : , 30
1173 : , advgetopt::getopt::get_line_width())
1174 : + advgetopt::getopt::format_usage_string(
1175 : "--size or -s <arg> (default is \"33\")"
1176 : , "define the size."
1177 : , 30
1178 : , advgetopt::getopt::get_line_width())
1179 : + advgetopt::getopt::format_usage_string(
1180 : "--version or -V"
1181 : , "print out the version of arguments and exit."
1182 : , 30
1183 : , advgetopt::getopt::get_line_width())
1184 : + "\n"
1185 : "Copyright matters\n"
1186 : "\n"
1187 : );
1188 : }
1189 : CATCH_END_SECTION()
1190 :
1191 10 : CATCH_START_SECTION("Check with the --options-help system flag")
1192 : {
1193 1 : advgetopt::option const options[] =
1194 : {
1195 : advgetopt::define_option(
1196 : advgetopt::Name("size")
1197 : , advgetopt::ShortName('s')
1198 : , advgetopt::Flags(advgetopt::command_flags<advgetopt::GETOPT_FLAG_REQUIRED
1199 : , advgetopt::GETOPT_FLAG_GROUP_COMMANDS>())
1200 : , advgetopt::Help("define the size.")
1201 : , advgetopt::DefaultValue("33")
1202 : ),
1203 : advgetopt::define_option(
1204 : advgetopt::Name("obscure")
1205 : , advgetopt::ShortName('o')
1206 : , advgetopt::Flags(advgetopt::command_flags<advgetopt::GETOPT_FLAG_REQUIRED
1207 : , advgetopt::GETOPT_FLAG_GROUP_COMMANDS>())
1208 : , advgetopt::Help("obscure command, hidden by default.")
1209 : ),
1210 : advgetopt::define_option(
1211 : advgetopt::Name("secret")
1212 : , advgetopt::ShortName('S')
1213 : , advgetopt::Flags(advgetopt::command_flags<advgetopt::GETOPT_FLAG_REQUIRED
1214 : , advgetopt::GETOPT_FLAG_GROUP_OPTIONS>())
1215 : , advgetopt::Help("even more secret command, hidden by default.")
1216 : ),
1217 : advgetopt::end_options()
1218 : };
1219 :
1220 1 : advgetopt::group_description const groups[] =
1221 : {
1222 : advgetopt::define_group(
1223 : advgetopt::GroupNumber(advgetopt::GETOPT_FLAG_GROUP_COMMANDS)
1224 : , advgetopt::GroupName("commands")
1225 : , advgetopt::GroupDescription("Commands:")
1226 : ),
1227 : advgetopt::define_group(
1228 : advgetopt::GroupNumber(advgetopt::GETOPT_FLAG_GROUP_OPTIONS)
1229 : , advgetopt::GroupName("options")
1230 : , advgetopt::GroupDescription("Options:")
1231 : ),
1232 : advgetopt::end_groups()
1233 : };
1234 :
1235 1 : advgetopt::options_environment environment_options;
1236 1 : environment_options.f_project_name = "unittest";
1237 1 : environment_options.f_options = options;
1238 1 : environment_options.f_environment_flags = advgetopt::GETOPT_ENVIRONMENT_FLAG_SYSTEM_PARAMETERS;
1239 1 : environment_options.f_help_header = "Usage: test system commands";
1240 1 : environment_options.f_help_footer = "Copyright matters";
1241 1 : environment_options.f_groups = groups;
1242 :
1243 1 : char const * cargv[] =
1244 : {
1245 : "/usr/bin/arguments",
1246 : "--options-help",
1247 : nullptr
1248 : };
1249 1 : int const argc(sizeof(cargv) / sizeof(cargv[0]) - 1);
1250 1 : char ** argv = const_cast<char **>(cargv);
1251 :
1252 2 : advgetopt::getopt opt(environment_options, argc, argv);
1253 :
1254 : // check that the result is valid
1255 :
1256 : // an invalid parameter, MUST NOT EXIST
1257 1 : CATCH_REQUIRE(opt.get_option("invalid-parameter") == nullptr);
1258 1 : CATCH_REQUIRE(opt.get_option('Z') == nullptr);
1259 1 : CATCH_REQUIRE_FALSE(opt.is_defined("invalid-parameter"));
1260 1 : CATCH_REQUIRE(opt.get_default("invalid-parameter").empty());
1261 1 : CATCH_REQUIRE(opt.size("invalid-parameter") == 0);
1262 :
1263 : // no default
1264 1 : CATCH_REQUIRE(opt.get_option("--") == nullptr);
1265 1 : CATCH_REQUIRE_FALSE(opt.is_defined("--"));
1266 1 : CATCH_REQUIRE(opt.get_default("--").empty());
1267 1 : CATCH_REQUIRE(opt.size("--") == 0);
1268 :
1269 : // valid parameter
1270 1 : CATCH_REQUIRE(opt.get_option("size") != nullptr);
1271 1 : CATCH_REQUIRE(opt.get_option('s') == opt.get_option("size"));
1272 1 : CATCH_REQUIRE_FALSE(opt.is_defined("size"));
1273 1 : CATCH_REQUIRE(opt.get_string("size") == "33");
1274 1 : CATCH_REQUIRE(opt.get_string("size", 0) == "33");
1275 1 : CATCH_REQUIRE(opt["size"] == "33");
1276 1 : CATCH_REQUIRE(opt.get_long("size") == 33);
1277 1 : CATCH_REQUIRE(opt.get_long("size", 0) == 33);
1278 1 : CATCH_REQUIRE(opt.has_default("size"));
1279 1 : CATCH_REQUIRE(opt.get_default("size") == "33");
1280 1 : CATCH_REQUIRE(opt.size("size") == 0);
1281 :
1282 : // help parameter
1283 1 : CATCH_REQUIRE(opt.get_option("options-help") != nullptr);
1284 1 : CATCH_REQUIRE(opt.is_defined("options-help"));
1285 1 : CATCH_REQUIRE(opt.get_string("options-help") == "");
1286 1 : CATCH_REQUIRE(opt.get_string("options-help", 0) == "");
1287 1 : CATCH_REQUIRE(opt["options-help"] == "");
1288 1 : CATCH_REQUIRE_FALSE(opt.has_default("options-help"));
1289 1 : CATCH_REQUIRE(opt.get_default("options-help").empty());
1290 1 : CATCH_REQUIRE(opt.size("options-help") == 1);
1291 :
1292 : // other parameters
1293 1 : CATCH_REQUIRE(opt.get_program_name() == "arguments");
1294 1 : CATCH_REQUIRE(opt.get_program_fullname() == "/usr/bin/arguments");
1295 :
1296 : // process system options now
1297 2 : std::stringstream ss;
1298 1 : advgetopt::flag_t const result(opt.process_system_options(ss));
1299 1 : CATCH_REQUIRE(result == advgetopt::SYSTEM_OPTION_HELP);
1300 1 : CATCH_REQUIRE_LONG_STRING(ss.str(),
1301 : "Usage: test system commands\n"
1302 : "\n"
1303 : "Options:\n"
1304 : " --secret or -S <arg> even more secret command, hidden by default.\n"
1305 : "\n"
1306 : "Copyright matters\n"
1307 : "\n"
1308 : );
1309 : }
1310 : CATCH_END_SECTION()
1311 5 : }
1312 :
1313 :
1314 :
1315 4 : CATCH_TEST_CASE("system_flags_copyright", "[arguments][valid][getopt][system_flags]")
1316 : {
1317 4 : CATCH_START_SECTION("Check with the --copyright system flag")
1318 : {
1319 1 : advgetopt::option const options[] =
1320 : {
1321 : advgetopt::define_option(
1322 : advgetopt::Name("size")
1323 : , advgetopt::ShortName('s')
1324 : , advgetopt::Flags(advgetopt::command_flags<advgetopt::GETOPT_FLAG_REQUIRED>())
1325 : , advgetopt::Help("define the size.")
1326 : , advgetopt::DefaultValue("23")
1327 : ),
1328 : advgetopt::end_options()
1329 : };
1330 :
1331 1 : advgetopt::options_environment environment_options;
1332 1 : environment_options.f_project_name = "unittest";
1333 1 : environment_options.f_options = options;
1334 1 : environment_options.f_environment_flags = advgetopt::GETOPT_ENVIRONMENT_FLAG_SYSTEM_PARAMETERS;
1335 1 : environment_options.f_copyright = "Copyright (c) " BOOST_PP_STRINGIZE(UTC_BUILD_YEAR) " Made to Order Software Corporation";
1336 :
1337 1 : char const * cargv[] =
1338 : {
1339 : "/usr/bin/arguments",
1340 : "--copyright",
1341 : nullptr
1342 : };
1343 1 : int const argc(sizeof(cargv) / sizeof(cargv[0]) - 1);
1344 1 : char ** argv = const_cast<char **>(cargv);
1345 :
1346 2 : advgetopt::getopt opt(environment_options, argc, argv);
1347 :
1348 : // check that the result is valid
1349 :
1350 : // an invalid parameter, MUST NOT EXIST
1351 1 : CATCH_REQUIRE(opt.get_option("invalid-parameter") == nullptr);
1352 1 : CATCH_REQUIRE(opt.get_option('Z') == nullptr);
1353 1 : CATCH_REQUIRE_FALSE(opt.is_defined("invalid-parameter"));
1354 1 : CATCH_REQUIRE(opt.get_default("invalid-parameter").empty());
1355 1 : CATCH_REQUIRE(opt.size("invalid-parameter") == 0);
1356 :
1357 : // no default
1358 1 : CATCH_REQUIRE(opt.get_option("--") == nullptr);
1359 1 : CATCH_REQUIRE_FALSE(opt.is_defined("--"));
1360 1 : CATCH_REQUIRE(opt.get_default("--").empty());
1361 1 : CATCH_REQUIRE(opt.size("--") == 0);
1362 :
1363 : // valid parameter
1364 1 : CATCH_REQUIRE(opt.get_option("size") != nullptr);
1365 1 : CATCH_REQUIRE(opt.get_option('s') == opt.get_option("size"));
1366 1 : CATCH_REQUIRE_FALSE(opt.is_defined("size"));
1367 1 : CATCH_REQUIRE(opt.get_string("size") == "23");
1368 1 : CATCH_REQUIRE(opt.get_string("size", 0) == "23");
1369 1 : CATCH_REQUIRE(opt["size"] == "23");
1370 1 : CATCH_REQUIRE(opt.get_long("size") == 23);
1371 1 : CATCH_REQUIRE(opt.get_long("size", 0) == 23);
1372 1 : CATCH_REQUIRE(opt.has_default("size"));
1373 1 : CATCH_REQUIRE(opt.get_default("size") == "23");
1374 1 : CATCH_REQUIRE(opt.size("size") == 0);
1375 :
1376 : // copyright parameter
1377 1 : CATCH_REQUIRE(opt.get_option("copyright") != nullptr);
1378 1 : CATCH_REQUIRE(opt.get_option('C') == opt.get_option("copyright"));
1379 1 : CATCH_REQUIRE(opt.is_defined("copyright"));
1380 1 : CATCH_REQUIRE(opt.get_string("copyright") == "");
1381 1 : CATCH_REQUIRE(opt.get_string("copyright", 0) == "");
1382 1 : CATCH_REQUIRE(opt["copyright"] == "");
1383 1 : CATCH_REQUIRE_FALSE(opt.has_default("copyright"));
1384 1 : CATCH_REQUIRE(opt.get_default("copyright").empty());
1385 1 : CATCH_REQUIRE(opt.size("copyright") == 1);
1386 :
1387 : // other parameters
1388 1 : CATCH_REQUIRE(opt.get_program_name() == "arguments");
1389 1 : CATCH_REQUIRE(opt.get_program_fullname() == "/usr/bin/arguments");
1390 :
1391 : // process system options now
1392 2 : std::stringstream ss;
1393 1 : advgetopt::flag_t const result(opt.process_system_options(ss));
1394 1 : CATCH_REQUIRE(result == advgetopt::SYSTEM_OPTION_COPYRIGHT);
1395 1 : CATCH_REQUIRE(ss.str() == "Copyright (c) " BOOST_PP_STRINGIZE(UTC_BUILD_YEAR) " Made to Order Software Corporation\n");
1396 : }
1397 : CATCH_END_SECTION()
1398 :
1399 4 : CATCH_START_SECTION("Check with the --copyright system flag, without a --copyright on the command line")
1400 1 : advgetopt::option const options[] =
1401 : {
1402 : advgetopt::define_option(
1403 : advgetopt::Name("size")
1404 : , advgetopt::ShortName('s')
1405 : , advgetopt::Flags(advgetopt::command_flags<advgetopt::GETOPT_FLAG_REQUIRED>())
1406 : , advgetopt::Help("define the size.")
1407 : , advgetopt::DefaultValue("53")
1408 : ),
1409 : advgetopt::end_options()
1410 : };
1411 :
1412 1 : advgetopt::options_environment environment_options;
1413 1 : environment_options.f_project_name = "unittest";
1414 1 : environment_options.f_options = options;
1415 1 : environment_options.f_environment_flags = advgetopt::GETOPT_ENVIRONMENT_FLAG_SYSTEM_PARAMETERS;
1416 1 : environment_options.f_help_header = "Usage: test system commands";
1417 1 : environment_options.f_help_footer = "Copyright matters";
1418 :
1419 1 : char const * cargv[] =
1420 : {
1421 : "/usr/bin/arguments",
1422 : "--size",
1423 : "1221",
1424 : nullptr
1425 : };
1426 1 : int const argc(sizeof(cargv) / sizeof(cargv[0]) - 1);
1427 1 : char ** argv = const_cast<char **>(cargv);
1428 :
1429 2 : advgetopt::getopt opt(environment_options, argc, argv);
1430 :
1431 : // check that the result is valid
1432 :
1433 : // an invalid parameter, MUST NOT EXIST
1434 1 : CATCH_REQUIRE(opt.get_option("invalid-parameter") == nullptr);
1435 1 : CATCH_REQUIRE(opt.get_option('Z') == nullptr);
1436 1 : CATCH_REQUIRE_FALSE(opt.is_defined("invalid-parameter"));
1437 1 : CATCH_REQUIRE(opt.get_default("invalid-parameter").empty());
1438 1 : CATCH_REQUIRE(opt.size("invalid-parameter") == 0);
1439 :
1440 : // no default
1441 1 : CATCH_REQUIRE(opt.get_option("--") == nullptr);
1442 1 : CATCH_REQUIRE_FALSE(opt.is_defined("--"));
1443 1 : CATCH_REQUIRE(opt.get_default("--").empty());
1444 1 : CATCH_REQUIRE(opt.size("--") == 0);
1445 :
1446 : // valid parameter
1447 1 : CATCH_REQUIRE(opt.get_option("size") != nullptr);
1448 1 : CATCH_REQUIRE(opt.get_option('s') == opt.get_option("size"));
1449 1 : CATCH_REQUIRE(opt.is_defined("size"));
1450 1 : CATCH_REQUIRE(opt.get_string("size") == "1221");
1451 1 : CATCH_REQUIRE(opt.get_string("size", 0) == "1221");
1452 1 : CATCH_REQUIRE(opt["size"] == "1221");
1453 1 : CATCH_REQUIRE(opt.get_long("size") == 1221);
1454 1 : CATCH_REQUIRE(opt.get_long("size", 0) == 1221);
1455 1 : CATCH_REQUIRE(opt.has_default("size"));
1456 1 : CATCH_REQUIRE(opt.get_default("size") == "53");
1457 1 : CATCH_REQUIRE(opt.size("size") == 1);
1458 :
1459 : // copyright parameter
1460 1 : CATCH_REQUIRE(opt.get_option("copyright") != nullptr);
1461 1 : CATCH_REQUIRE(opt.get_option('C') == opt.get_option("copyright"));
1462 1 : CATCH_REQUIRE_FALSE(opt.is_defined("copyright"));
1463 1 : CATCH_REQUIRE_FALSE(opt.has_default("copyright"));
1464 1 : CATCH_REQUIRE(opt.get_default("copyright").empty());
1465 1 : CATCH_REQUIRE(opt.size("copyright") == 0);
1466 :
1467 : // other parameters
1468 1 : CATCH_REQUIRE(opt.get_program_name() == "arguments");
1469 1 : CATCH_REQUIRE(opt.get_program_fullname() == "/usr/bin/arguments");
1470 :
1471 : // process system options now
1472 2 : std::stringstream ss;
1473 1 : advgetopt::flag_t const result(opt.process_system_options(ss));
1474 1 : CATCH_REQUIRE(result == advgetopt::SYSTEM_OPTION_NONE);
1475 1 : CATCH_REQUIRE(ss.str().empty());
1476 : CATCH_END_SECTION()
1477 2 : }
1478 :
1479 :
1480 :
1481 4 : CATCH_TEST_CASE("system_flags_license", "[arguments][valid][getopt][system_flags]")
1482 : {
1483 4 : CATCH_START_SECTION("Check with the --license system flag")
1484 : {
1485 1 : advgetopt::option const options[] =
1486 : {
1487 : advgetopt::define_option(
1488 : advgetopt::Name("size")
1489 : , advgetopt::ShortName('s')
1490 : , advgetopt::Flags(advgetopt::command_flags<advgetopt::GETOPT_FLAG_REQUIRED>())
1491 : , advgetopt::Help("define the size.")
1492 : , advgetopt::DefaultValue("73")
1493 : ),
1494 : advgetopt::end_options()
1495 : };
1496 :
1497 1 : advgetopt::options_environment environment_options;
1498 1 : environment_options.f_project_name = "unittest";
1499 1 : environment_options.f_options = options;
1500 1 : environment_options.f_environment_flags = advgetopt::GETOPT_ENVIRONMENT_FLAG_SYSTEM_PARAMETERS;
1501 1 : environment_options.f_license = "GPL v2";
1502 :
1503 1 : char const * cargv[] =
1504 : {
1505 : "/usr/bin/arguments",
1506 : "--license",
1507 : nullptr
1508 : };
1509 1 : int const argc(sizeof(cargv) / sizeof(cargv[0]) - 1);
1510 1 : char ** argv = const_cast<char **>(cargv);
1511 :
1512 2 : advgetopt::getopt opt(environment_options, argc, argv);
1513 :
1514 : // check that the result is valid
1515 :
1516 : // an invalid parameter, MUST NOT EXIST
1517 1 : CATCH_REQUIRE(opt.get_option("invalid-parameter") == nullptr);
1518 1 : CATCH_REQUIRE(opt.get_option('Z') == nullptr);
1519 1 : CATCH_REQUIRE_FALSE(opt.is_defined("invalid-parameter"));
1520 1 : CATCH_REQUIRE(opt.get_default("invalid-parameter").empty());
1521 1 : CATCH_REQUIRE(opt.size("invalid-parameter") == 0);
1522 :
1523 : // no default
1524 1 : CATCH_REQUIRE(opt.get_option("--") == nullptr);
1525 1 : CATCH_REQUIRE_FALSE(opt.is_defined("--"));
1526 1 : CATCH_REQUIRE(opt.get_default("--").empty());
1527 1 : CATCH_REQUIRE(opt.size("--") == 0);
1528 :
1529 : // valid parameter
1530 1 : CATCH_REQUIRE(opt.get_option("size") != nullptr);
1531 1 : CATCH_REQUIRE(opt.get_option('s') == opt.get_option("size"));
1532 1 : CATCH_REQUIRE_FALSE(opt.is_defined("size"));
1533 1 : CATCH_REQUIRE(opt.get_string("size") == "73");
1534 1 : CATCH_REQUIRE(opt.get_string("size", 0) == "73");
1535 1 : CATCH_REQUIRE(opt["size"] == "73");
1536 1 : CATCH_REQUIRE(opt.get_long("size") == 73);
1537 1 : CATCH_REQUIRE(opt.get_long("size", 0) == 73);
1538 1 : CATCH_REQUIRE(opt.has_default("size"));
1539 1 : CATCH_REQUIRE(opt.get_default("size") == "73");
1540 1 : CATCH_REQUIRE(opt.size("size") == 0);
1541 :
1542 : // license parameter
1543 1 : CATCH_REQUIRE(opt.get_option("license") != nullptr);
1544 1 : CATCH_REQUIRE(opt.get_option('L') == opt.get_option("license"));
1545 1 : CATCH_REQUIRE(opt.is_defined("license"));
1546 1 : CATCH_REQUIRE(opt.get_string("license") == "");
1547 1 : CATCH_REQUIRE(opt.get_string("license", 0) == "");
1548 1 : CATCH_REQUIRE(opt["license"] == "");
1549 1 : CATCH_REQUIRE_FALSE(opt.has_default("license"));
1550 1 : CATCH_REQUIRE(opt.get_default("license").empty());
1551 1 : CATCH_REQUIRE(opt.size("license") == 1);
1552 :
1553 : // other parameters
1554 1 : CATCH_REQUIRE(opt.get_program_name() == "arguments");
1555 1 : CATCH_REQUIRE(opt.get_program_fullname() == "/usr/bin/arguments");
1556 :
1557 : // process system options now
1558 2 : std::stringstream ss;
1559 1 : advgetopt::flag_t const result(opt.process_system_options(ss));
1560 1 : CATCH_REQUIRE(result == advgetopt::SYSTEM_OPTION_LICENSE);
1561 1 : CATCH_REQUIRE(ss.str() == "GPL v2\n");
1562 : }
1563 : CATCH_END_SECTION()
1564 :
1565 4 : CATCH_START_SECTION("Check with the --license system flag, without a --license on the command line")
1566 1 : advgetopt::option const options[] =
1567 : {
1568 : advgetopt::define_option(
1569 : advgetopt::Name("size")
1570 : , advgetopt::ShortName('s')
1571 : , advgetopt::Flags(advgetopt::command_flags<advgetopt::GETOPT_FLAG_REQUIRED>())
1572 : , advgetopt::Help("define the size.")
1573 : , advgetopt::DefaultValue("103")
1574 : ),
1575 : advgetopt::end_options()
1576 : };
1577 :
1578 1 : advgetopt::options_environment environment_options;
1579 1 : environment_options.f_project_name = "unittest";
1580 1 : environment_options.f_options = options;
1581 1 : environment_options.f_environment_flags = advgetopt::GETOPT_ENVIRONMENT_FLAG_SYSTEM_PARAMETERS;
1582 1 : environment_options.f_help_header = "Usage: test system commands";
1583 1 : environment_options.f_help_footer = "Copyright matters";
1584 :
1585 1 : char const * cargv[] =
1586 : {
1587 : "/usr/bin/arguments",
1588 : "--size",
1589 : "1221",
1590 : nullptr
1591 : };
1592 1 : int const argc(sizeof(cargv) / sizeof(cargv[0]) - 1);
1593 1 : char ** argv = const_cast<char **>(cargv);
1594 :
1595 2 : advgetopt::getopt opt(environment_options, argc, argv);
1596 :
1597 : // check that the result is valid
1598 :
1599 : // an invalid parameter, MUST NOT EXIST
1600 1 : CATCH_REQUIRE(opt.get_option("invalid-parameter") == nullptr);
1601 1 : CATCH_REQUIRE(opt.get_option('Z') == nullptr);
1602 1 : CATCH_REQUIRE_FALSE(opt.is_defined("invalid-parameter"));
1603 1 : CATCH_REQUIRE(opt.get_default("invalid-parameter").empty());
1604 1 : CATCH_REQUIRE(opt.size("invalid-parameter") == 0);
1605 :
1606 : // no default
1607 1 : CATCH_REQUIRE(opt.get_option("--") == nullptr);
1608 1 : CATCH_REQUIRE_FALSE(opt.is_defined("--"));
1609 1 : CATCH_REQUIRE(opt.get_default("--").empty());
1610 1 : CATCH_REQUIRE(opt.size("--") == 0);
1611 :
1612 : // valid parameter
1613 1 : CATCH_REQUIRE(opt.get_option("size") != nullptr);
1614 1 : CATCH_REQUIRE(opt.get_option('s') == opt.get_option("size"));
1615 1 : CATCH_REQUIRE(opt.is_defined("size"));
1616 1 : CATCH_REQUIRE(opt.get_string("size") == "1221");
1617 1 : CATCH_REQUIRE(opt.get_string("size", 0) == "1221");
1618 1 : CATCH_REQUIRE(opt["size"] == "1221");
1619 1 : CATCH_REQUIRE(opt.get_long("size") == 1221);
1620 1 : CATCH_REQUIRE(opt.get_long("size", 0) == 1221);
1621 1 : CATCH_REQUIRE(opt.has_default("size"));
1622 1 : CATCH_REQUIRE(opt.get_default("size") == "103");
1623 1 : CATCH_REQUIRE(opt.size("size") == 1);
1624 :
1625 : // license parameter
1626 1 : CATCH_REQUIRE(opt.get_option("license") != nullptr);
1627 1 : CATCH_REQUIRE(opt.get_option('L') == opt.get_option("license"));
1628 1 : CATCH_REQUIRE_FALSE(opt.is_defined("license"));
1629 1 : CATCH_REQUIRE_FALSE(opt.has_default("license"));
1630 1 : CATCH_REQUIRE(opt.get_default("license").empty());
1631 1 : CATCH_REQUIRE(opt.size("license") == 0);
1632 :
1633 : // other parameters
1634 1 : CATCH_REQUIRE(opt.get_program_name() == "arguments");
1635 1 : CATCH_REQUIRE(opt.get_program_fullname() == "/usr/bin/arguments");
1636 :
1637 : // process system options now
1638 2 : std::stringstream ss;
1639 1 : advgetopt::flag_t const result(opt.process_system_options(ss));
1640 1 : CATCH_REQUIRE(result == advgetopt::SYSTEM_OPTION_NONE);
1641 1 : CATCH_REQUIRE(ss.str().empty());
1642 : CATCH_END_SECTION()
1643 2 : }
1644 :
1645 :
1646 :
1647 4 : CATCH_TEST_CASE("system_flags_build_date", "[arguments][valid][getopt][system_flags]")
1648 : {
1649 4 : CATCH_START_SECTION("Check with the --build-date system flag")
1650 : {
1651 1 : advgetopt::option const options[] =
1652 : {
1653 : advgetopt::define_option(
1654 : advgetopt::Name("size")
1655 : , advgetopt::ShortName('s')
1656 : , advgetopt::Flags(advgetopt::command_flags<advgetopt::GETOPT_FLAG_REQUIRED>())
1657 : , advgetopt::Help("define the size.")
1658 : , advgetopt::DefaultValue("7301")
1659 : ),
1660 : advgetopt::end_options()
1661 : };
1662 :
1663 1 : advgetopt::options_environment environment_options;
1664 1 : environment_options.f_project_name = "unittest";
1665 1 : environment_options.f_options = options;
1666 1 : environment_options.f_environment_flags = advgetopt::GETOPT_ENVIRONMENT_FLAG_SYSTEM_PARAMETERS;
1667 :
1668 1 : char const * cargv[] =
1669 : {
1670 : "/usr/bin/arguments",
1671 : "--build-date",
1672 : nullptr
1673 : };
1674 1 : int const argc(sizeof(cargv) / sizeof(cargv[0]) - 1);
1675 1 : char ** argv = const_cast<char **>(cargv);
1676 :
1677 2 : advgetopt::getopt opt(environment_options, argc, argv);
1678 :
1679 : // check that the result is valid
1680 :
1681 : // an invalid parameter, MUST NOT EXIST
1682 1 : CATCH_REQUIRE(opt.get_option("invalid-parameter") == nullptr);
1683 1 : CATCH_REQUIRE(opt.get_option('Z') == nullptr);
1684 1 : CATCH_REQUIRE_FALSE(opt.is_defined("invalid-parameter"));
1685 1 : CATCH_REQUIRE(opt.get_default("invalid-parameter").empty());
1686 1 : CATCH_REQUIRE(opt.size("invalid-parameter") == 0);
1687 :
1688 : // no default
1689 1 : CATCH_REQUIRE(opt.get_option("--") == nullptr);
1690 1 : CATCH_REQUIRE_FALSE(opt.is_defined("--"));
1691 1 : CATCH_REQUIRE(opt.get_default("--").empty());
1692 1 : CATCH_REQUIRE(opt.size("--") == 0);
1693 :
1694 : // valid parameter
1695 1 : CATCH_REQUIRE(opt.get_option("size") != nullptr);
1696 1 : CATCH_REQUIRE(opt.get_option('s') == opt.get_option("size"));
1697 1 : CATCH_REQUIRE_FALSE(opt.is_defined("size"));
1698 1 : CATCH_REQUIRE(opt.get_string("size") == "7301");
1699 1 : CATCH_REQUIRE(opt.get_string("size", 0) == "7301");
1700 1 : CATCH_REQUIRE(opt["size"] == "7301");
1701 1 : CATCH_REQUIRE(opt.get_long("size") == 7301);
1702 1 : CATCH_REQUIRE(opt.get_long("size", 0) == 7301);
1703 1 : CATCH_REQUIRE(opt.has_default("size"));
1704 1 : CATCH_REQUIRE(opt.get_default("size") == "7301");
1705 1 : CATCH_REQUIRE(opt.size("size") == 0);
1706 :
1707 : // build-date parameter
1708 1 : CATCH_REQUIRE(opt.get_option("build-date") != nullptr);
1709 1 : CATCH_REQUIRE(opt.is_defined("build-date"));
1710 1 : CATCH_REQUIRE(opt.get_string("build-date") == "");
1711 1 : CATCH_REQUIRE(opt.get_string("build-date", 0) == "");
1712 1 : CATCH_REQUIRE(opt["build-date"] == "");
1713 1 : CATCH_REQUIRE_FALSE(opt.has_default("build-date"));
1714 1 : CATCH_REQUIRE(opt.get_default("build-date").empty());
1715 1 : CATCH_REQUIRE(opt.size("build-date") == 1);
1716 :
1717 : // other parameters
1718 1 : CATCH_REQUIRE(opt.get_program_name() == "arguments");
1719 1 : CATCH_REQUIRE(opt.get_program_fullname() == "/usr/bin/arguments");
1720 :
1721 : // process system options now
1722 2 : std::stringstream ss;
1723 1 : advgetopt::flag_t const result(opt.process_system_options(ss));
1724 1 : CATCH_REQUIRE(result == advgetopt::SYSTEM_OPTION_BUILD_DATE);
1725 1 : CATCH_REQUIRE(ss.str() == "Built on "
1726 : + std::string(environment_options.f_build_date)
1727 : + " at "
1728 : + environment_options.f_build_time
1729 : + "\n");
1730 : }
1731 : CATCH_END_SECTION()
1732 :
1733 4 : CATCH_START_SECTION("Check with the --build-date system flag, without a --build-date on the command line")
1734 1 : advgetopt::option const options[] =
1735 : {
1736 : advgetopt::define_option(
1737 : advgetopt::Name("size")
1738 : , advgetopt::ShortName('s')
1739 : , advgetopt::Flags(advgetopt::command_flags<advgetopt::GETOPT_FLAG_REQUIRED>())
1740 : , advgetopt::Help("define the size.")
1741 : , advgetopt::DefaultValue("103")
1742 : ),
1743 : advgetopt::end_options()
1744 : };
1745 :
1746 1 : advgetopt::options_environment environment_options;
1747 1 : environment_options.f_project_name = "unittest";
1748 1 : environment_options.f_options = options;
1749 1 : environment_options.f_environment_flags = advgetopt::GETOPT_ENVIRONMENT_FLAG_SYSTEM_PARAMETERS;
1750 1 : environment_options.f_help_header = "Usage: test system commands";
1751 1 : environment_options.f_help_footer = "Copyright matters";
1752 :
1753 1 : char const * cargv[] =
1754 : {
1755 : "/usr/bin/arguments",
1756 : "--size",
1757 : "1221",
1758 : nullptr
1759 : };
1760 1 : int const argc(sizeof(cargv) / sizeof(cargv[0]) - 1);
1761 1 : char ** argv = const_cast<char **>(cargv);
1762 :
1763 2 : advgetopt::getopt opt(environment_options, argc, argv);
1764 :
1765 : // check that the result is valid
1766 :
1767 : // an invalid parameter, MUST NOT EXIST
1768 1 : CATCH_REQUIRE(opt.get_option("invalid-parameter") == nullptr);
1769 1 : CATCH_REQUIRE(opt.get_option('Z') == nullptr);
1770 1 : CATCH_REQUIRE_FALSE(opt.is_defined("invalid-parameter"));
1771 1 : CATCH_REQUIRE(opt.get_default("invalid-parameter").empty());
1772 1 : CATCH_REQUIRE(opt.size("invalid-parameter") == 0);
1773 :
1774 : // no default
1775 1 : CATCH_REQUIRE(opt.get_option("--") == nullptr);
1776 1 : CATCH_REQUIRE_FALSE(opt.is_defined("--"));
1777 1 : CATCH_REQUIRE(opt.get_default("--").empty());
1778 1 : CATCH_REQUIRE(opt.size("--") == 0);
1779 :
1780 : // valid parameter
1781 1 : CATCH_REQUIRE(opt.get_option("size") != nullptr);
1782 1 : CATCH_REQUIRE(opt.get_option('s') == opt.get_option("size"));
1783 1 : CATCH_REQUIRE(opt.is_defined("size"));
1784 1 : CATCH_REQUIRE(opt.get_string("size") == "1221");
1785 1 : CATCH_REQUIRE(opt.get_string("size", 0) == "1221");
1786 1 : CATCH_REQUIRE(opt["size"] == "1221");
1787 1 : CATCH_REQUIRE(opt.get_long("size") == 1221);
1788 1 : CATCH_REQUIRE(opt.get_long("size", 0) == 1221);
1789 1 : CATCH_REQUIRE(opt.has_default("size"));
1790 1 : CATCH_REQUIRE(opt.get_default("size") == "103");
1791 1 : CATCH_REQUIRE(opt.size("size") == 1);
1792 :
1793 : // build-date parameter
1794 1 : CATCH_REQUIRE(opt.get_option("build-date") != nullptr);
1795 1 : CATCH_REQUIRE_FALSE(opt.is_defined("build-date"));
1796 1 : CATCH_REQUIRE_FALSE(opt.has_default("build-date"));
1797 1 : CATCH_REQUIRE(opt.get_default("build-date").empty());
1798 1 : CATCH_REQUIRE(opt.size("build-date") == 0);
1799 :
1800 : // other parameters
1801 1 : CATCH_REQUIRE(opt.get_program_name() == "arguments");
1802 1 : CATCH_REQUIRE(opt.get_program_fullname() == "/usr/bin/arguments");
1803 :
1804 : // process system options now
1805 2 : std::stringstream ss;
1806 1 : advgetopt::flag_t const result(opt.process_system_options(ss));
1807 1 : CATCH_REQUIRE(result == advgetopt::SYSTEM_OPTION_NONE);
1808 1 : CATCH_REQUIRE(ss.str().empty());
1809 : CATCH_END_SECTION()
1810 2 : }
1811 :
1812 :
1813 :
1814 6 : CATCH_TEST_CASE("system_flags_environment_variable_name", "[arguments][valid][getopt][system_flags]")
1815 : {
1816 8 : CATCH_START_SECTION("Check with the --environment-variable-name system flag")
1817 : {
1818 1 : advgetopt::option const options[] =
1819 : {
1820 : advgetopt::define_option(
1821 : advgetopt::Name("size")
1822 : , advgetopt::ShortName('s')
1823 : , advgetopt::Flags(advgetopt::command_flags<advgetopt::GETOPT_FLAG_REQUIRED>())
1824 : , advgetopt::Help("define the size.")
1825 : , advgetopt::DefaultValue("7301")
1826 : ),
1827 : advgetopt::end_options()
1828 : };
1829 :
1830 1 : advgetopt::options_environment environment_options;
1831 1 : environment_options.f_project_name = "unittest";
1832 1 : environment_options.f_options = options;
1833 1 : environment_options.f_environment_flags = advgetopt::GETOPT_ENVIRONMENT_FLAG_SYSTEM_PARAMETERS;
1834 1 : environment_options.f_environment_variable_name = "ADVGETOPT_OPTIONS";
1835 :
1836 1 : char const * cargv[] =
1837 : {
1838 : "/usr/bin/arguments",
1839 : "--environment-variable-name",
1840 : nullptr
1841 : };
1842 1 : int const argc(sizeof(cargv) / sizeof(cargv[0]) - 1);
1843 1 : char ** argv = const_cast<char **>(cargv);
1844 :
1845 2 : advgetopt::getopt opt(environment_options, argc, argv);
1846 :
1847 : // check that the result is valid
1848 :
1849 : // an invalid parameter, MUST NOT EXIST
1850 1 : CATCH_REQUIRE(opt.get_option("invalid-parameter") == nullptr);
1851 1 : CATCH_REQUIRE(opt.get_option('Z') == nullptr);
1852 1 : CATCH_REQUIRE_FALSE(opt.is_defined("invalid-parameter"));
1853 1 : CATCH_REQUIRE(opt.get_default("invalid-parameter").empty());
1854 1 : CATCH_REQUIRE(opt.size("invalid-parameter") == 0);
1855 :
1856 : // no default
1857 1 : CATCH_REQUIRE(opt.get_option("--") == nullptr);
1858 1 : CATCH_REQUIRE_FALSE(opt.is_defined("--"));
1859 1 : CATCH_REQUIRE(opt.get_default("--").empty());
1860 1 : CATCH_REQUIRE(opt.size("--") == 0);
1861 :
1862 : // valid parameter
1863 1 : CATCH_REQUIRE(opt.get_option("size") != nullptr);
1864 1 : CATCH_REQUIRE(opt.get_option('s') == opt.get_option("size"));
1865 1 : CATCH_REQUIRE_FALSE(opt.is_defined("size"));
1866 1 : CATCH_REQUIRE(opt.get_string("size") == "7301");
1867 1 : CATCH_REQUIRE(opt.get_string("size", 0) == "7301");
1868 1 : CATCH_REQUIRE(opt["size"] == "7301");
1869 1 : CATCH_REQUIRE(opt.get_long("size") == 7301);
1870 1 : CATCH_REQUIRE(opt.get_long("size", 0) == 7301);
1871 1 : CATCH_REQUIRE(opt.has_default("size"));
1872 1 : CATCH_REQUIRE(opt.get_default("size") == "7301");
1873 1 : CATCH_REQUIRE(opt.size("size") == 0);
1874 :
1875 : // environment-variable-name parameter
1876 1 : CATCH_REQUIRE(opt.get_option("environment-variable-name") != nullptr);
1877 1 : CATCH_REQUIRE(opt.is_defined("environment-variable-name"));
1878 1 : CATCH_REQUIRE(opt.get_string("environment-variable-name") == "");
1879 1 : CATCH_REQUIRE(opt.get_string("environment-variable-name", 0) == "");
1880 1 : CATCH_REQUIRE(opt["environment-variable-name"] == "");
1881 1 : CATCH_REQUIRE_FALSE(opt.has_default("environment-variable-name"));
1882 1 : CATCH_REQUIRE(opt.get_default("environment-variable-name").empty());
1883 1 : CATCH_REQUIRE(opt.size("environment-variable-name") == 1);
1884 :
1885 : // other parameters
1886 1 : CATCH_REQUIRE(opt.get_program_name() == "arguments");
1887 1 : CATCH_REQUIRE(opt.get_program_fullname() == "/usr/bin/arguments");
1888 :
1889 : // process system options now
1890 2 : std::stringstream ss;
1891 1 : advgetopt::flag_t const result(opt.process_system_options(ss));
1892 1 : CATCH_REQUIRE(result == advgetopt::SYSTEM_OPTION_ENVIRONMENT_VARIABLE_NAME);
1893 1 : CATCH_REQUIRE(ss.str() == "ADVGETOPT_OPTIONS\n");
1894 : }
1895 : CATCH_END_SECTION()
1896 :
1897 8 : CATCH_START_SECTION("Check with the --environment-variable-name system flag with nullptr")
1898 : {
1899 1 : advgetopt::option const options[] =
1900 : {
1901 : advgetopt::define_option(
1902 : advgetopt::Name("size")
1903 : , advgetopt::ShortName('s')
1904 : , advgetopt::Flags(advgetopt::command_flags<advgetopt::GETOPT_FLAG_REQUIRED>())
1905 : , advgetopt::Help("define the size.")
1906 : , advgetopt::DefaultValue("7301")
1907 : ),
1908 : advgetopt::end_options()
1909 : };
1910 :
1911 1 : advgetopt::options_environment environment_options;
1912 1 : environment_options.f_project_name = "unittest";
1913 1 : environment_options.f_options = options;
1914 1 : environment_options.f_environment_flags = advgetopt::GETOPT_ENVIRONMENT_FLAG_SYSTEM_PARAMETERS;
1915 1 : environment_options.f_environment_variable_name = nullptr;
1916 :
1917 1 : char const * cargv[] =
1918 : {
1919 : "/usr/bin/arguments",
1920 : "--environment-variable-name",
1921 : nullptr
1922 : };
1923 1 : int const argc(sizeof(cargv) / sizeof(cargv[0]) - 1);
1924 1 : char ** argv = const_cast<char **>(cargv);
1925 :
1926 2 : advgetopt::getopt opt(environment_options, argc, argv);
1927 :
1928 : // check that the result is valid
1929 :
1930 : // an invalid parameter, MUST NOT EXIST
1931 1 : CATCH_REQUIRE(opt.get_option("invalid-parameter") == nullptr);
1932 1 : CATCH_REQUIRE(opt.get_option('Z') == nullptr);
1933 1 : CATCH_REQUIRE_FALSE(opt.is_defined("invalid-parameter"));
1934 1 : CATCH_REQUIRE(opt.get_default("invalid-parameter").empty());
1935 1 : CATCH_REQUIRE(opt.size("invalid-parameter") == 0);
1936 :
1937 : // no default
1938 1 : CATCH_REQUIRE(opt.get_option("--") == nullptr);
1939 1 : CATCH_REQUIRE_FALSE(opt.is_defined("--"));
1940 1 : CATCH_REQUIRE(opt.get_default("--").empty());
1941 1 : CATCH_REQUIRE(opt.size("--") == 0);
1942 :
1943 : // valid parameter
1944 1 : CATCH_REQUIRE(opt.get_option("size") != nullptr);
1945 1 : CATCH_REQUIRE(opt.get_option('s') == opt.get_option("size"));
1946 1 : CATCH_REQUIRE_FALSE(opt.is_defined("size"));
1947 1 : CATCH_REQUIRE(opt.get_string("size") == "7301");
1948 1 : CATCH_REQUIRE(opt.get_string("size", 0) == "7301");
1949 1 : CATCH_REQUIRE(opt["size"] == "7301");
1950 1 : CATCH_REQUIRE(opt.get_long("size") == 7301);
1951 1 : CATCH_REQUIRE(opt.get_long("size", 0) == 7301);
1952 1 : CATCH_REQUIRE(opt.has_default("size"));
1953 1 : CATCH_REQUIRE(opt.get_default("size") == "7301");
1954 1 : CATCH_REQUIRE(opt.size("size") == 0);
1955 :
1956 : // environment-variable-name parameter
1957 1 : CATCH_REQUIRE(opt.get_option("environment-variable-name") != nullptr);
1958 1 : CATCH_REQUIRE(opt.is_defined("environment-variable-name"));
1959 1 : CATCH_REQUIRE(opt.get_string("environment-variable-name") == "");
1960 1 : CATCH_REQUIRE(opt.get_string("environment-variable-name", 0) == "");
1961 1 : CATCH_REQUIRE(opt["environment-variable-name"] == "");
1962 1 : CATCH_REQUIRE_FALSE(opt.has_default("environment-variable-name"));
1963 1 : CATCH_REQUIRE(opt.get_default("environment-variable-name").empty());
1964 1 : CATCH_REQUIRE(opt.size("environment-variable-name") == 1);
1965 :
1966 : // other parameters
1967 1 : CATCH_REQUIRE(opt.get_program_name() == "arguments");
1968 1 : CATCH_REQUIRE(opt.get_program_fullname() == "/usr/bin/arguments");
1969 :
1970 : // process system options now
1971 2 : std::stringstream ss;
1972 1 : advgetopt::flag_t const result(opt.process_system_options(ss));
1973 1 : CATCH_REQUIRE(result == advgetopt::SYSTEM_OPTION_ENVIRONMENT_VARIABLE_NAME);
1974 1 : CATCH_REQUIRE(ss.str() == "unittest does not support an environment variable.\n");
1975 : }
1976 : CATCH_END_SECTION()
1977 :
1978 8 : CATCH_START_SECTION("Check with the --environment-variable-name system flag with \"\"")
1979 : {
1980 1 : advgetopt::option const options[] =
1981 : {
1982 : advgetopt::define_option(
1983 : advgetopt::Name("size")
1984 : , advgetopt::ShortName('s')
1985 : , advgetopt::Flags(advgetopt::command_flags<advgetopt::GETOPT_FLAG_REQUIRED>())
1986 : , advgetopt::Help("define the size.")
1987 : , advgetopt::DefaultValue("7301")
1988 : ),
1989 : advgetopt::end_options()
1990 : };
1991 :
1992 1 : advgetopt::options_environment environment_options;
1993 1 : environment_options.f_project_name = "unittest";
1994 1 : environment_options.f_options = options;
1995 1 : environment_options.f_environment_flags = advgetopt::GETOPT_ENVIRONMENT_FLAG_SYSTEM_PARAMETERS;
1996 1 : environment_options.f_environment_variable_name = "";
1997 :
1998 1 : char const * cargv[] =
1999 : {
2000 : "/usr/bin/arguments",
2001 : "--environment-variable-name",
2002 : nullptr
2003 : };
2004 1 : int const argc(sizeof(cargv) / sizeof(cargv[0]) - 1);
2005 1 : char ** argv = const_cast<char **>(cargv);
2006 :
2007 2 : advgetopt::getopt opt(environment_options, argc, argv);
2008 :
2009 : // check that the result is valid
2010 :
2011 : // an invalid parameter, MUST NOT EXIST
2012 1 : CATCH_REQUIRE(opt.get_option("invalid-parameter") == nullptr);
2013 1 : CATCH_REQUIRE(opt.get_option('Z') == nullptr);
2014 1 : CATCH_REQUIRE_FALSE(opt.is_defined("invalid-parameter"));
2015 1 : CATCH_REQUIRE(opt.get_default("invalid-parameter").empty());
2016 1 : CATCH_REQUIRE(opt.size("invalid-parameter") == 0);
2017 :
2018 : // no default
2019 1 : CATCH_REQUIRE(opt.get_option("--") == nullptr);
2020 1 : CATCH_REQUIRE_FALSE(opt.is_defined("--"));
2021 1 : CATCH_REQUIRE(opt.get_default("--").empty());
2022 1 : CATCH_REQUIRE(opt.size("--") == 0);
2023 :
2024 : // valid parameter
2025 1 : CATCH_REQUIRE(opt.get_option("size") != nullptr);
2026 1 : CATCH_REQUIRE(opt.get_option('s') == opt.get_option("size"));
2027 1 : CATCH_REQUIRE_FALSE(opt.is_defined("size"));
2028 1 : CATCH_REQUIRE(opt.get_string("size") == "7301");
2029 1 : CATCH_REQUIRE(opt.get_string("size", 0) == "7301");
2030 1 : CATCH_REQUIRE(opt["size"] == "7301");
2031 1 : CATCH_REQUIRE(opt.get_long("size") == 7301);
2032 1 : CATCH_REQUIRE(opt.get_long("size", 0) == 7301);
2033 1 : CATCH_REQUIRE(opt.has_default("size"));
2034 1 : CATCH_REQUIRE(opt.get_default("size") == "7301");
2035 1 : CATCH_REQUIRE(opt.size("size") == 0);
2036 :
2037 : // environment-variable-name parameter
2038 1 : CATCH_REQUIRE(opt.get_option("environment-variable-name") != nullptr);
2039 1 : CATCH_REQUIRE(opt.is_defined("environment-variable-name"));
2040 1 : CATCH_REQUIRE(opt.get_string("environment-variable-name") == "");
2041 1 : CATCH_REQUIRE(opt.get_string("environment-variable-name", 0) == "");
2042 1 : CATCH_REQUIRE(opt["environment-variable-name"] == "");
2043 1 : CATCH_REQUIRE_FALSE(opt.has_default("environment-variable-name"));
2044 1 : CATCH_REQUIRE(opt.get_default("environment-variable-name").empty());
2045 1 : CATCH_REQUIRE(opt.size("environment-variable-name") == 1);
2046 :
2047 : // other parameters
2048 1 : CATCH_REQUIRE(opt.get_program_name() == "arguments");
2049 1 : CATCH_REQUIRE(opt.get_program_fullname() == "/usr/bin/arguments");
2050 :
2051 : // process system options now
2052 2 : std::stringstream ss;
2053 1 : advgetopt::flag_t const result(opt.process_system_options(ss));
2054 1 : CATCH_REQUIRE(result == advgetopt::SYSTEM_OPTION_ENVIRONMENT_VARIABLE_NAME);
2055 1 : CATCH_REQUIRE(ss.str() == "unittest does not support an environment variable.\n");
2056 : }
2057 : CATCH_END_SECTION()
2058 :
2059 8 : CATCH_START_SECTION("Check with the --environment-variable-name system flag, without a --environment-variable-name on the command line")
2060 1 : advgetopt::option const options[] =
2061 : {
2062 : advgetopt::define_option(
2063 : advgetopt::Name("size")
2064 : , advgetopt::ShortName('s')
2065 : , advgetopt::Flags(advgetopt::command_flags<advgetopt::GETOPT_FLAG_REQUIRED>())
2066 : , advgetopt::Help("define the size.")
2067 : , advgetopt::DefaultValue("103")
2068 : ),
2069 : advgetopt::end_options()
2070 : };
2071 :
2072 1 : advgetopt::options_environment environment_options;
2073 1 : environment_options.f_project_name = "unittest";
2074 1 : environment_options.f_options = options;
2075 1 : environment_options.f_environment_flags = advgetopt::GETOPT_ENVIRONMENT_FLAG_SYSTEM_PARAMETERS;
2076 1 : environment_options.f_help_header = "Usage: test system commands";
2077 1 : environment_options.f_help_footer = "Copyright matters";
2078 :
2079 1 : char const * cargv[] =
2080 : {
2081 : "/usr/bin/arguments",
2082 : "--size",
2083 : "1221",
2084 : nullptr
2085 : };
2086 1 : int const argc(sizeof(cargv) / sizeof(cargv[0]) - 1);
2087 1 : char ** argv = const_cast<char **>(cargv);
2088 :
2089 2 : advgetopt::getopt opt(environment_options, argc, argv);
2090 :
2091 : // check that the result is valid
2092 :
2093 : // an invalid parameter, MUST NOT EXIST
2094 1 : CATCH_REQUIRE(opt.get_option("invalid-parameter") == nullptr);
2095 1 : CATCH_REQUIRE(opt.get_option('Z') == nullptr);
2096 1 : CATCH_REQUIRE_FALSE(opt.is_defined("invalid-parameter"));
2097 1 : CATCH_REQUIRE(opt.get_default("invalid-parameter").empty());
2098 1 : CATCH_REQUIRE(opt.size("invalid-parameter") == 0);
2099 :
2100 : // no default
2101 1 : CATCH_REQUIRE(opt.get_option("--") == nullptr);
2102 1 : CATCH_REQUIRE_FALSE(opt.is_defined("--"));
2103 1 : CATCH_REQUIRE(opt.get_default("--").empty());
2104 1 : CATCH_REQUIRE(opt.size("--") == 0);
2105 :
2106 : // valid parameter
2107 1 : CATCH_REQUIRE(opt.get_option("size") != nullptr);
2108 1 : CATCH_REQUIRE(opt.get_option('s') == opt.get_option("size"));
2109 1 : CATCH_REQUIRE(opt.is_defined("size"));
2110 1 : CATCH_REQUIRE(opt.get_string("size") == "1221");
2111 1 : CATCH_REQUIRE(opt.get_string("size", 0) == "1221");
2112 1 : CATCH_REQUIRE(opt["size"] == "1221");
2113 1 : CATCH_REQUIRE(opt.get_long("size") == 1221);
2114 1 : CATCH_REQUIRE(opt.get_long("size", 0) == 1221);
2115 1 : CATCH_REQUIRE(opt.has_default("size"));
2116 1 : CATCH_REQUIRE(opt.get_default("size") == "103");
2117 1 : CATCH_REQUIRE(opt.size("size") == 1);
2118 :
2119 : // environment-variable-name parameter
2120 1 : CATCH_REQUIRE(opt.get_option("environment-variable-name") != nullptr);
2121 1 : CATCH_REQUIRE_FALSE(opt.is_defined("environment-variable-name"));
2122 1 : CATCH_REQUIRE_FALSE(opt.has_default("environment-variable-name"));
2123 1 : CATCH_REQUIRE(opt.get_default("environment-variable-name").empty());
2124 1 : CATCH_REQUIRE(opt.size("environment-variable-name") == 0);
2125 :
2126 : // other parameters
2127 1 : CATCH_REQUIRE(opt.get_program_name() == "arguments");
2128 1 : CATCH_REQUIRE(opt.get_program_fullname() == "/usr/bin/arguments");
2129 :
2130 : // process system options now
2131 2 : std::stringstream ss;
2132 1 : advgetopt::flag_t const result(opt.process_system_options(ss));
2133 1 : CATCH_REQUIRE(result == advgetopt::SYSTEM_OPTION_NONE);
2134 1 : CATCH_REQUIRE(ss.str().empty());
2135 : CATCH_END_SECTION()
2136 4 : }
2137 :
2138 :
2139 :
2140 6 : CATCH_TEST_CASE("system_flags_configuration_filenames", "[arguments][valid][getopt][system_flags]")
2141 : {
2142 8 : CATCH_START_SECTION("Check with the --configuration-filenames system flag")
2143 : {
2144 2 : snap::safe_setenv env("HOME", "/home/advgetopt");
2145 :
2146 1 : advgetopt::option const options[] =
2147 : {
2148 : advgetopt::define_option(
2149 : advgetopt::Name("size")
2150 : , advgetopt::ShortName('s')
2151 : , advgetopt::Flags(advgetopt::command_flags<advgetopt::GETOPT_FLAG_REQUIRED>())
2152 : , advgetopt::Help("define the size.")
2153 : , advgetopt::DefaultValue("3101")
2154 : ),
2155 : advgetopt::end_options()
2156 : };
2157 :
2158 1 : char const * confs[] =
2159 : {
2160 : ".config/file.mdi",
2161 : "/etc/snapwebsites/server.conf",
2162 : "~/.config/advgetopt/snap.conf",
2163 : nullptr
2164 : };
2165 1 : char const * dirs[] =
2166 : {
2167 : ".config",
2168 : "/etc/secret",
2169 : "~/.config/snapwebsites",
2170 : nullptr
2171 : };
2172 :
2173 1 : advgetopt::options_environment environment_options;
2174 1 : environment_options.f_project_name = "unittest";
2175 1 : environment_options.f_options = options;
2176 1 : environment_options.f_environment_flags = advgetopt::GETOPT_ENVIRONMENT_FLAG_SYSTEM_PARAMETERS;
2177 1 : environment_options.f_configuration_files = confs;
2178 1 : environment_options.f_configuration_filename = "snapdb.conf";
2179 1 : environment_options.f_configuration_directories = dirs;
2180 :
2181 1 : char const * cargv[] =
2182 : {
2183 : "/usr/bin/arguments",
2184 : "--configuration-filenames",
2185 : nullptr
2186 : };
2187 1 : int const argc(sizeof(cargv) / sizeof(cargv[0]) - 1);
2188 1 : char ** argv = const_cast<char **>(cargv);
2189 :
2190 2 : advgetopt::getopt opt(environment_options, argc, argv);
2191 :
2192 : // check that the result is valid
2193 :
2194 : // an invalid parameter, MUST NOT EXIST
2195 1 : CATCH_REQUIRE(opt.get_option("invalid-parameter") == nullptr);
2196 1 : CATCH_REQUIRE(opt.get_option('Z') == nullptr);
2197 1 : CATCH_REQUIRE_FALSE(opt.is_defined("invalid-parameter"));
2198 1 : CATCH_REQUIRE(opt.get_default("invalid-parameter").empty());
2199 1 : CATCH_REQUIRE(opt.size("invalid-parameter") == 0);
2200 :
2201 : // no default
2202 1 : CATCH_REQUIRE(opt.get_option("--") == nullptr);
2203 1 : CATCH_REQUIRE_FALSE(opt.is_defined("--"));
2204 1 : CATCH_REQUIRE(opt.get_default("--").empty());
2205 1 : CATCH_REQUIRE(opt.size("--") == 0);
2206 :
2207 : // valid parameter
2208 1 : CATCH_REQUIRE(opt.get_option("size") != nullptr);
2209 1 : CATCH_REQUIRE(opt.get_option('s') == opt.get_option("size"));
2210 1 : CATCH_REQUIRE_FALSE(opt.is_defined("size"));
2211 1 : CATCH_REQUIRE(opt.get_string("size") == "3101");
2212 1 : CATCH_REQUIRE(opt.get_string("size", 0) == "3101");
2213 1 : CATCH_REQUIRE(opt["size"] == "3101");
2214 1 : CATCH_REQUIRE(opt.get_long("size") == 3101);
2215 1 : CATCH_REQUIRE(opt.get_long("size", 0) == 3101);
2216 1 : CATCH_REQUIRE(opt.has_default("size"));
2217 1 : CATCH_REQUIRE(opt.get_default("size") == "3101");
2218 1 : CATCH_REQUIRE(opt.size("size") == 0);
2219 :
2220 : // configuration-filenames parameter
2221 1 : CATCH_REQUIRE(opt.get_option("configuration-filenames") != nullptr);
2222 1 : CATCH_REQUIRE(opt.is_defined("configuration-filenames"));
2223 1 : CATCH_REQUIRE(opt.get_string("configuration-filenames") == "");
2224 1 : CATCH_REQUIRE(opt.get_string("configuration-filenames", 0) == "");
2225 1 : CATCH_REQUIRE(opt["configuration-filenames"] == "");
2226 1 : CATCH_REQUIRE_FALSE(opt.has_default("configuration-filenames"));
2227 1 : CATCH_REQUIRE(opt.get_default("configuration-filenames").empty());
2228 1 : CATCH_REQUIRE(opt.size("configuration-filenames") == 1);
2229 :
2230 : // other parameters
2231 1 : CATCH_REQUIRE(opt.get_program_name() == "arguments");
2232 1 : CATCH_REQUIRE(opt.get_program_fullname() == "/usr/bin/arguments");
2233 :
2234 : // process system options now
2235 2 : std::stringstream ss;
2236 1 : advgetopt::flag_t const result(opt.process_system_options(ss));
2237 1 : CATCH_REQUIRE(result == advgetopt::SYSTEM_OPTION_CONFIGURATION_FILENAMES);
2238 1 : CATCH_REQUIRE(getenv("HOME") != nullptr);
2239 2 : std::string const home(getenv("HOME"));
2240 1 : CATCH_REQUIRE(ss.str() ==
2241 : "Configuration filenames:\n"
2242 : " . .config/file.mdi\n"
2243 : " . .config/unittest.d/50-file.mdi\n"
2244 : " . /etc/snapwebsites/server.conf\n"
2245 : " . /etc/snapwebsites/unittest.d/50-server.conf\n"
2246 : " . " + home + "/.config/advgetopt/snap.conf\n"
2247 : " . .config/snapdb.conf\n"
2248 : " . .config/unittest.d/50-snapdb.conf\n"
2249 : " . /etc/secret/snapdb.conf\n"
2250 : " . /etc/secret/unittest.d/50-snapdb.conf\n"
2251 : " . " + home + "/.config/snapwebsites/snapdb.conf\n"
2252 : );
2253 : }
2254 : CATCH_END_SECTION()
2255 :
2256 8 : CATCH_START_SECTION("Check with the --configuration-filenames system flag with --config-dir too")
2257 : {
2258 2 : snap::safe_setenv env("HOME", "/home/advgetopt");
2259 :
2260 1 : advgetopt::option const options[] =
2261 : {
2262 : advgetopt::define_option(
2263 : advgetopt::Name("size")
2264 : , advgetopt::ShortName('s')
2265 : , advgetopt::Flags(advgetopt::command_flags<advgetopt::GETOPT_FLAG_REQUIRED>())
2266 : , advgetopt::Help("define the size.")
2267 : , advgetopt::DefaultValue("3101")
2268 : ),
2269 : advgetopt::end_options()
2270 : };
2271 :
2272 1 : char const * confs[] =
2273 : {
2274 : ".config/file.mdi",
2275 : "/etc/snapwebsites/server.conf",
2276 : "~/.config/advgetopt/snap.conf",
2277 : nullptr
2278 : };
2279 1 : char const * dirs[] =
2280 : {
2281 : ".config",
2282 : "/etc/secret",
2283 : "~/.config/snapwebsites",
2284 : nullptr
2285 : };
2286 :
2287 1 : advgetopt::options_environment environment_options;
2288 1 : environment_options.f_project_name = "unittest";
2289 1 : environment_options.f_options = options;
2290 1 : environment_options.f_environment_flags = advgetopt::GETOPT_ENVIRONMENT_FLAG_SYSTEM_PARAMETERS;
2291 1 : environment_options.f_configuration_files = confs;
2292 1 : environment_options.f_configuration_filename = "snapdb.conf";
2293 1 : environment_options.f_configuration_directories = dirs;
2294 :
2295 1 : char const * cargv[] =
2296 : {
2297 : "/usr/bin/arguments",
2298 : "--config-dir",
2299 : "/var/lib/advgetopt",
2300 : "--configuration-filenames",
2301 : "--config-dir",
2302 : "/opt/config",
2303 : nullptr
2304 : };
2305 1 : int const argc(sizeof(cargv) / sizeof(cargv[0]) - 1);
2306 1 : char ** argv = const_cast<char **>(cargv);
2307 :
2308 2 : advgetopt::getopt opt(environment_options, argc, argv);
2309 :
2310 : // check that the result is valid
2311 :
2312 : // an invalid parameter, MUST NOT EXIST
2313 1 : CATCH_REQUIRE(opt.get_option("invalid-parameter") == nullptr);
2314 1 : CATCH_REQUIRE(opt.get_option('Z') == nullptr);
2315 1 : CATCH_REQUIRE_FALSE(opt.is_defined("invalid-parameter"));
2316 1 : CATCH_REQUIRE(opt.get_default("invalid-parameter").empty());
2317 1 : CATCH_REQUIRE(opt.size("invalid-parameter") == 0);
2318 :
2319 : // no default
2320 1 : CATCH_REQUIRE(opt.get_option("--") == nullptr);
2321 1 : CATCH_REQUIRE_FALSE(opt.is_defined("--"));
2322 1 : CATCH_REQUIRE(opt.get_default("--").empty());
2323 1 : CATCH_REQUIRE(opt.size("--") == 0);
2324 :
2325 : // valid parameter
2326 1 : CATCH_REQUIRE(opt.get_option("size") != nullptr);
2327 1 : CATCH_REQUIRE(opt.get_option('s') == opt.get_option("size"));
2328 1 : CATCH_REQUIRE_FALSE(opt.is_defined("size"));
2329 1 : CATCH_REQUIRE(opt.get_string("size") == "3101");
2330 1 : CATCH_REQUIRE(opt.get_string("size", 0) == "3101");
2331 1 : CATCH_REQUIRE(opt["size"] == "3101");
2332 1 : CATCH_REQUIRE(opt.get_long("size") == 3101);
2333 1 : CATCH_REQUIRE(opt.get_long("size", 0) == 3101);
2334 1 : CATCH_REQUIRE(opt.has_default("size"));
2335 1 : CATCH_REQUIRE(opt.get_default("size") == "3101");
2336 1 : CATCH_REQUIRE(opt.size("size") == 0);
2337 :
2338 : // configuration-filenames parameter
2339 1 : CATCH_REQUIRE(opt.get_option("configuration-filenames") != nullptr);
2340 1 : CATCH_REQUIRE(opt.is_defined("configuration-filenames"));
2341 1 : CATCH_REQUIRE(opt.get_string("configuration-filenames") == "");
2342 1 : CATCH_REQUIRE(opt.get_string("configuration-filenames", 0) == "");
2343 1 : CATCH_REQUIRE(opt["configuration-filenames"] == "");
2344 1 : CATCH_REQUIRE_FALSE(opt.has_default("configuration-filenames"));
2345 1 : CATCH_REQUIRE(opt.get_default("configuration-filenames").empty());
2346 1 : CATCH_REQUIRE(opt.size("configuration-filenames") == 1);
2347 :
2348 : // other parameters
2349 1 : CATCH_REQUIRE(opt.get_program_name() == "arguments");
2350 1 : CATCH_REQUIRE(opt.get_program_fullname() == "/usr/bin/arguments");
2351 :
2352 : // process system options now
2353 2 : std::stringstream ss;
2354 1 : advgetopt::flag_t const result(opt.process_system_options(ss));
2355 1 : CATCH_REQUIRE(result == (advgetopt::SYSTEM_OPTION_CONFIGURATION_FILENAMES
2356 : | advgetopt::SYSTEM_OPTION_CONFIG_DIR));
2357 1 : CATCH_REQUIRE(getenv("HOME") != nullptr);
2358 2 : std::string const home(getenv("HOME"));
2359 1 : CATCH_REQUIRE(ss.str() ==
2360 : "Configuration filenames:\n"
2361 : " . .config/file.mdi\n"
2362 : " . .config/unittest.d/50-file.mdi\n"
2363 : " . /etc/snapwebsites/server.conf\n"
2364 : " . /etc/snapwebsites/unittest.d/50-server.conf\n"
2365 : " . " + home + "/.config/advgetopt/snap.conf\n"
2366 : " . /var/lib/advgetopt/snapdb.conf\n"
2367 : " . /var/lib/advgetopt/unittest.d/50-snapdb.conf\n"
2368 : " . /opt/config/snapdb.conf\n"
2369 : " . /opt/config/unittest.d/50-snapdb.conf\n"
2370 : " . .config/snapdb.conf\n"
2371 : " . .config/unittest.d/50-snapdb.conf\n"
2372 : " . /etc/secret/snapdb.conf\n"
2373 : " . /etc/secret/unittest.d/50-snapdb.conf\n"
2374 : " . " + home + "/.config/snapwebsites/snapdb.conf\n"
2375 : );
2376 : }
2377 : CATCH_END_SECTION()
2378 :
2379 8 : CATCH_START_SECTION("Check with the --configuration-filenames system flag without any configuration files")
2380 : {
2381 1 : advgetopt::option const options[] =
2382 : {
2383 : advgetopt::define_option(
2384 : advgetopt::Name("size")
2385 : , advgetopt::ShortName('s')
2386 : , advgetopt::Flags(advgetopt::command_flags<advgetopt::GETOPT_FLAG_REQUIRED>())
2387 : , advgetopt::Help("define the size.")
2388 : , advgetopt::DefaultValue("3101")
2389 : ),
2390 : advgetopt::end_options()
2391 : };
2392 :
2393 1 : advgetopt::options_environment environment_options;
2394 1 : environment_options.f_project_name = "unittest";
2395 1 : environment_options.f_options = options;
2396 1 : environment_options.f_environment_flags = advgetopt::GETOPT_ENVIRONMENT_FLAG_SYSTEM_PARAMETERS;
2397 :
2398 1 : char const * cargv[] =
2399 : {
2400 : "/usr/bin/arguments",
2401 : "--configuration-filenames",
2402 : nullptr
2403 : };
2404 1 : int const argc(sizeof(cargv) / sizeof(cargv[0]) - 1);
2405 1 : char ** argv = const_cast<char **>(cargv);
2406 :
2407 2 : advgetopt::getopt opt(environment_options, argc, argv);
2408 :
2409 : // check that the result is valid
2410 :
2411 : // an invalid parameter, MUST NOT EXIST
2412 1 : CATCH_REQUIRE(opt.get_option("invalid-parameter") == nullptr);
2413 1 : CATCH_REQUIRE(opt.get_option('Z') == nullptr);
2414 1 : CATCH_REQUIRE_FALSE(opt.is_defined("invalid-parameter"));
2415 1 : CATCH_REQUIRE(opt.get_default("invalid-parameter").empty());
2416 1 : CATCH_REQUIRE(opt.size("invalid-parameter") == 0);
2417 :
2418 : // no default
2419 1 : CATCH_REQUIRE(opt.get_option("--") == nullptr);
2420 1 : CATCH_REQUIRE_FALSE(opt.is_defined("--"));
2421 1 : CATCH_REQUIRE(opt.get_default("--").empty());
2422 1 : CATCH_REQUIRE(opt.size("--") == 0);
2423 :
2424 : // valid parameter
2425 1 : CATCH_REQUIRE(opt.get_option("size") != nullptr);
2426 1 : CATCH_REQUIRE(opt.get_option('s') == opt.get_option("size"));
2427 1 : CATCH_REQUIRE_FALSE(opt.is_defined("size"));
2428 1 : CATCH_REQUIRE(opt.get_string("size") == "3101");
2429 1 : CATCH_REQUIRE(opt.get_string("size", 0) == "3101");
2430 1 : CATCH_REQUIRE(opt["size"] == "3101");
2431 1 : CATCH_REQUIRE(opt.get_long("size") == 3101);
2432 1 : CATCH_REQUIRE(opt.get_long("size", 0) == 3101);
2433 1 : CATCH_REQUIRE(opt.has_default("size"));
2434 1 : CATCH_REQUIRE(opt.get_default("size") == "3101");
2435 1 : CATCH_REQUIRE(opt.size("size") == 0);
2436 :
2437 : // configuration-filenames parameter
2438 1 : CATCH_REQUIRE(opt.get_option("configuration-filenames") != nullptr);
2439 1 : CATCH_REQUIRE(opt.is_defined("configuration-filenames"));
2440 1 : CATCH_REQUIRE(opt.get_string("configuration-filenames") == "");
2441 1 : CATCH_REQUIRE(opt.get_string("configuration-filenames", 0) == "");
2442 1 : CATCH_REQUIRE(opt["configuration-filenames"] == "");
2443 1 : CATCH_REQUIRE_FALSE(opt.has_default("configuration-filenames"));
2444 1 : CATCH_REQUIRE(opt.get_default("configuration-filenames").empty());
2445 1 : CATCH_REQUIRE(opt.size("configuration-filenames") == 1);
2446 :
2447 : // other parameters
2448 1 : CATCH_REQUIRE(opt.get_program_name() == "arguments");
2449 1 : CATCH_REQUIRE(opt.get_program_fullname() == "/usr/bin/arguments");
2450 :
2451 : // process system options now
2452 2 : std::stringstream ss;
2453 1 : advgetopt::flag_t const result(opt.process_system_options(ss));
2454 1 : CATCH_REQUIRE(result == advgetopt::SYSTEM_OPTION_CONFIGURATION_FILENAMES);
2455 1 : CATCH_REQUIRE(ss.str() == "unittest does not support configuration files.\n");
2456 : }
2457 : CATCH_END_SECTION()
2458 :
2459 8 : CATCH_START_SECTION("Check with the --configuration-filenames system flag, without a --configuration-filenames on the command line")
2460 1 : advgetopt::option const options[] =
2461 : {
2462 : advgetopt::define_option(
2463 : advgetopt::Name("size")
2464 : , advgetopt::ShortName('s')
2465 : , advgetopt::Flags(advgetopt::command_flags<advgetopt::GETOPT_FLAG_REQUIRED>())
2466 : , advgetopt::Help("define the size.")
2467 : , advgetopt::DefaultValue("193")
2468 : ),
2469 : advgetopt::end_options()
2470 : };
2471 :
2472 1 : char const * confs[] =
2473 : {
2474 : ".config/file.mdi",
2475 : "/etc/snapwebsites/server.conf",
2476 : "~/.config/advgetopt/snap.conf",
2477 : nullptr
2478 : };
2479 1 : char const * dirs[] =
2480 : {
2481 : ".config",
2482 : "/etc/secret",
2483 : "~/.config/snapwebsites",
2484 : nullptr
2485 : };
2486 :
2487 1 : advgetopt::options_environment environment_options;
2488 1 : environment_options.f_project_name = "unittest";
2489 1 : environment_options.f_options = options;
2490 1 : environment_options.f_environment_flags = advgetopt::GETOPT_ENVIRONMENT_FLAG_SYSTEM_PARAMETERS;
2491 1 : environment_options.f_help_header = "Usage: test system commands";
2492 1 : environment_options.f_help_footer = "Copyright matters";
2493 1 : environment_options.f_configuration_files = confs;
2494 1 : environment_options.f_configuration_filename = "snapdb.conf";
2495 1 : environment_options.f_configuration_directories = dirs;
2496 :
2497 1 : char const * cargv[] =
2498 : {
2499 : "/usr/bin/arguments",
2500 : "--size",
2501 : "1221",
2502 : nullptr
2503 : };
2504 1 : int const argc(sizeof(cargv) / sizeof(cargv[0]) - 1);
2505 1 : char ** argv = const_cast<char **>(cargv);
2506 :
2507 2 : advgetopt::getopt opt(environment_options, argc, argv);
2508 :
2509 : // check that the result is valid
2510 :
2511 : // an invalid parameter, MUST NOT EXIST
2512 1 : CATCH_REQUIRE(opt.get_option("invalid-parameter") == nullptr);
2513 1 : CATCH_REQUIRE(opt.get_option('Z') == nullptr);
2514 1 : CATCH_REQUIRE_FALSE(opt.is_defined("invalid-parameter"));
2515 1 : CATCH_REQUIRE(opt.get_default("invalid-parameter").empty());
2516 1 : CATCH_REQUIRE(opt.size("invalid-parameter") == 0);
2517 :
2518 : // no default
2519 1 : CATCH_REQUIRE(opt.get_option("--") == nullptr);
2520 1 : CATCH_REQUIRE_FALSE(opt.is_defined("--"));
2521 1 : CATCH_REQUIRE(opt.get_default("--").empty());
2522 1 : CATCH_REQUIRE(opt.size("--") == 0);
2523 :
2524 : // valid parameter
2525 1 : CATCH_REQUIRE(opt.get_option("size") != nullptr);
2526 1 : CATCH_REQUIRE(opt.get_option('s') == opt.get_option("size"));
2527 1 : CATCH_REQUIRE(opt.is_defined("size"));
2528 1 : CATCH_REQUIRE(opt.get_string("size") == "1221");
2529 1 : CATCH_REQUIRE(opt.get_string("size", 0) == "1221");
2530 1 : CATCH_REQUIRE(opt["size"] == "1221");
2531 1 : CATCH_REQUIRE(opt.get_long("size") == 1221);
2532 1 : CATCH_REQUIRE(opt.get_long("size", 0) == 1221);
2533 1 : CATCH_REQUIRE(opt.has_default("size"));
2534 1 : CATCH_REQUIRE(opt.get_default("size") == "193");
2535 1 : CATCH_REQUIRE(opt.size("size") == 1);
2536 :
2537 : // configuration-filenames parameter
2538 1 : CATCH_REQUIRE(opt.get_option("configuration-filenames") != nullptr);
2539 1 : CATCH_REQUIRE_FALSE(opt.is_defined("configuration-filenames"));
2540 1 : CATCH_REQUIRE_FALSE(opt.has_default("configuration-filenames"));
2541 1 : CATCH_REQUIRE(opt.get_default("configuration-filenames").empty());
2542 1 : CATCH_REQUIRE(opt.size("configuration-filenames") == 0);
2543 :
2544 : // other parameters
2545 1 : CATCH_REQUIRE(opt.get_program_name() == "arguments");
2546 1 : CATCH_REQUIRE(opt.get_program_fullname() == "/usr/bin/arguments");
2547 :
2548 : // process system options now
2549 2 : std::stringstream ss;
2550 1 : advgetopt::flag_t const result(opt.process_system_options(ss));
2551 1 : CATCH_REQUIRE(result == advgetopt::SYSTEM_OPTION_NONE);
2552 1 : CATCH_REQUIRE(ss.str().empty());
2553 : CATCH_END_SECTION()
2554 4 : }
2555 :
2556 :
2557 :
2558 5 : CATCH_TEST_CASE("system_flags_path_to_option_definitions", "[arguments][valid][getopt][system_flags]")
2559 : {
2560 6 : CATCH_START_SECTION("Check with the --path-to-option-definitions system flag (Default)")
2561 : {
2562 1 : advgetopt::option const options[] =
2563 : {
2564 : advgetopt::define_option(
2565 : advgetopt::Name("size")
2566 : , advgetopt::ShortName('s')
2567 : , advgetopt::Flags(advgetopt::command_flags<advgetopt::GETOPT_FLAG_REQUIRED>())
2568 : , advgetopt::Help("define the size.")
2569 : , advgetopt::DefaultValue("7301")
2570 : ),
2571 : advgetopt::end_options()
2572 : };
2573 :
2574 1 : advgetopt::options_environment environment_options;
2575 1 : environment_options.f_project_name = "unittest";
2576 1 : environment_options.f_options = options;
2577 1 : environment_options.f_environment_flags = advgetopt::GETOPT_ENVIRONMENT_FLAG_SYSTEM_PARAMETERS;
2578 :
2579 1 : char const * cargv[] =
2580 : {
2581 : "/usr/bin/arguments",
2582 : "--path-to-option-definitions",
2583 : nullptr
2584 : };
2585 1 : int const argc(sizeof(cargv) / sizeof(cargv[0]) - 1);
2586 1 : char ** argv = const_cast<char **>(cargv);
2587 :
2588 2 : advgetopt::getopt opt(environment_options, argc, argv);
2589 :
2590 : // check that the result is valid
2591 :
2592 : // an invalid parameter, MUST NOT EXIST
2593 1 : CATCH_REQUIRE(opt.get_option("invalid-parameter") == nullptr);
2594 1 : CATCH_REQUIRE(opt.get_option('Z') == nullptr);
2595 1 : CATCH_REQUIRE_FALSE(opt.is_defined("invalid-parameter"));
2596 1 : CATCH_REQUIRE(opt.get_default("invalid-parameter").empty());
2597 1 : CATCH_REQUIRE(opt.size("invalid-parameter") == 0);
2598 :
2599 : // no default
2600 1 : CATCH_REQUIRE(opt.get_option("--") == nullptr);
2601 1 : CATCH_REQUIRE_FALSE(opt.is_defined("--"));
2602 1 : CATCH_REQUIRE(opt.get_default("--").empty());
2603 1 : CATCH_REQUIRE(opt.size("--") == 0);
2604 :
2605 : // valid parameter
2606 1 : CATCH_REQUIRE(opt.get_option("size") != nullptr);
2607 1 : CATCH_REQUIRE(opt.get_option('s') == opt.get_option("size"));
2608 1 : CATCH_REQUIRE_FALSE(opt.is_defined("size"));
2609 1 : CATCH_REQUIRE(opt.get_string("size") == "7301");
2610 1 : CATCH_REQUIRE(opt.get_string("size", 0) == "7301");
2611 1 : CATCH_REQUIRE(opt["size"] == "7301");
2612 1 : CATCH_REQUIRE(opt.get_long("size") == 7301);
2613 1 : CATCH_REQUIRE(opt.get_long("size", 0) == 7301);
2614 1 : CATCH_REQUIRE(opt.has_default("size"));
2615 1 : CATCH_REQUIRE(opt.get_default("size") == "7301");
2616 1 : CATCH_REQUIRE(opt.size("size") == 0);
2617 :
2618 : // path-to-option-definitions parameter
2619 1 : CATCH_REQUIRE(opt.get_option("path-to-option-definitions") != nullptr);
2620 1 : CATCH_REQUIRE(opt.is_defined("path-to-option-definitions"));
2621 1 : CATCH_REQUIRE(opt.get_string("path-to-option-definitions") == "");
2622 1 : CATCH_REQUIRE(opt.get_string("path-to-option-definitions", 0) == "");
2623 1 : CATCH_REQUIRE(opt["path-to-option-definitions"] == "");
2624 1 : CATCH_REQUIRE_FALSE(opt.has_default("path-to-option-definitions"));
2625 1 : CATCH_REQUIRE(opt.get_default("path-to-option-definitions").empty());
2626 1 : CATCH_REQUIRE(opt.size("path-to-option-definitions") == 1);
2627 :
2628 : // other parameters
2629 1 : CATCH_REQUIRE(opt.get_program_name() == "arguments");
2630 1 : CATCH_REQUIRE(opt.get_program_fullname() == "/usr/bin/arguments");
2631 :
2632 : // process system options now
2633 2 : std::stringstream ss;
2634 1 : advgetopt::flag_t const result(opt.process_system_options(ss));
2635 1 : CATCH_REQUIRE(result == advgetopt::SYSTEM_OPTION_PATH_TO_OPTION_DEFINITIONS);
2636 1 : CATCH_REQUIRE(ss.str() == "/usr/share/advgetopt/options/\n");
2637 : }
2638 : CATCH_END_SECTION()
2639 :
2640 6 : CATCH_START_SECTION("Check with the --path-to-option-definitions system flag (Specified)")
2641 : {
2642 1 : advgetopt::option const options[] =
2643 : {
2644 : advgetopt::define_option(
2645 : advgetopt::Name("size")
2646 : , advgetopt::ShortName('s')
2647 : , advgetopt::Flags(advgetopt::command_flags<advgetopt::GETOPT_FLAG_REQUIRED>())
2648 : , advgetopt::Help("define the size.")
2649 : , advgetopt::DefaultValue("7301")
2650 : ),
2651 : advgetopt::end_options()
2652 : };
2653 :
2654 1 : advgetopt::options_environment environment_options;
2655 1 : environment_options.f_project_name = "unittest";
2656 1 : environment_options.f_options = options;
2657 1 : environment_options.f_environment_flags = advgetopt::GETOPT_ENVIRONMENT_FLAG_SYSTEM_PARAMETERS;
2658 1 : environment_options.f_options_files_directory = "/opt/advgetopt/configs";
2659 :
2660 1 : char const * cargv[] =
2661 : {
2662 : "/usr/bin/arguments",
2663 : "--path-to-option-definitions",
2664 : nullptr
2665 : };
2666 1 : int const argc(sizeof(cargv) / sizeof(cargv[0]) - 1);
2667 1 : char ** argv = const_cast<char **>(cargv);
2668 :
2669 2 : advgetopt::getopt opt(environment_options, argc, argv);
2670 :
2671 : // check that the result is valid
2672 :
2673 : // an invalid parameter, MUST NOT EXIST
2674 1 : CATCH_REQUIRE(opt.get_option("invalid-parameter") == nullptr);
2675 1 : CATCH_REQUIRE(opt.get_option('Z') == nullptr);
2676 1 : CATCH_REQUIRE_FALSE(opt.is_defined("invalid-parameter"));
2677 1 : CATCH_REQUIRE(opt.get_default("invalid-parameter").empty());
2678 1 : CATCH_REQUIRE(opt.size("invalid-parameter") == 0);
2679 :
2680 : // no default
2681 1 : CATCH_REQUIRE(opt.get_option("--") == nullptr);
2682 1 : CATCH_REQUIRE_FALSE(opt.is_defined("--"));
2683 1 : CATCH_REQUIRE(opt.get_default("--").empty());
2684 1 : CATCH_REQUIRE(opt.size("--") == 0);
2685 :
2686 : // valid parameter
2687 1 : CATCH_REQUIRE(opt.get_option("size") != nullptr);
2688 1 : CATCH_REQUIRE(opt.get_option('s') == opt.get_option("size"));
2689 1 : CATCH_REQUIRE_FALSE(opt.is_defined("size"));
2690 1 : CATCH_REQUIRE(opt.get_string("size") == "7301");
2691 1 : CATCH_REQUIRE(opt.get_string("size", 0) == "7301");
2692 1 : CATCH_REQUIRE(opt["size"] == "7301");
2693 1 : CATCH_REQUIRE(opt.get_long("size") == 7301);
2694 1 : CATCH_REQUIRE(opt.get_long("size", 0) == 7301);
2695 1 : CATCH_REQUIRE(opt.has_default("size"));
2696 1 : CATCH_REQUIRE(opt.get_default("size") == "7301");
2697 1 : CATCH_REQUIRE(opt.size("size") == 0);
2698 :
2699 : // path-to-option-definitions parameter
2700 1 : CATCH_REQUIRE(opt.get_option("path-to-option-definitions") != nullptr);
2701 1 : CATCH_REQUIRE(opt.is_defined("path-to-option-definitions"));
2702 1 : CATCH_REQUIRE(opt.get_string("path-to-option-definitions") == "");
2703 1 : CATCH_REQUIRE(opt.get_string("path-to-option-definitions", 0) == "");
2704 1 : CATCH_REQUIRE(opt["path-to-option-definitions"] == "");
2705 1 : CATCH_REQUIRE_FALSE(opt.has_default("path-to-option-definitions"));
2706 1 : CATCH_REQUIRE(opt.get_default("path-to-option-definitions").empty());
2707 1 : CATCH_REQUIRE(opt.size("path-to-option-definitions") == 1);
2708 :
2709 : // other parameters
2710 1 : CATCH_REQUIRE(opt.get_program_name() == "arguments");
2711 1 : CATCH_REQUIRE(opt.get_program_fullname() == "/usr/bin/arguments");
2712 :
2713 : // process system options now
2714 2 : std::stringstream ss;
2715 1 : advgetopt::flag_t const result(opt.process_system_options(ss));
2716 1 : CATCH_REQUIRE(result == advgetopt::SYSTEM_OPTION_PATH_TO_OPTION_DEFINITIONS);
2717 1 : CATCH_REQUIRE(ss.str() == "/opt/advgetopt/configs/\n");
2718 : }
2719 : CATCH_END_SECTION()
2720 :
2721 6 : CATCH_START_SECTION("Check with the --path-to-option-definitions system flag, without a --path-to-option-definitions on the command line")
2722 1 : advgetopt::option const options[] =
2723 : {
2724 : advgetopt::define_option(
2725 : advgetopt::Name("size")
2726 : , advgetopt::ShortName('s')
2727 : , advgetopt::Flags(advgetopt::command_flags<advgetopt::GETOPT_FLAG_REQUIRED>())
2728 : , advgetopt::Help("define the size.")
2729 : , advgetopt::DefaultValue("303")
2730 : ),
2731 : advgetopt::end_options()
2732 : };
2733 :
2734 1 : advgetopt::options_environment environment_options;
2735 1 : environment_options.f_project_name = "unittest";
2736 1 : environment_options.f_options = options;
2737 1 : environment_options.f_environment_flags = advgetopt::GETOPT_ENVIRONMENT_FLAG_SYSTEM_PARAMETERS;
2738 1 : environment_options.f_help_header = "Usage: test system commands";
2739 1 : environment_options.f_help_footer = "Copyright matters";
2740 :
2741 1 : char const * cargv[] =
2742 : {
2743 : "/usr/bin/arguments",
2744 : "--size",
2745 : "1919",
2746 : nullptr
2747 : };
2748 1 : int const argc(sizeof(cargv) / sizeof(cargv[0]) - 1);
2749 1 : char ** argv = const_cast<char **>(cargv);
2750 :
2751 2 : advgetopt::getopt opt(environment_options, argc, argv);
2752 :
2753 : // check that the result is valid
2754 :
2755 : // an invalid parameter, MUST NOT EXIST
2756 1 : CATCH_REQUIRE(opt.get_option("invalid-parameter") == nullptr);
2757 1 : CATCH_REQUIRE(opt.get_option('Z') == nullptr);
2758 1 : CATCH_REQUIRE_FALSE(opt.is_defined("invalid-parameter"));
2759 1 : CATCH_REQUIRE(opt.get_default("invalid-parameter").empty());
2760 1 : CATCH_REQUIRE(opt.size("invalid-parameter") == 0);
2761 :
2762 : // no default
2763 1 : CATCH_REQUIRE(opt.get_option("--") == nullptr);
2764 1 : CATCH_REQUIRE_FALSE(opt.is_defined("--"));
2765 1 : CATCH_REQUIRE(opt.get_default("--").empty());
2766 1 : CATCH_REQUIRE(opt.size("--") == 0);
2767 :
2768 : // valid parameter
2769 1 : CATCH_REQUIRE(opt.get_option("size") != nullptr);
2770 1 : CATCH_REQUIRE(opt.get_option('s') == opt.get_option("size"));
2771 1 : CATCH_REQUIRE(opt.is_defined("size"));
2772 1 : CATCH_REQUIRE(opt.get_string("size") == "1919");
2773 1 : CATCH_REQUIRE(opt.get_string("size", 0) == "1919");
2774 1 : CATCH_REQUIRE(opt["size"] == "1919");
2775 1 : CATCH_REQUIRE(opt.get_long("size") == 1919);
2776 1 : CATCH_REQUIRE(opt.get_long("size", 0) == 1919);
2777 1 : CATCH_REQUIRE(opt.has_default("size"));
2778 1 : CATCH_REQUIRE(opt.get_default("size") == "303");
2779 1 : CATCH_REQUIRE(opt.size("size") == 1);
2780 :
2781 : // environment-variable-name parameter
2782 1 : CATCH_REQUIRE(opt.get_option("path-to-option-definitions") != nullptr);
2783 1 : CATCH_REQUIRE_FALSE(opt.is_defined("path-to-option-definitions"));
2784 1 : CATCH_REQUIRE_FALSE(opt.has_default("path-to-option-definitions"));
2785 1 : CATCH_REQUIRE(opt.get_default("path-to-option-definitions").empty());
2786 1 : CATCH_REQUIRE(opt.size("path-to-option-definitions") == 0);
2787 :
2788 : // other parameters
2789 1 : CATCH_REQUIRE(opt.get_program_name() == "arguments");
2790 1 : CATCH_REQUIRE(opt.get_program_fullname() == "/usr/bin/arguments");
2791 :
2792 : // process system options now
2793 2 : std::stringstream ss;
2794 1 : advgetopt::flag_t const result(opt.process_system_options(ss));
2795 1 : CATCH_REQUIRE(result == advgetopt::SYSTEM_OPTION_NONE);
2796 1 : CATCH_REQUIRE(ss.str().empty());
2797 : CATCH_END_SECTION()
2798 3 : }
2799 :
2800 :
2801 :
2802 :
2803 :
2804 :
2805 :
2806 :
2807 6 : CATCH_TEST_CASE("invalid_option_name", "[arguments][invalid][getopt]")
2808 : {
2809 8 : CATCH_START_SECTION("Verify that asking for the string of a non-existant option fails")
2810 1 : advgetopt::options_environment environment_options;
2811 1 : environment_options.f_project_name = "unittest";
2812 1 : environment_options.f_options = nullptr;
2813 1 : environment_options.f_help_header = "Usage: test get_string() functions";
2814 :
2815 2 : advgetopt::getopt opt(environment_options);
2816 :
2817 1 : CATCH_REQUIRE_THROWS_MATCHES(
2818 : opt.get_string("non-existant")
2819 : , advgetopt::getopt_logic_error
2820 : , Catch::Matchers::ExceptionMessage(
2821 : "getopt_logic_error: there is no --non-existant option defined."));
2822 :
2823 1 : CATCH_REQUIRE_THROWS_MATCHES(
2824 : opt.get_string("non-existant", 0)
2825 : , advgetopt::getopt_logic_error
2826 : , Catch::Matchers::ExceptionMessage(
2827 : "getopt_logic_error: there is no --non-existant option defined."));
2828 :
2829 1 : CATCH_REQUIRE_THROWS_MATCHES(
2830 : opt.get_string("non-existant", 1)
2831 : , advgetopt::getopt_logic_error
2832 : , Catch::Matchers::ExceptionMessage(
2833 : "getopt_logic_error: there is no --non-existant option defined."));
2834 : CATCH_END_SECTION()
2835 :
2836 8 : CATCH_START_SECTION("Verify that asking for the long of a non-existant option fails")
2837 1 : advgetopt::options_environment environment_options;
2838 1 : environment_options.f_project_name = "unittest";
2839 1 : environment_options.f_options = nullptr;
2840 1 : environment_options.f_help_header = "Usage: test get_string() functions";
2841 :
2842 2 : advgetopt::getopt opt(environment_options);
2843 :
2844 1 : CATCH_REQUIRE_THROWS_MATCHES(
2845 : opt.get_long("non-existant")
2846 : , advgetopt::getopt_logic_error
2847 : , Catch::Matchers::ExceptionMessage(
2848 : "getopt_logic_error: there is no --non-existant option defined."));
2849 :
2850 1 : CATCH_REQUIRE_THROWS_MATCHES(
2851 : opt.get_long("non-existant", 0)
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_long("non-existant", 1)
2858 : , advgetopt::getopt_logic_error
2859 : , Catch::Matchers::ExceptionMessage(
2860 : "getopt_logic_error: there is no --non-existant option defined."));
2861 : CATCH_END_SECTION()
2862 :
2863 8 : CATCH_START_SECTION("Verify that asking for a default with an empty string fails")
2864 1 : advgetopt::options_environment environment_options;
2865 1 : environment_options.f_project_name = "unittest";
2866 1 : environment_options.f_options = nullptr;
2867 1 : environment_options.f_help_header = "Usage: test get_default() functions";
2868 :
2869 2 : advgetopt::getopt opt(environment_options);
2870 :
2871 1 : CATCH_REQUIRE_THROWS_MATCHES(
2872 : opt.has_default("")
2873 : , advgetopt::getopt_logic_error
2874 : , Catch::Matchers::ExceptionMessage(
2875 : "getopt_logic_error: argument name cannot be empty."));
2876 :
2877 1 : CATCH_REQUIRE_THROWS_MATCHES(
2878 : opt.has_default(std::string())
2879 : , advgetopt::getopt_logic_error
2880 : , Catch::Matchers::ExceptionMessage(
2881 : "getopt_logic_error: argument name cannot be empty."));
2882 :
2883 1 : CATCH_REQUIRE_THROWS_MATCHES(
2884 : opt.get_default("")
2885 : , advgetopt::getopt_logic_error
2886 : , Catch::Matchers::ExceptionMessage(
2887 : "getopt_logic_error: argument name cannot be empty."));
2888 :
2889 1 : CATCH_REQUIRE_THROWS_MATCHES(
2890 : opt.get_default(std::string())
2891 : , advgetopt::getopt_logic_error
2892 : , Catch::Matchers::ExceptionMessage(
2893 : "getopt_logic_error: argument name cannot be empty."));
2894 : CATCH_END_SECTION()
2895 :
2896 8 : CATCH_START_SECTION("[] operators want a valid name")
2897 1 : advgetopt::options_environment environment_options;
2898 1 : environment_options.f_project_name = "unittest";
2899 1 : environment_options.f_options = nullptr;
2900 1 : environment_options.f_help_header = "Usage: test get_default() functions";
2901 :
2902 2 : advgetopt::getopt opt(environment_options);
2903 :
2904 1 : CATCH_REQUIRE_THROWS_MATCHES(
2905 : opt[""]
2906 : , advgetopt::getopt_logic_error
2907 : , Catch::Matchers::ExceptionMessage(
2908 : "getopt_logic_error: argument name cannot be empty."));
2909 :
2910 1 : CATCH_REQUIRE_THROWS_MATCHES(
2911 : opt[std::string()]
2912 : , advgetopt::getopt_logic_error
2913 : , Catch::Matchers::ExceptionMessage(
2914 : "getopt_logic_error: argument name cannot be empty."));
2915 :
2916 1 : CATCH_REQUIRE_THROWS_MATCHES(
2917 : opt["g"]
2918 : , advgetopt::getopt_logic_error
2919 : , Catch::Matchers::ExceptionMessage(
2920 : "getopt_logic_error: argument name cannot be one letter if it does not exist in operator []."));
2921 :
2922 1 : CATCH_REQUIRE_THROWS_MATCHES(
2923 : opt[std::string("g")]
2924 : , advgetopt::getopt_logic_error
2925 : , Catch::Matchers::ExceptionMessage(
2926 : "getopt_logic_error: argument name cannot be one letter if it does not exist in operator []."));
2927 :
2928 1 : advgetopt::getopt const & const_opt(opt);
2929 :
2930 1 : CATCH_REQUIRE_THROWS_MATCHES(
2931 : const_opt[""]
2932 : , advgetopt::getopt_logic_error
2933 : , Catch::Matchers::ExceptionMessage(
2934 : "getopt_logic_error: argument name cannot be empty."));
2935 :
2936 1 : CATCH_REQUIRE_THROWS_MATCHES(
2937 : const_opt[std::string()]
2938 : , advgetopt::getopt_logic_error
2939 : , Catch::Matchers::ExceptionMessage(
2940 : "getopt_logic_error: argument name cannot be empty."));
2941 : CATCH_END_SECTION()
2942 4 : }
2943 :
2944 :
2945 :
2946 5 : CATCH_TEST_CASE("missing_default_value", "[arguments][invalid][getopt]")
2947 : {
2948 6 : CATCH_START_SECTION("Verify a string value without arguments and no default")
2949 1 : advgetopt::option const options[] =
2950 : {
2951 : advgetopt::define_option(
2952 : advgetopt::Name("size")
2953 : , advgetopt::ShortName('s')
2954 : , advgetopt::Flags(advgetopt::command_flags<advgetopt::GETOPT_FLAG_REQUIRED>())
2955 : , advgetopt::Help("define the size.")
2956 : ),
2957 : advgetopt::end_options()
2958 : };
2959 :
2960 1 : advgetopt::options_environment environment_options;
2961 1 : environment_options.f_project_name = "unittest";
2962 1 : environment_options.f_options = options;
2963 1 : environment_options.f_help_header = "Usage: test get_string() functions";
2964 :
2965 1 : char const * cargv[] =
2966 : {
2967 : "/usr/bin/arguments",
2968 : nullptr
2969 : };
2970 1 : int const argc(sizeof(cargv) / sizeof(cargv[0]) - 1);
2971 1 : char ** argv = const_cast<char **>(cargv);
2972 :
2973 2 : advgetopt::getopt opt(environment_options, argc, argv);
2974 :
2975 : // check that the result is valid
2976 :
2977 : // an invalid parameter, MUST NOT EXIST
2978 1 : CATCH_REQUIRE(opt.get_option("invalid-parameter") == nullptr);
2979 1 : CATCH_REQUIRE(opt.get_option('Z') == nullptr);
2980 1 : CATCH_REQUIRE_FALSE(opt.is_defined("invalid-parameter"));
2981 1 : CATCH_REQUIRE(opt.get_default("invalid-parameter").empty());
2982 1 : CATCH_REQUIRE(opt.size("invalid-parameter") == 0);
2983 :
2984 : // no default
2985 1 : CATCH_REQUIRE(opt.get_option("--") == nullptr);
2986 1 : CATCH_REQUIRE_FALSE(opt.is_defined("--"));
2987 1 : CATCH_REQUIRE(opt.get_default("--").empty());
2988 1 : CATCH_REQUIRE(opt.size("--") == 0);
2989 :
2990 : // the valid parameter
2991 1 : CATCH_REQUIRE(opt.get_option("size") != nullptr);
2992 1 : CATCH_REQUIRE(opt.get_option('s') != nullptr);
2993 1 : CATCH_REQUIRE_FALSE(opt.is_defined("size"));
2994 1 : CATCH_REQUIRE(opt.size("size") == 0);
2995 1 : CATCH_REQUIRE(static_cast<advgetopt::getopt const &>(opt)["size"].empty());
2996 :
2997 1 : CATCH_REQUIRE_THROWS_MATCHES(
2998 : opt.get_string("size")
2999 : , advgetopt::getopt_logic_error
3000 : , Catch::Matchers::ExceptionMessage(
3001 : "getopt_logic_error: the --size option was not defined on the command line and it has no default."));
3002 :
3003 1 : CATCH_REQUIRE_THROWS_MATCHES(
3004 : opt.get_string("size", 0)
3005 : , advgetopt::getopt_logic_error
3006 : , Catch::Matchers::ExceptionMessage(
3007 : "getopt_logic_error: the --size option was not defined on the command line and it has no default."));
3008 :
3009 1 : CATCH_REQUIRE_THROWS_MATCHES(
3010 : opt.get_string("size", 1)
3011 : , advgetopt::getopt_logic_error
3012 : , Catch::Matchers::ExceptionMessage(
3013 : "getopt_logic_error: the --size option was not defined on the command line and it has no default."));
3014 :
3015 : // these do not create an entry (even though it looks like it,
3016 : // i.e. it would for an std::map)
3017 : //
3018 1 : CATCH_REQUIRE(opt["size"].empty());
3019 1 : CATCH_REQUIRE(opt["size"].length() == 0);
3020 1 : CATCH_REQUIRE(opt["size"].size() == 0);
3021 :
3022 1 : CATCH_REQUIRE(opt.size("size") == 0);
3023 :
3024 1 : CATCH_REQUIRE_THROWS_MATCHES(
3025 : opt.get_string("size", 0)
3026 : , advgetopt::getopt_logic_error
3027 : , Catch::Matchers::ExceptionMessage(
3028 : "getopt_logic_error: the --size option was not defined on the command line and it has no default."));
3029 :
3030 1 : CATCH_REQUIRE_THROWS_MATCHES(
3031 : opt.get_string("size", 1)
3032 : , advgetopt::getopt_logic_error
3033 : , Catch::Matchers::ExceptionMessage(
3034 : "getopt_logic_error: the --size option was not defined on the command line and it has no default."));
3035 :
3036 : // now this one does create a value
3037 : //
3038 1 : opt["size"] = "45.3";
3039 :
3040 1 : CATCH_REQUIRE(opt.get_string("size") == "45.3");
3041 1 : CATCH_REQUIRE(opt.get_string("size", 0) == "45.3");
3042 :
3043 1 : CATCH_REQUIRE_THROWS_MATCHES(
3044 : opt.get_string("size", 1)
3045 : , advgetopt::getopt_undefined
3046 : , Catch::Matchers::ExceptionMessage(
3047 : "getopt_exception: option_info::get_value(): no value at index 1 (idx >= 1) for --size so you can't get this value."));
3048 :
3049 : // other parameters
3050 1 : CATCH_REQUIRE(opt.get_program_name() == "arguments");
3051 1 : CATCH_REQUIRE(opt.get_program_fullname() == "/usr/bin/arguments");
3052 : CATCH_END_SECTION()
3053 :
3054 6 : CATCH_START_SECTION("Verify an integer (long) value without arguments and no default")
3055 1 : advgetopt::option const options[] =
3056 : {
3057 : advgetopt::define_option(
3058 : advgetopt::Name("size")
3059 : , advgetopt::ShortName('s')
3060 : , advgetopt::Flags(advgetopt::command_flags<advgetopt::GETOPT_FLAG_REQUIRED>())
3061 : , advgetopt::Help("define the size.")
3062 : ),
3063 : advgetopt::end_options()
3064 : };
3065 :
3066 1 : advgetopt::options_environment environment_options;
3067 1 : environment_options.f_project_name = "unittest";
3068 1 : environment_options.f_options = options;
3069 1 : environment_options.f_help_header = "Usage: test get_long() functions";
3070 :
3071 1 : char const * cargv[] =
3072 : {
3073 : "/usr/bin/arguments",
3074 : nullptr
3075 : };
3076 1 : int const argc(sizeof(cargv) / sizeof(cargv[0]) - 1);
3077 1 : char ** argv = const_cast<char **>(cargv);
3078 :
3079 2 : advgetopt::getopt opt(environment_options, argc, argv);
3080 :
3081 : // check that the result is valid
3082 :
3083 : // an invalid parameter, MUST NOT EXIST
3084 1 : CATCH_REQUIRE(opt.get_option("invalid-parameter") == nullptr);
3085 1 : CATCH_REQUIRE(opt.get_option('Z') == nullptr);
3086 1 : CATCH_REQUIRE_FALSE(opt.is_defined("invalid-parameter"));
3087 1 : CATCH_REQUIRE(opt.get_default("invalid-parameter").empty());
3088 1 : CATCH_REQUIRE(opt.size("invalid-parameter") == 0);
3089 :
3090 : // no default
3091 1 : CATCH_REQUIRE(opt.get_option("--") == nullptr);
3092 1 : CATCH_REQUIRE_FALSE(opt.is_defined("--"));
3093 1 : CATCH_REQUIRE(opt.get_default("--").empty());
3094 1 : CATCH_REQUIRE(opt.size("--") == 0);
3095 :
3096 : // the valid parameter
3097 1 : CATCH_REQUIRE(opt.get_option("size") != nullptr);
3098 1 : CATCH_REQUIRE(opt.get_option('s') != nullptr);
3099 1 : CATCH_REQUIRE_FALSE(opt.is_defined("size"));
3100 1 : CATCH_REQUIRE_FALSE(opt.has_default("size"));
3101 1 : CATCH_REQUIRE(opt.size("size") == 0);
3102 :
3103 1 : CATCH_REQUIRE_THROWS_MATCHES(
3104 : opt.get_long("size")
3105 : , advgetopt::getopt_logic_error
3106 : , Catch::Matchers::ExceptionMessage(
3107 : "getopt_logic_error: the --size option was not defined on the command line and it has no or an empty default."));
3108 :
3109 1 : CATCH_REQUIRE_THROWS_MATCHES(
3110 : opt.get_long("size", 0)
3111 : , advgetopt::getopt_logic_error
3112 : , Catch::Matchers::ExceptionMessage(
3113 : "getopt_logic_error: the --size option was not defined on the command line and it has no or an empty default."));
3114 :
3115 1 : CATCH_REQUIRE_THROWS_MATCHES(
3116 : opt.get_long("size", 1)
3117 : , advgetopt::getopt_logic_error
3118 : , Catch::Matchers::ExceptionMessage(
3119 : "getopt_logic_error: the --size option was not defined on the command line and it has no or an empty default."));
3120 :
3121 : // other parameters
3122 1 : CATCH_REQUIRE(opt.get_program_name() == "arguments");
3123 1 : CATCH_REQUIRE(opt.get_program_fullname() == "/usr/bin/arguments");
3124 : CATCH_END_SECTION()
3125 :
3126 6 : CATCH_START_SECTION("Verify an integer (long) value without arguments and an empty string as default")
3127 1 : advgetopt::option const options[] =
3128 : {
3129 : advgetopt::define_option(
3130 : advgetopt::Name("size")
3131 : , advgetopt::ShortName('s')
3132 : , advgetopt::Flags(advgetopt::command_flags<advgetopt::GETOPT_FLAG_REQUIRED>())
3133 : , advgetopt::Help("define the size.")
3134 : , advgetopt::DefaultValue("")
3135 : ),
3136 : advgetopt::end_options()
3137 : };
3138 :
3139 1 : advgetopt::options_environment environment_options;
3140 1 : environment_options.f_project_name = "unittest";
3141 1 : environment_options.f_options = options;
3142 1 : environment_options.f_help_header = "Usage: test get_long() functions";
3143 :
3144 1 : char const * cargv[] =
3145 : {
3146 : "/usr/bin/arguments",
3147 : nullptr
3148 : };
3149 1 : int const argc(sizeof(cargv) / sizeof(cargv[0]) - 1);
3150 1 : char ** argv = const_cast<char **>(cargv);
3151 :
3152 2 : advgetopt::getopt opt(environment_options, argc, argv);
3153 :
3154 : // check that the result is valid
3155 :
3156 : // an invalid parameter, MUST NOT EXIST
3157 1 : CATCH_REQUIRE(opt.get_option("invalid-parameter") == nullptr);
3158 1 : CATCH_REQUIRE(opt.get_option('Z') == nullptr);
3159 1 : CATCH_REQUIRE_FALSE(opt.is_defined("invalid-parameter"));
3160 1 : CATCH_REQUIRE(opt.get_default("invalid-parameter").empty());
3161 1 : CATCH_REQUIRE(opt.size("invalid-parameter") == 0);
3162 :
3163 : // no default
3164 1 : CATCH_REQUIRE(opt.get_option("--") == nullptr);
3165 1 : CATCH_REQUIRE_FALSE(opt.is_defined("--"));
3166 1 : CATCH_REQUIRE(opt.get_default("--").empty());
3167 1 : CATCH_REQUIRE(opt.size("--") == 0);
3168 :
3169 : // the valid parameter
3170 1 : CATCH_REQUIRE(opt.get_option("size") != nullptr);
3171 1 : CATCH_REQUIRE(opt.get_option('s') != nullptr);
3172 1 : CATCH_REQUIRE_FALSE(opt.is_defined("size"));
3173 1 : CATCH_REQUIRE(opt.has_default("size"));
3174 1 : CATCH_REQUIRE(opt.size("size") == 0);
3175 :
3176 1 : CATCH_REQUIRE_THROWS_MATCHES(
3177 : opt.get_long("size")
3178 : , advgetopt::getopt_logic_error
3179 : , Catch::Matchers::ExceptionMessage(
3180 : "getopt_logic_error: the --size option was not defined on the command line and it has no or an empty default."));
3181 :
3182 1 : CATCH_REQUIRE_THROWS_MATCHES(
3183 : opt.get_long("size", 0)
3184 : , advgetopt::getopt_logic_error
3185 : , Catch::Matchers::ExceptionMessage(
3186 : "getopt_logic_error: the --size option was not defined on the command line and it has no or an empty default."));
3187 :
3188 1 : CATCH_REQUIRE_THROWS_MATCHES(
3189 : opt.get_long("size", 1)
3190 : , advgetopt::getopt_logic_error
3191 : , Catch::Matchers::ExceptionMessage(
3192 : "getopt_logic_error: the --size option was not defined on the command line and it has no or an empty default."));
3193 :
3194 : // other parameters
3195 1 : CATCH_REQUIRE(opt.get_program_name() == "arguments");
3196 1 : CATCH_REQUIRE(opt.get_program_fullname() == "/usr/bin/arguments");
3197 : CATCH_END_SECTION()
3198 3 : }
3199 :
3200 :
3201 :
3202 3 : CATCH_TEST_CASE("incompatible_default_value", "[arguments][invalid][getopt]")
3203 : {
3204 2 : CATCH_START_SECTION("Verify an integer (long) value without arguments and a non-numeric default")
3205 1 : advgetopt::option const options[] =
3206 : {
3207 : advgetopt::define_option(
3208 : advgetopt::Name("size")
3209 : , advgetopt::ShortName('s')
3210 : , advgetopt::Flags(advgetopt::command_flags<advgetopt::GETOPT_FLAG_REQUIRED>())
3211 : , advgetopt::Help("define the size.")
3212 : , advgetopt::DefaultValue("undefined")
3213 : ),
3214 : advgetopt::end_options()
3215 : };
3216 :
3217 1 : advgetopt::options_environment environment_options;
3218 1 : environment_options.f_project_name = "unittest";
3219 1 : environment_options.f_options = options;
3220 1 : environment_options.f_help_header = "Usage: test get_long() functions";
3221 :
3222 1 : char const * cargv[] =
3223 : {
3224 : "/usr/bin/arguments",
3225 : nullptr
3226 : };
3227 1 : int const argc(sizeof(cargv) / sizeof(cargv[0]) - 1);
3228 1 : char ** argv = const_cast<char **>(cargv);
3229 :
3230 2 : advgetopt::getopt opt(environment_options, argc, argv);
3231 :
3232 : // check that the result is valid
3233 :
3234 : // an invalid parameter, MUST NOT EXIST
3235 1 : CATCH_REQUIRE(opt.get_option("invalid-parameter") == nullptr);
3236 1 : CATCH_REQUIRE(opt.get_option('Z') == nullptr);
3237 1 : CATCH_REQUIRE_FALSE(opt.is_defined("invalid-parameter"));
3238 1 : CATCH_REQUIRE(opt.get_default("invalid-parameter").empty());
3239 1 : CATCH_REQUIRE(opt.size("invalid-parameter") == 0);
3240 :
3241 : // no default
3242 1 : CATCH_REQUIRE(opt.get_option("--") == nullptr);
3243 1 : CATCH_REQUIRE_FALSE(opt.is_defined("--"));
3244 1 : CATCH_REQUIRE(opt.get_default("--").empty());
3245 1 : CATCH_REQUIRE(opt.size("--") == 0);
3246 :
3247 : // the valid parameter
3248 1 : CATCH_REQUIRE(opt.get_option("size") != nullptr);
3249 1 : CATCH_REQUIRE(opt.get_option('s') != nullptr);
3250 1 : CATCH_REQUIRE_FALSE(opt.is_defined("size"));
3251 1 : CATCH_REQUIRE(opt.has_default("size"));
3252 1 : CATCH_REQUIRE(opt.get_default("size") == "undefined"); // this works, it fails with get_long() though
3253 1 : CATCH_REQUIRE(opt.size("size") == 0);
3254 :
3255 1 : CATCH_REQUIRE_THROWS_MATCHES(
3256 : opt.get_long("size")
3257 : , advgetopt::getopt_logic_error
3258 : , Catch::Matchers::ExceptionMessage(
3259 : "getopt_logic_error: invalid default number \"undefined\" for option --size"));
3260 :
3261 1 : CATCH_REQUIRE_THROWS_MATCHES(
3262 : opt.get_long("size", 0)
3263 : , advgetopt::getopt_logic_error
3264 : , Catch::Matchers::ExceptionMessage(
3265 : "getopt_logic_error: invalid default number \"undefined\" for option --size"));
3266 :
3267 1 : CATCH_REQUIRE_THROWS_MATCHES(
3268 : opt.get_long("size", 1)
3269 : , advgetopt::getopt_logic_error
3270 : , Catch::Matchers::ExceptionMessage(
3271 : "getopt_logic_error: invalid default number \"undefined\" for option --size"));
3272 :
3273 : // other parameters
3274 1 : CATCH_REQUIRE(opt.get_program_name() == "arguments");
3275 1 : CATCH_REQUIRE(opt.get_program_fullname() == "/usr/bin/arguments");
3276 : CATCH_END_SECTION()
3277 1 : }
3278 :
3279 :
3280 :
3281 4 : CATCH_TEST_CASE("out_of_range_value", "[arguments][invalid][getopt]")
3282 : {
3283 4 : CATCH_START_SECTION("Verify an integer (long) value without arguments and a non-numeric default")
3284 1 : advgetopt::option const options[] =
3285 : {
3286 : advgetopt::define_option(
3287 : advgetopt::Name("size")
3288 : , advgetopt::ShortName('s')
3289 : , advgetopt::Flags(advgetopt::command_flags<advgetopt::GETOPT_FLAG_REQUIRED>())
3290 : , advgetopt::Help("define the size.")
3291 : , advgetopt::DefaultValue("-300")
3292 : ),
3293 : advgetopt::end_options()
3294 : };
3295 :
3296 1 : advgetopt::options_environment environment_options;
3297 1 : environment_options.f_project_name = "unittest";
3298 1 : environment_options.f_options = options;
3299 1 : environment_options.f_help_header = "Usage: test get_long() functions";
3300 :
3301 1 : char const * cargv[] =
3302 : {
3303 : "/usr/bin/arguments",
3304 : "--size",
3305 : "312",
3306 : nullptr
3307 : };
3308 1 : int const argc(sizeof(cargv) / sizeof(cargv[0]) - 1);
3309 1 : char ** argv = const_cast<char **>(cargv);
3310 :
3311 2 : advgetopt::getopt opt(environment_options, argc, argv);
3312 :
3313 : // check that the result is valid
3314 :
3315 : // an invalid parameter, MUST NOT EXIST
3316 1 : CATCH_REQUIRE(opt.get_option("invalid-parameter") == nullptr);
3317 1 : CATCH_REQUIRE(opt.get_option('Z') == nullptr);
3318 1 : CATCH_REQUIRE_FALSE(opt.is_defined("invalid-parameter"));
3319 1 : CATCH_REQUIRE(opt.get_default("invalid-parameter").empty());
3320 1 : CATCH_REQUIRE(opt.size("invalid-parameter") == 0);
3321 :
3322 : // no default
3323 1 : CATCH_REQUIRE(opt.get_option("--") == nullptr);
3324 1 : CATCH_REQUIRE_FALSE(opt.is_defined("--"));
3325 1 : CATCH_REQUIRE(opt.get_default("--").empty());
3326 1 : CATCH_REQUIRE(opt.size("--") == 0);
3327 :
3328 : // the valid parameter
3329 1 : CATCH_REQUIRE(opt.get_option("size") != nullptr);
3330 1 : CATCH_REQUIRE(opt.get_option('s') != nullptr);
3331 1 : CATCH_REQUIRE(opt.is_defined("size"));
3332 1 : CATCH_REQUIRE(opt.get_string("size") == "312");
3333 1 : CATCH_REQUIRE(opt.get_string("size", 0) == "312");
3334 1 : CATCH_REQUIRE(opt["size"] == "312");
3335 1 : CATCH_REQUIRE(opt.get_long("size") == 312);
3336 1 : CATCH_REQUIRE(opt.get_long("size", 0) == 312);
3337 1 : CATCH_REQUIRE(opt.get_default("size") == "-300");
3338 1 : CATCH_REQUIRE(opt.size("size") == 1);
3339 :
3340 1 : SNAP_CATCH2_NAMESPACE::push_expected_log("error: 312 is out of bounds (-100..100 inclusive) in parameter --size.");
3341 1 : CATCH_REQUIRE(opt.get_long("size", 0, -100, 100) == -1);
3342 1 : SNAP_CATCH2_NAMESPACE::expected_logs_stack_is_empty();
3343 :
3344 : // other parameters
3345 1 : CATCH_REQUIRE(opt.get_program_name() == "arguments");
3346 1 : CATCH_REQUIRE(opt.get_program_fullname() == "/usr/bin/arguments");
3347 : CATCH_END_SECTION()
3348 :
3349 4 : CATCH_START_SECTION("Verify an integer (long) value without arguments and a non-numeric default")
3350 1 : advgetopt::option const options[] =
3351 : {
3352 : advgetopt::define_option(
3353 : advgetopt::Name("size")
3354 : , advgetopt::ShortName('s')
3355 : , advgetopt::Flags(advgetopt::command_flags<advgetopt::GETOPT_FLAG_REQUIRED>())
3356 : , advgetopt::Help("define the size.")
3357 : , advgetopt::DefaultValue("-300")
3358 : ),
3359 : advgetopt::end_options()
3360 : };
3361 :
3362 1 : advgetopt::options_environment environment_options;
3363 1 : environment_options.f_project_name = "unittest";
3364 1 : environment_options.f_options = options;
3365 1 : environment_options.f_help_header = "Usage: test get_long() functions";
3366 :
3367 1 : char const * cargv[] =
3368 : {
3369 : "/usr/bin/arguments",
3370 : nullptr
3371 : };
3372 1 : int const argc(sizeof(cargv) / sizeof(cargv[0]) - 1);
3373 1 : char ** argv = const_cast<char **>(cargv);
3374 :
3375 2 : advgetopt::getopt opt(environment_options, argc, argv);
3376 :
3377 : // check that the result is valid
3378 :
3379 : // an invalid parameter, MUST NOT EXIST
3380 1 : CATCH_REQUIRE(opt.get_option("invalid-parameter") == nullptr);
3381 1 : CATCH_REQUIRE(opt.get_option('Z') == nullptr);
3382 1 : CATCH_REQUIRE_FALSE(opt.is_defined("invalid-parameter"));
3383 1 : CATCH_REQUIRE(opt.get_default("invalid-parameter").empty());
3384 1 : CATCH_REQUIRE(opt.size("invalid-parameter") == 0);
3385 :
3386 : // no default
3387 1 : CATCH_REQUIRE(opt.get_option("--") == nullptr);
3388 1 : CATCH_REQUIRE_FALSE(opt.is_defined("--"));
3389 1 : CATCH_REQUIRE(opt.get_default("--").empty());
3390 1 : CATCH_REQUIRE(opt.size("--") == 0);
3391 :
3392 : // the valid parameter
3393 1 : CATCH_REQUIRE(opt.get_option("size") != nullptr);
3394 1 : CATCH_REQUIRE(opt.get_option('s') != nullptr);
3395 1 : CATCH_REQUIRE_FALSE(opt.is_defined("size"));
3396 1 : CATCH_REQUIRE(opt.get_default("size") == "-300");
3397 1 : CATCH_REQUIRE(opt.size("size") == 0);
3398 :
3399 1 : SNAP_CATCH2_NAMESPACE::push_expected_log("error: -300 is out of bounds (-100..100 inclusive) in parameter --size.");
3400 1 : CATCH_REQUIRE(opt.get_long("size", 0, -100, 100) == -1);
3401 1 : SNAP_CATCH2_NAMESPACE::expected_logs_stack_is_empty();
3402 :
3403 : // other parameters
3404 1 : CATCH_REQUIRE(opt.get_program_name() == "arguments");
3405 1 : CATCH_REQUIRE(opt.get_program_fullname() == "/usr/bin/arguments");
3406 : CATCH_END_SECTION()
3407 8 : }
3408 :
3409 :
3410 : // vim: ts=4 sw=4 et
|