Line data Source code
1 : // Copyright (c) 2006-2021 Made to Order Software Corp. All Rights Reserved
2 : //
3 : // https://snapwebsites.org/project/advgetopt
4 : // contact@m2osw.com
5 : //
6 : // This program is free software; you can redistribute it and/or modify
7 : // it under the terms of the GNU General Public License as published by
8 : // the Free Software Foundation; either version 2 of the License, or
9 : // (at your option) any later version.
10 : //
11 : // This program is distributed in the hope that it will be useful,
12 : // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 : // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 : // GNU General Public License for more details.
15 : //
16 : // You should have received a copy of the GNU General Public License along
17 : // with this program; if not, write to the Free Software Foundation, Inc.,
18 : // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 :
20 : /** \file
21 : * \brief Advanced getopt implementation.
22 : *
23 : * The advgetopt class and implementation is an advanced library to parse
24 : * command line parameters from static definitions specified by the caller.
25 : *
26 : * The class supports the command line options, options found in a default
27 : * configuration file or in a user defined configuration file.
28 : *
29 : * The class also includes support for displaying error messages and help
30 : * information about all the command line arguments.
31 : */
32 :
33 :
34 : /** \mainpage
35 : * The advanced getopt library to handle your command line tools seemlessly.
36 : *
37 : * The library offers an advanced way to parse command line arguments,
38 : * an environment variable, and configuration files in a seamless manner.
39 : * The advgetopt::getopt class is what is used everywhere for that purpose.
40 : *
41 : * The class can be used in a very restrictive mode, meaning that all
42 : * the parameters must clearly be defined by the application. It can also
43 : * be used in a dynamic way where the parameters are dynamically added
44 : * to the list of available options.
45 : *
46 : * The library supports multiple levels in your options. The simplest
47 : * is to use a scope operator like so:
48 : *
49 : * \code
50 : * level1::level2::level3::etc = 123
51 : * \endcode
52 : *
53 : * \note
54 : * The library understands the scope operator (::), the period (.), and
55 : * the slash (/) as level separator. So the following are equivalent.
56 : * Internally, all are changed to the scope operator (::).
57 : *
58 : * \code
59 : * level1::level2::level3::etc = 123
60 : * level1.level2.level3.etc = 123
61 : * level1/level2/level3/etc = 123
62 : * \endcode
63 : *
64 : * The labels in a .ini format (i.e. `[name]` defines sections) are viewed
65 : * as a first level. That name automatically get prepended to the parameters
66 : * appearing under them. Additional levels can be added by using the
67 : * scope operator, again.
68 : *
69 : * \code
70 : * [level1]
71 : * level2::level3::etc = 123
72 : * \endcode
73 : *
74 : * Support for other formats may be added later. For example, we could
75 : * read XML and JSON files. Right now, we are focused on Unix configuration
76 : * files and pretty much never even need two levels.
77 : *
78 : * The library is capable of writing your configuration back to file. It
79 : * will know when a value was modified and only the modified values get
80 : * saved to the read/write configuration file(s). (Either the user file
81 : * or the system file under the `\<proc>.d/...` sub-path.)
82 : */
83 :
84 : // self
85 : //
86 : #include "advgetopt/advgetopt.h"
87 :
88 :
89 : // advgetopt lib
90 : //
91 : #include "advgetopt/exception.h"
92 :
93 :
94 : // cppthread lib
95 : //
96 : #include <cppthread/log.h>
97 :
98 :
99 : // libutf8 lib
100 : //
101 : #include <libutf8/iterator.h>
102 :
103 :
104 : // boost lib
105 : //
106 : #include <boost/algorithm/string/replace.hpp>
107 :
108 :
109 : // last include
110 : //
111 : #include <snapdev/poison.h>
112 :
113 :
114 :
115 :
116 :
117 : /** \brief The advgetopt environment to parse command line options.
118 : *
119 : * This namespace encompasses all the declarations and implementations
120 : * of functions used to parse and access the command line options.
121 : */
122 : namespace advgetopt
123 : {
124 :
125 :
126 : namespace
127 : {
128 :
129 :
130 : /** \brief Definitions of the system options.
131 : *
132 : * The system options are options we add automatically (if the user asked
133 : * for them) and handle automatically when they are found.
134 : *
135 : * The following are the currently supported system options:
136 : *
137 : * \li `--help`
138 : *
139 : * Print out the usage() with most of the command line arguments.
140 : *
141 : * \li '--long-help'
142 : *
143 : * Print all the command line arguments with usage().
144 : *
145 : * The long help is is only added if the list of options include at least
146 : * one group flag (GETOPT_FLAG_SHOW_GROUP1 or GETOPT_FLAG_SHOW_GROUP2).
147 : * See the getopt::parse_options_from_group_names() in advgetopt_usage.cpp.
148 : *
149 : * \li '--\<name>-help'
150 : *
151 : * Print the help from the group named \<name>.
152 : *
153 : * These command line options are added only when groups are defined.
154 : *
155 : * \li `--version`
156 : *
157 : * Print out the version.
158 : *
159 : * \li `--copyright`
160 : *
161 : * Print out the copyright notice.
162 : *
163 : * \li `--license`
164 : *
165 : * Print out the license notice.
166 : *
167 : * \li `--build-date`
168 : *
169 : * Print out the build time and date.
170 : *
171 : * \li `--environment-variable-name`
172 : *
173 : * Print out the build time and date.
174 : *
175 : * \li `--configuration-filenames`
176 : *
177 : * Print out the list of configuration file names that the system checks
178 : * for configuration data.
179 : *
180 : * \li `--path-to-option-definitions`
181 : *
182 : * Print out the path to files which define options for this tool.
183 : *
184 : * \li `--source-option-sources`
185 : *
186 : * Print out all the options and their sources. This shows you where a
187 : * value come from: command line, environment variable, configuration file,
188 : * etc.
189 : */
190 : option const g_system_options[] =
191 : {
192 : define_option(
193 : Name("build-date")
194 : , Flags(standalone_command_flags<GETOPT_FLAG_GROUP_COMMANDS>())
195 : , Help("print out the time and date when %p was built and exit.")
196 : ),
197 : define_option(
198 : Name("configuration-filenames")
199 : , Flags(standalone_command_flags<GETOPT_FLAG_GROUP_COMMANDS>())
200 : , Help("print out the list of configuration files checked out by this tool.")
201 : ),
202 : define_option(
203 : Name("copyright")
204 : , ShortName('C')
205 : , Flags(standalone_command_flags<GETOPT_FLAG_GROUP_COMMANDS>())
206 : , Help("print out the copyright of %p and exit.")
207 : ),
208 : define_option(
209 : Name("environment-variable-name")
210 : , Flags(standalone_command_flags<GETOPT_FLAG_GROUP_COMMANDS>())
211 : , Help("print out the name of the environment variable supported by %p (if any.)")
212 : ),
213 : define_option(
214 : Name("help")
215 : , ShortName('h')
216 : , Flags(standalone_command_flags<GETOPT_FLAG_GROUP_COMMANDS
217 : , GETOPT_FLAG_SHOW_USAGE_ON_ERROR>())
218 : , Help("print out this help screen and exit.")
219 : ),
220 : define_option(
221 : Name("license")
222 : , ShortName('L')
223 : , Flags(standalone_command_flags<GETOPT_FLAG_GROUP_COMMANDS>())
224 : , Help("print out the license of %p and exit.")
225 : ),
226 : define_option(
227 : Name("path-to-option-definitions")
228 : , Flags(standalone_command_flags<GETOPT_FLAG_GROUP_COMMANDS>())
229 : , Help("print out the path to the option definitons.")
230 : ),
231 : define_option(
232 : Name("show-option-sources")
233 : , Flags(standalone_command_flags<GETOPT_FLAG_GROUP_COMMANDS>())
234 : , Help("parse all the options and then print out the source of each value and each override.")
235 : ),
236 : define_option(
237 : Name("version")
238 : , ShortName('V')
239 : , Flags(standalone_command_flags<GETOPT_FLAG_GROUP_COMMANDS>())
240 : , Help("print out the version of %p and exit.")
241 : ),
242 : end_options()
243 : };
244 :
245 :
246 : /** \brief Optional list of options.
247 : *
248 : * This optional list of options is added only when the
249 : * f_configuration_filename parameter has a valid filename.
250 : *
251 : * The following are the currently added options:
252 : *
253 : * \li `--config-dir`
254 : *
255 : * This option allows for adding more configuration directories.
256 : * These work the same way as directories defined in the
257 : * f_configuration_directories.
258 : */
259 : option const g_if_configuration_filename_system_options[] =
260 : {
261 : define_option(
262 : Name("config-dir")
263 : , Flags(any_flags<GETOPT_FLAG_COMMAND_LINE
264 : , GETOPT_FLAG_ENVIRONMENT_VARIABLE
265 : , GETOPT_FLAG_REQUIRED
266 : , GETOPT_FLAG_MULTIPLE
267 : , GETOPT_FLAG_GROUP_OPTIONS>())
268 : , Help("add one or more configuration directory paths to search for configuration files.")
269 : ),
270 : end_options()
271 : };
272 :
273 :
274 :
275 :
276 : /** \brief Value when no default option was defined.
277 : *
278 : * Some options may have defaults in which case their indexes are used.
279 : * By default, an option has no defaults and we instead use -1.
280 : */
281 : int const NO_DEFAULT_OPT = -1;
282 :
283 :
284 : /** \brief Check whether this parameter is an argument.
285 : *
286 : * An argument is defined as a command line parameter that starts with
287 : * a dash and is not just "-".
288 : *
289 : * Note that "--" is viewed as an argument (this function returns true)
290 : * and the getopt class takes it as a command meaning any other parameter
291 : * is not an argument.
292 : */
293 231 : bool is_arg(char const * a)
294 : {
295 : // "-" and "--" are not options; however "--" returns true
296 : // because after a "--" we take the data as default arguments
297 231 : return a[0] == '-' && a[1] != '\0';
298 : }
299 :
300 :
301 :
302 : /** \brief Check for a "--show-option-sources" flag.
303 : *
304 : * When this flag is defined, we turn on the trace mode in the option_info
305 : * class (which is a global flag). That way we can honor the
306 : * "--show-option-sources" when we are done parsing the configuration files,
307 : * environment variable, and command line.
308 : *
309 : * \param[in] argc The number of items in the \p argv array.
310 : * \param[in] argv The arguments.
311 : */
312 511 : void check_for_show_sources(int argc, char * argv[])
313 : {
314 : static bool found = false;
315 :
316 511 : if(!found && argv != nullptr)
317 : {
318 1041 : for(int idx(1); idx < argc; ++idx)
319 : {
320 670 : if(strcmp(argv[idx], "--show-option-sources") == 0)
321 : {
322 1 : found = true;
323 1 : option_info::set_trace_sources(true);
324 : }
325 : }
326 : }
327 511 : }
328 :
329 :
330 :
331 : } // no name namespace
332 :
333 :
334 :
335 : /** \class getopt_exception
336 : * \brief Base exception of the advgetopt class.
337 : *
338 : * This exception is the base exception of all the advgetopt exceptions.
339 : * Catching this exception allows you to capture all the getopt exceptions.
340 : */
341 :
342 :
343 : /** \class getopt_exception_default
344 : * \brief No default and no value specified.
345 : *
346 : * When a parameter is not specified and no default is available, this
347 : * exception is raised.
348 : */
349 :
350 :
351 : /** \class getopt_exception_undefined
352 : * \brief Attempting to access something that is undefined.
353 : *
354 : * This exception is used when you attempt to access data that was not
355 : * defined. For example, if your tool tries to read parameter "version"
356 : * and that was not defined in the list of options, this exception is
357 : * raised.
358 : */
359 :
360 :
361 : /** \class getopt_exception_invalid
362 : * \brief Attempted to use some invalid data.
363 : *
364 : * This exception is used whenever an attempt is made to access data that
365 : * does not make sense (is invalid.)
366 : *
367 : * For example, the table of options makes use of enumerations for different
368 : * parts. If one of these has a value which does not represent a valid
369 : * enumeration value, then this exception is raised.
370 : */
371 :
372 :
373 : /** \class getopt
374 : * \brief Class used to parse command line options.
375 : *
376 : * This class is the one used by all the wpkg tools to parse the command line
377 : * options. It is very advanced and is capable to read many different types
378 : * of options with a letter (-h) and a word (--verbose) with no parameters,
379 : * one parameter, any number of parameters, and a set of "filenames" (lose
380 : * options that are not specific to an option.)
381 : */
382 :
383 :
384 : /** \struct option
385 : * \brief Structure representing an option.
386 : *
387 : * When creating a getopt() object you have to pass an array of options. That
388 : * array is defined as a set of option structures.
389 : *
390 : * The last option must be an end_options(). It has its f_flags set to
391 : * GETOPT_FLAG_END and all the other parameters are set to zero (i.e. no
392 : * name, no short name, etc.)
393 : *
394 : * Note that with the newer version of the library, you are not expected
395 : * to manually define an array of options. Instead, you want to use the
396 : * C++ functions such as Name(), ShortName(), Flags(), etc. These functions
397 : * are capable of verifying that at least some of the values are valid at
398 : * compile time.
399 : */
400 :
401 :
402 : /** \brief Initialize the getopt object.
403 : *
404 : * \section into Introduction
405 : *
406 : * This constructor initializes a getopt object. It also reads and parses
407 : * the corresponding option configuration file if it exists (based on the
408 : * project name defined in the environment parameter.)
409 : *
410 : * \section program_name Program Name
411 : *
412 : * Once constructed, if you want to have access to the program name, make
413 : * sure to call this function with your `argv` variable:
414 : *
415 : * \code
416 : * opt.parse_program_name(argv);
417 : * \endcode
418 : *
419 : * Remember that the program name is often used in error messages so having
420 : * it defined early is generally a good idea.
421 : *
422 : * \section dynamism Dynamic Options
423 : *
424 : * This constructor is most often used when you want to dynamically add
425 : * options to your executable with the parse_options_info() function.
426 : * For example, the list of options may vary slightly depending on what
427 : * your command is named when launched.
428 : *
429 : * For example:
430 : *
431 : * \code
432 : * if(time(nullptr) & 1)
433 : * {
434 : * opt.parse_options_info(odd_options);
435 : * }
436 : * else
437 : * {
438 : * opt.parse_options_info(even_options);
439 : * }
440 : * \endcode
441 : *
442 : * \section aliases Linking Aliases
443 : *
444 : * After you added all your dynamic options, you want to make sure that
445 : * aliases are linked to the final option. You should always call that
446 : * function because you can't be sure whether someone will add such an
447 : * alias in the .ini option file.
448 : *
449 : * \code
450 : * opt.link_aliases();
451 : * \endcode
452 : *
453 : * You can call this function any number of times. So if you add yet
454 : * more dynamic options at a later time, just make sure to call it
455 : * again in case aliases were added.
456 : *
457 : * \section parse Parse the Arguments
458 : *
459 : * Finally, you want to call the following functions in that order to
460 : * parse the data from configuration files, the environment variable,
461 : * and the list of command line arguments:
462 : *
463 : * \code
464 : * opt.parse_configuration_files();
465 : * opt.parse_environment_variable();
466 : * opt.parse_arguments(argc, argv, option_source_t::SOURCE_COMMAND_LINE);
467 : * \endcode
468 : *
469 : * The order is important because the last command line option found is
470 : * the one kept. So if the same argument is found in the configuration
471 : * file, the environment variable and the command line, the one on the
472 : * command line is kept. In most cases it makes no difference for standalone
473 : * flags, but arguments that expect a parameter will be changed to the last
474 : * specified value.
475 : *
476 : * If you want to determine the configuration filenames, you may use the
477 : * process_configuration_file() function directly instead of the
478 : * parse_configuration_files() function. This also gives you the ability
479 : * to test whether a configuration file was indeed read.
480 : *
481 : * Note that the parse_arguments() last parameter (only_environment_variable)
482 : * is expected to be left along when you call it with `argc` and `argv`.
483 : *
484 : * If you just have a string instead of an `argv` variable, call the
485 : * parse_string() function instead. It will transform your string in an
486 : * array of arguments and then call the parse_arguments() for you.
487 : *
488 : * \attention
489 : * Note that the program name does not get defined until you call the
490 : * parse_program_name() function since that information comes from the
491 : * first arguments of your command line which we do not get on
492 : * construction in this case.
493 : *
494 : * \attention
495 : * Since the arguments are not known to the getopt system yet, the
496 : * GETOPT_ENVIRONMENT_FLAG_PROCESS_SYSTEM_PARAMETERS is not used in
497 : * this case.
498 : *
499 : * \param[in] opt_env The list of options that your program supports.
500 : *
501 : * \sa link_aliases()
502 : * \sa parse_arguments()
503 : * \sa parse_configuration_files()
504 : * \sa parse_environment_variable()
505 : * \sa parse_string()
506 : * \sa process_configuration_file()
507 : *
508 : * \sa initialize_parser()
509 : * \sa finish_parsing()
510 : */
511 78 : getopt::getopt(options_environment const & opt_env)
512 : {
513 78 : initialize_parser(opt_env);
514 78 : }
515 :
516 :
517 : /** \brief Initialize the getopt object.
518 : *
519 : * The constructor initializes a getopt object and parse the specified
520 : * argv array. If defined, it also parses a configuration file and
521 : * an environment variable.
522 : *
523 : * The order in which parameters are parsed is important since only the
524 : * last value is kept:
525 : *
526 : * \li Each existing configuration file in the order defined in the vector;
527 : * \li The environment variable;
528 : * \li the argv array.
529 : *
530 : * The constructor calls the reset() function to start the parsing. It is
531 : * possible to call the reset() function at any time to parse a new set
532 : * of parameters.
533 : *
534 : * The argv array cannot be nullptr and the array cannot be empty. It must have
535 : * at least one entry representing the program name (argv[0]).
536 : *
537 : * The configuration_files vector can be empty in which case no configuration
538 : * files are read.
539 : *
540 : * The environment_variable_name can be nullptr or the empty string in which case
541 : * it is ignored.
542 : *
543 : * \note
544 : * All the data gets copied while parsed. If the argv array is deleted on
545 : * return, the getopt object remains valid.
546 : *
547 : * \exception getopt_exit
548 : * This function calls finish_parsing() which may throw this exception.
549 : * See that function for details.
550 : *
551 : * \param[in] opt_env The list of options that your program supports.
552 : * \param[in] argc The number of arguments in argv.
553 : * \param[in] argv An array of strings representing arguments.
554 : *
555 : * \sa initialize_parser()
556 : * \sa finish_parsing()
557 : */
558 260 : getopt::getopt(options_environment const & opt_env
559 : , int argc
560 283 : , char * argv[])
561 : {
562 260 : check_for_show_sources(argc, argv);
563 260 : initialize_parser(opt_env);
564 249 : finish_parsing(argc, argv);
565 237 : }
566 :
567 :
568 : /** \brief Initialize the parser.
569 : *
570 : * This function is called from the two constructors. It initializes the
571 : * basic options from the user definitions, the file when there is one,
572 : * the group names, and if allowed the system command line options.
573 : *
574 : * This is enough to then parse arguments or configuration files, although
575 : * in most cases this is used to allow for additional environment options
576 : * to be inserted before calling the finish_parsing() function.
577 : *
578 : * \param[in] opt_env The list of options that your program supports.
579 : */
580 338 : void getopt::initialize_parser(options_environment const & opt_env)
581 : {
582 338 : f_options_environment = opt_env;
583 :
584 338 : parse_options_info(f_options_environment.f_options, false);
585 330 : parse_options_from_file();
586 327 : parse_options_from_group_names();
587 327 : if(has_flag(GETOPT_ENVIRONMENT_FLAG_SYSTEM_PARAMETERS | GETOPT_ENVIRONMENT_FLAG_PROCESS_SYSTEM_PARAMETERS))
588 : {
589 80 : parse_options_info(g_system_options, true);
590 80 : if(f_options_environment.f_configuration_filename != nullptr
591 19 : && *f_options_environment.f_configuration_filename != '\0')
592 : {
593 : // add the "--config-dir <path> ..." option
594 : //
595 16 : parse_options_info(g_if_configuration_filename_system_options, true);
596 : }
597 : }
598 327 : }
599 :
600 :
601 : /** \brief Actually parse everything.
602 : *
603 : * This function allows you to run the second half of the initialization
604 : * process. We've broken this process up in two, so you can initialize
605 : * a getopt object, add some other options, then finish up the
606 : * initialization process by calling this function.
607 : *
608 : * The command line arguments, configuration files.
609 : *
610 : * \exception getopt_exit
611 : * If the GETOPT_ENVIRONMENT_FLAG_PROCESS_SYSTEM_PARAMETERS is set and
612 : * a system command was specified on the command, such as --help or
613 : * --version, then that command is run and the function throws this
614 : * exception.
615 : *
616 : * \param[in] argc The number of arguments in argv.
617 : * \param[in] argv An array of strings representing arguments.
618 : */
619 252 : void getopt::finish_parsing(int argc, char * argv[])
620 : {
621 252 : if(argv == nullptr)
622 : {
623 1 : throw getopt_logic_error("argv pointer cannot be nullptr");
624 : }
625 :
626 251 : check_for_show_sources(argc, argv);
627 :
628 251 : parse_program_name(argv);
629 251 : if(f_options_by_name.empty())
630 : {
631 3 : throw getopt_logic_error("an empty list of options is not legal, you must defined at least one (i.e. --version, --help...)");
632 : }
633 :
634 248 : link_aliases();
635 :
636 244 : define_environment_variable_data();
637 :
638 244 : parse_configuration_files(argc, argv);
639 244 : f_parsed = false;
640 244 : parse_environment_variable();
641 244 : f_parsed = false;
642 244 : parse_arguments(argc, argv, option_source_t::SOURCE_COMMAND_LINE, false);
643 :
644 244 : if(has_flag(GETOPT_ENVIRONMENT_FLAG_PROCESS_SYSTEM_PARAMETERS))
645 : {
646 5 : flag_t const result(process_system_options(std::cout));
647 5 : if((result & SYSTEM_OPTION_COMMANDS_MASK) != 0)
648 : {
649 4 : throw getopt_exit("system command processed.", 0);
650 : }
651 : }
652 240 : }
653 :
654 :
655 : /** \brief Verify that the parser is done.
656 : *
657 : * This function ensures that the parser is done. If the parser is not yet
658 : * done, then the function raises an exception. This allows me to detect
659 : * that I am trying to access a parameter before the whole parsing process
660 : * is done (i.e. I had a call to is_defined("config-dir") happening in the
661 : * configuration handling way before the environment variables and command
662 : * line arguments were parsed, that would never work!)
663 : *
664 : * \exception getopt_initialization
665 : * This exception is raised if the parser is not done yet.
666 : */
667 2675 : void getopt::is_parsed() const
668 : {
669 2675 : if(!f_parsed
670 7 : && (f_options_environment.f_environment_flags & GETOPT_ENVIRONMENT_FLAG_AUTO_DONE) == 0)
671 : {
672 : throw getopt_initialization(
673 : "function called too soon, parser is not done yet"
674 : " (i.e. is_defined(), get_string(), get_integer()"
675 7 : " cannot be called until the parser is done)");
676 : }
677 2668 : }
678 :
679 :
680 : /** \brief Return a reference to the options environment.
681 : *
682 : * This function returns a reference to the options environment that
683 : * was passed to the constructor. This is useful to functions that
684 : * do not otherwise have access to that object.
685 : *
686 : * \return This getopt options environment.
687 : */
688 1 : options_environment const & getopt::get_options_environment() const
689 : {
690 1 : return f_options_environment;
691 : }
692 :
693 :
694 : /** \brief Check whether an environment flag is set or not.
695 : *
696 : * This function checks the environment flags for the specified \p flag.
697 : * When the flag is set, the function returns true.
698 : *
699 : * You may test multiple flags at the same time, if any one of them is set,
700 : * then the function returns true.
701 : *
702 : * \param[in] flag The flag to check out.
703 : *
704 : * \return true if the flag is set.
705 : */
706 736 : bool getopt::has_flag(flag_t flag) const
707 : {
708 736 : return (f_options_environment.f_environment_flags & flag) != 0;
709 : }
710 :
711 :
712 : /** \brief Retrieve the environment variable string.
713 : *
714 : * This function retrieves the environment variable string and saves it
715 : * in the f_environment_variable field. This is used to parse that string
716 : * and add option values, and also by the configuration file loader to see
717 : * whether a --config-dir was used in there.
718 : */
719 495 : void getopt::define_environment_variable_data()
720 : {
721 495 : f_environment_variable.clear();
722 :
723 495 : if(f_options_environment.f_environment_variable_name == nullptr
724 237 : || *f_options_environment.f_environment_variable_name == '\0')
725 : {
726 : // no name
727 : //
728 268 : return;
729 : }
730 :
731 227 : char const * s(getenv(f_options_environment.f_environment_variable_name));
732 227 : if(s == nullptr)
733 : {
734 : // no environment variable with that name
735 : //
736 130 : return;
737 : }
738 :
739 97 : f_environment_variable = s;
740 : }
741 :
742 :
743 : /** \brief Check for an environment variable.
744 : *
745 : * If the name of an environment variable is specified in the option
746 : * environment structure, then it is read as a command line string.
747 : * This function parses the string in an array of strings and then parses
748 : * it as an argv array (just like the argv parameter defined in a main()
749 : * function).
750 : *
751 : * Since the environment variable is checked after the configuration files,
752 : * the options defined in the variable can change the definitions from
753 : * the configuration files.
754 : *
755 : * Like in the configuration files, only options can be specified in the
756 : * environment variable and commands generate an error. The system knows
757 : * since options that can be included in the environment variable are
758 : * marked by the GETOPT_FLAG_ENVIRONMENT_VARIABLE flag. In other words,
759 : * you may allow options to appear on the command line, in configuration
760 : * files, in environment variables or a mix of all of these locations.
761 : */
762 251 : void getopt::parse_environment_variable()
763 : {
764 251 : define_environment_variable_data();
765 251 : if(!f_environment_variable.empty())
766 : {
767 50 : parse_string(
768 : f_environment_variable
769 : , option_source_t::SOURCE_ENVIRONMENT_VARIABLE
770 : , true);
771 : }
772 :
773 251 : f_parsed = true;
774 251 : }
775 :
776 :
777 : /** \brief Parse a string similar to a command line argument.
778 : *
779 : * This function parses a line of command line arguments from a string.
780 : * Especially, it is used to parse the environment variable which is
781 : * a string of arguments.
782 : *
783 : * This can be used to parse the command line string as received under
784 : * MS-Windows (i.e. an unparsed one long string of arguments, where
785 : * you also need to do the glob() calls yourself.)
786 : *
787 : * This function actually transforms the input string in an array of
788 : * strings and then calls the parse_arguments() function.
789 : *
790 : * \note
791 : * The input allows for an empty string in which case pretty much nothing
792 : * happens.
793 : *
794 : * \param[in] str The string that is going to be parsed.
795 : * \param[in] source Where the value comes from.
796 : * \param[in] only_environment_variable Whether only options marked with
797 : * the GETOPT_FLAG_ENVIRONMENT_VARIABLE flag are accepted.
798 : */
799 51 : void getopt::parse_string(
800 : std::string const & str
801 : , option_source_t source
802 : , bool only_environment_variable)
803 : {
804 101 : string_list_t args(split_environment(str));
805 51 : if(args.empty())
806 : {
807 : // nothing extra to do
808 : //
809 1 : return;
810 : }
811 :
812 : // TODO: expand the arguments that include unquoted '*', '?', '[...]'
813 : // (note that we remove the quotes at the moment so we'd have
814 : // to keep track of that specific problem...)
815 :
816 : // the argv array has to be a null terminated bare string pointers
817 : //
818 100 : std::vector<char *> sub_argv;
819 :
820 50 : sub_argv.resize(args.size() + 2);
821 :
822 : // argv[0] is the program name
823 : //
824 50 : sub_argv[0] = const_cast<char *>(f_program_fullname.c_str());
825 :
826 : // the other arguments are from the variable
827 : //
828 242 : for(size_t idx(0); idx < args.size(); ++idx)
829 : {
830 192 : sub_argv[idx + 1] = const_cast<char *>(args[idx].c_str());
831 : }
832 :
833 : // this is probably already a nullptr
834 : //
835 50 : sub_argv[args.size() + 1] = nullptr;
836 :
837 : // now convert those parameters in values
838 : //
839 100 : parse_arguments(
840 50 : static_cast<int>(sub_argv.size() - 1)
841 : , sub_argv.data()
842 : , source
843 : , only_environment_variable);
844 : }
845 :
846 :
847 : /** \brief Transform a string in an array of arguments.
848 : *
849 : * This function is used to transform a string to an array of arguments
850 : * that can then be used with the parse_arguments() function.
851 : *
852 : * For example, it is used to parse the environment variable string.
853 : *
854 : * \note
855 : * The input string may include quotes. These will be removed. There is
856 : * currently no support for the backslash character.
857 : *
858 : * \param[in] environment The string to be split in arguments.
859 : *
860 : * \return An array of strings.
861 : */
862 57 : string_list_t getopt::split_environment(std::string const & environment)
863 : {
864 : // this is exactly like the command line only in an environment variable
865 : // so parse the parameters just like the shell
866 : //
867 57 : string_list_t args;
868 114 : std::string a;
869 57 : char const * s(environment.c_str());
870 3541 : while(*s != '\0')
871 : {
872 1742 : if(isspace(*s))
873 : {
874 156 : if(!a.empty())
875 : {
876 154 : args.push_back(a);
877 154 : a.clear();
878 : }
879 1 : do
880 : {
881 157 : ++s;
882 : }
883 157 : while(isspace(*s));
884 : }
885 1586 : else if(*s == '"'
886 1583 : || *s == '\'')
887 : {
888 : // support quotations and remove them from the argument
889 : //
890 5 : char const quote(*s++);
891 351 : while(*s != '\0'
892 178 : && *s != quote)
893 : {
894 173 : a += *s++;
895 : }
896 5 : if(*s != '\0')
897 : {
898 5 : ++s;
899 5 : }
900 : }
901 : else
902 : {
903 1581 : a += *s++;
904 : }
905 : }
906 :
907 57 : if(!a.empty())
908 : {
909 49 : args.push_back(a);
910 : }
911 :
912 114 : return args;
913 : }
914 :
915 :
916 : /** \brief Parse an array of arguments.
917 : *
918 : * This function accepts an array of arguments as received by the main()
919 : * function. By default, though, you pass the argc/argv parameters to
920 : * the getopt() constructor which automatically calls this function.
921 : *
922 : * This functin is public so you can call it with additional lists of
923 : * arguments. If that list of arguments comes as a string, you may want
924 : * to call the parse_string() function instead. It will transform your
925 : * string in a list of parameters for you.
926 : *
927 : * When the \p only_environment_variable parameter is set to true, then
928 : * it is considered that the input arguments were found in an environment
929 : * variables and they are only accepted if the corresponding option
930 : * definition includes the GETOPT_FLAG_ENVIRONMENT_VARIABLE flag.
931 : *
932 : * When the \p only_environment_variable parameter is set to false, the
933 : * arguments are viewed as command line arguments and the corresponding
934 : * options must include the GETOPT_FLAG_COMMAND_LINE flag.
935 : *
936 : * Variables get overridden by the newest values found in the list of
937 : * arguments.
938 : *
939 : * Note that the command line arguments are the only ones that should
940 : * include a command (opposed to an option that alters the behavior of
941 : * your commands.) However, the advgetopt system expects you to properly
942 : * define what can be used in a configuration file, in an environment
943 : * variable, or directly on the command line. It is not in charge of
944 : * that part in itself.
945 : *
946 : * \note
947 : * The code may find some errors in the tables passed to the advgetopt
948 : * environment (i.e. a duplicate definition.) When such errors are
949 : * detected, an exception is raised. Errors found on the command line
950 : * generate a log message. If you setup a callback, you can then decide
951 : * to either call exit(1) or raise your own exception.
952 : *
953 : * \note
954 : * The function does NOT check whether the list of arguments (argv) is
955 : * terminated by nullptr. The argc parameter must be correct.
956 : *
957 : * \param[in] argc The number of arguments in argv.
958 : * \param[in] argv The argument strings terminated by a nullptr.
959 : * \param[in] source Where the value comes from.
960 : * \param[in] only_environment_variable Accept command line arguments (false)
961 : * or environment variable arguments (true).
962 : */
963 308 : void getopt::parse_arguments(
964 : int argc
965 : , char * argv[]
966 : , option_source_t source
967 : , bool only_environment_variable)
968 : {
969 726 : for(int i(1); i < argc; ++i)
970 : {
971 433 : if(argv[i][0] == '-')
972 : {
973 378 : if(argv[i][1] == '-')
974 : {
975 328 : if(argv[i][2] == '\0')
976 : {
977 : // end of options, skip the '--' and then anything else
978 : // is taken as "filenames" (or whatever the tool expects)
979 : //
980 12 : if(f_default_option == nullptr)
981 : {
982 2 : cppthread::log << cppthread::log_level_t::error
983 1 : << "no default options defined; thus \"--\" is not accepted by this program."
984 2 : << cppthread::end;
985 1 : break;
986 : }
987 :
988 11 : if(only_environment_variable)
989 : {
990 6 : if(!f_default_option->has_flag(GETOPT_FLAG_ENVIRONMENT_VARIABLE))
991 : {
992 2 : cppthread::log << cppthread::log_level_t::error
993 1 : << "option \"--\" is not supported in the environment variable."
994 2 : << cppthread::end;
995 1 : break;
996 : }
997 : }
998 : else
999 : {
1000 5 : if(!f_default_option->has_flag(GETOPT_FLAG_COMMAND_LINE))
1001 : {
1002 2 : cppthread::log << cppthread::log_level_t::error
1003 1 : << "option \"--\" is not supported on the command line."
1004 2 : << cppthread::end;
1005 1 : break;
1006 : }
1007 : }
1008 :
1009 : // in this case we do NOT test whether an argument uses
1010 : // a dash (-) we take them all as default options
1011 : //
1012 14 : while(i + 1 < argc)
1013 : {
1014 14 : ++i;
1015 14 : f_default_option->add_value(argv[i], source);
1016 : }
1017 : }
1018 : else
1019 : {
1020 : // a long option, check that it is defined in the
1021 : // programmer defined options
1022 : //
1023 628 : std::string option_name(argv[i] + 2);
1024 628 : std::string option_value;
1025 316 : std::string::size_type const pos(option_name.find('='));
1026 316 : if(pos != std::string::npos)
1027 : {
1028 21 : if(pos == 0)
1029 : {
1030 2 : cppthread::log << cppthread::log_level_t::error
1031 1 : << "name missing in \""
1032 1 : << argv[i]
1033 1 : << "\"."
1034 2 : << cppthread::end;
1035 1 : break;
1036 : }
1037 :
1038 20 : option_value = option_name.substr(pos + 1);
1039 20 : option_name.resize(pos);
1040 : }
1041 626 : option_info::pointer_t opt(get_option(option_name));
1042 314 : if(opt == nullptr)
1043 : {
1044 2 : cppthread::log << cppthread::log_level_t::error
1045 1 : << "option \"--"
1046 1 : << option_name
1047 1 : << "\" is not supported."
1048 2 : << cppthread::end;
1049 1 : break;
1050 : }
1051 313 : if(only_environment_variable)
1052 : {
1053 57 : if(!opt->has_flag(GETOPT_FLAG_ENVIRONMENT_VARIABLE))
1054 : {
1055 2 : cppthread::log << cppthread::log_level_t::error
1056 1 : << "option \"--"
1057 1 : << option_name
1058 1 : << "\" is not supported in the environment variable."
1059 2 : << cppthread::end;
1060 1 : break;
1061 : }
1062 : }
1063 : else
1064 : {
1065 256 : if(!opt->has_flag(GETOPT_FLAG_COMMAND_LINE))
1066 : {
1067 2 : cppthread::log << cppthread::log_level_t::error
1068 1 : << "option \"--"
1069 1 : << option_name
1070 1 : << "\" is not supported on the command line."
1071 2 : << cppthread::end;
1072 1 : break;
1073 : }
1074 : }
1075 311 : if(pos != std::string::npos)
1076 : {
1077 : // the user specified a value after an equal sign
1078 : //
1079 18 : add_option_from_string(opt, option_value, std::string(), source);
1080 : }
1081 : else
1082 : {
1083 293 : add_options(opt, i, argc, argv, source);
1084 : }
1085 : }
1086 : }
1087 : else
1088 : {
1089 50 : if(argv[i][1] == '\0')
1090 : {
1091 : // stdin/stdout (a '-' by itself)
1092 : //
1093 9 : if(f_default_option == nullptr)
1094 : {
1095 2 : cppthread::log << cppthread::log_level_t::error
1096 1 : << "no default options defined; thus \"-\" is not accepted by this program."
1097 2 : << cppthread::end;
1098 1 : break;
1099 : }
1100 8 : if(only_environment_variable)
1101 : {
1102 4 : if(!f_default_option->has_flag(GETOPT_FLAG_ENVIRONMENT_VARIABLE))
1103 : {
1104 2 : cppthread::log << cppthread::log_level_t::error
1105 1 : << "option \"-\" is not supported in the environment variable."
1106 2 : << cppthread::end;
1107 1 : break;
1108 : }
1109 : }
1110 : else
1111 : {
1112 4 : if(!f_default_option->has_flag(GETOPT_FLAG_COMMAND_LINE))
1113 : {
1114 2 : cppthread::log << cppthread::log_level_t::error
1115 1 : << "option \"-\" is not supported on the command line."
1116 2 : << cppthread::end;
1117 1 : break;
1118 : }
1119 : }
1120 :
1121 : // this is similar to a default option by itself
1122 : //
1123 6 : f_default_option->add_value(argv[i], source);
1124 : }
1125 : else
1126 : {
1127 : // short option(s)
1128 : //
1129 : // i gets incremented by add_options() so we have to
1130 : // keep a copy in `k`
1131 : //
1132 82 : std::string const short_args_string(argv[i] + 1);
1133 81 : for(libutf8::utf8_iterator short_args(short_args_string)
1134 81 : ; short_args != short_args_string.end()
1135 : ; ++short_args)
1136 : {
1137 83 : option_info::pointer_t opt(get_option(*short_args));
1138 43 : if(opt == nullptr)
1139 : {
1140 2 : cppthread::log << cppthread::log_level_t::error
1141 1 : << "option \"-"
1142 2 : << short_name_to_string(*short_args)
1143 1 : << "\" is not supported."
1144 3 : << cppthread::end;
1145 1 : break;
1146 : }
1147 42 : if(only_environment_variable)
1148 : {
1149 17 : if(!opt->has_flag(GETOPT_FLAG_ENVIRONMENT_VARIABLE))
1150 : {
1151 2 : cppthread::log << cppthread::log_level_t::error
1152 1 : << "option \"-"
1153 2 : << short_name_to_string(*short_args)
1154 1 : << "\" is not supported in the environment variable."
1155 3 : << cppthread::end;
1156 1 : break;
1157 : }
1158 : }
1159 : else
1160 : {
1161 25 : if(!opt->has_flag(GETOPT_FLAG_COMMAND_LINE))
1162 : {
1163 2 : cppthread::log << cppthread::log_level_t::error
1164 1 : << "option \"-"
1165 2 : << short_name_to_string(*short_args)
1166 1 : << "\" is not supported on the command line."
1167 3 : << cppthread::end;
1168 1 : break;
1169 : }
1170 : }
1171 40 : add_options(opt, i, argc, argv, source);
1172 : }
1173 : }
1174 : }
1175 : }
1176 : else
1177 : {
1178 : // direct entry (filename or whatever the tool expects as a default)
1179 : //
1180 55 : if(f_default_option == nullptr)
1181 : {
1182 4 : cppthread::log << cppthread::log_level_t::error
1183 2 : << "no default options defined; we do not know what to do of \""
1184 2 : << argv[i]
1185 2 : << "\"; standalone parameters are not accepted by this program."
1186 4 : << cppthread::end;
1187 2 : break;
1188 : }
1189 53 : if(only_environment_variable)
1190 : {
1191 27 : if(!f_default_option->has_flag(GETOPT_FLAG_ENVIRONMENT_VARIABLE))
1192 : {
1193 2 : cppthread::log << cppthread::log_level_t::error
1194 1 : << "default options are not supported in the environment variable."
1195 2 : << cppthread::end;
1196 1 : break;
1197 : }
1198 : }
1199 : else
1200 : {
1201 26 : if(!f_default_option->has_flag(GETOPT_FLAG_COMMAND_LINE))
1202 : {
1203 2 : cppthread::log << cppthread::log_level_t::error
1204 1 : << "default options are not supported on the command line."
1205 2 : << cppthread::end;
1206 1 : break;
1207 : }
1208 : }
1209 51 : f_default_option->add_value(argv[i], source);
1210 : }
1211 : }
1212 :
1213 307 : f_parsed = true;
1214 307 : }
1215 :
1216 :
1217 : /** \brief Return the alias if there is one.
1218 : *
1219 : * This function returns the input \p opt parameter unless it is an
1220 : * alias in which case the destination alias option is returned instead.
1221 : *
1222 : * \param[in] opt The option for which an alias is desired.
1223 : *
1224 : * \return \p opt unless it is an alias in which case
1225 : * opt->get_alias_destination() is returned.
1226 : *
1227 : * \sa option_info::get_alias()
1228 : */
1229 4653 : option_info::pointer_t getopt::get_alias_destination(option_info::pointer_t opt) const
1230 : {
1231 9306 : if(opt != nullptr
1232 4653 : && opt->has_flag(GETOPT_FLAG_ALIAS))
1233 : {
1234 90 : opt = opt->get_alias_destination();
1235 90 : if(opt == nullptr)
1236 : {
1237 1 : throw getopt_undefined("getopt::get_alias_destination(): alias is missing. Did you call link_aliases()?");
1238 : }
1239 : }
1240 :
1241 4652 : return opt;
1242 : }
1243 :
1244 :
1245 : /** \brief Retrieve the complete list of options.
1246 : *
1247 : * Applications that let their users enter dynamically options need to
1248 : * have access to the resulting list of options which may not otherwise
1249 : * be known.
1250 : *
1251 : * \return The map of options indexed by name.
1252 : *
1253 : * \sa parse_options_from_file()
1254 : */
1255 1 : option_info::map_by_name_t const & getopt::get_options() const
1256 : {
1257 1 : return f_options_by_name;
1258 : }
1259 :
1260 :
1261 : /** \brief Retrieve an option by name.
1262 : *
1263 : * This function retrieves an option by name. The function handles the
1264 : * special case of the default option. This means "--" can always be
1265 : * used to access the default option, whever the name given to that
1266 : * option in the declaration of your options.
1267 : *
1268 : * Of course, if no default is defined, then "--" returns a nullptr.
1269 : *
1270 : * By default the function returns the final option. That is, if the
1271 : * named option is an alias, the destination option is returned, not
1272 : * the alias. This way the rest of the code is much simpler. You may
1273 : * get the exact option, even if it is aliased, by setting the
1274 : * \p exact_option parameter to true. It is really rare that you
1275 : * would need to do so, though.
1276 : *
1277 : * \param[in] name The name of the option to retrieve.
1278 : * \param[in] exact_option Return the exact option, not its alias.
1279 : *
1280 : * \return The pointer to the named option or nullptr if not found.
1281 : */
1282 5597 : option_info::pointer_t getopt::get_option(std::string const & name, bool exact_option) const
1283 : {
1284 : // we need a special case when looking for the default option
1285 : // because the name may not be "--" in the option table
1286 : // (i.e. you could call your default option "filenames" for
1287 : // example.)
1288 : //
1289 11194 : option_info::pointer_t opt;
1290 :
1291 11194 : std::string const n(boost::replace_all_copy(name, "_", "-"));
1292 :
1293 11194 : if(n.length() == 2
1294 441 : && n[0] == '-'
1295 6033 : && n[1] == '-')
1296 : {
1297 436 : opt = f_default_option;
1298 : }
1299 : else
1300 : {
1301 5161 : short_name_t short_name(string_to_short_name(n));
1302 5161 : if(short_name != NO_SHORT_NAME)
1303 : {
1304 6 : auto it(f_options_by_short_name.find(short_name));
1305 6 : if(it != f_options_by_short_name.end())
1306 : {
1307 2 : opt = it->second;
1308 : }
1309 : }
1310 : else
1311 : {
1312 5155 : auto it(f_options_by_name.find(n));
1313 5155 : if(it != f_options_by_name.end())
1314 : {
1315 3174 : opt = it->second;
1316 : }
1317 : }
1318 : }
1319 :
1320 : return exact_option
1321 : ? opt
1322 11193 : : get_alias_destination(opt);
1323 : }
1324 :
1325 :
1326 : /** \brief Get an option using its short name.
1327 : *
1328 : * This function searches for an option given its short name.
1329 : *
1330 : * By default the function returns the final option. That is, if the
1331 : * named option is an alias, the destination option is returned, not
1332 : * the alias. This way the rest of the code is much simpler. You may
1333 : * get the exact option, even if it is aliased, by setting the
1334 : * \p exact_option parameter to true. It is really rare that you
1335 : * would need to do so, though.
1336 : *
1337 : * \param[in] short_name The short name of the option to look for.
1338 : * \param[in] exact_option Return the exact option, not its alias.
1339 : *
1340 : * \return The pointer to the option or nullptr if not found.
1341 : */
1342 1606 : option_info::pointer_t getopt::get_option(short_name_t short_name, bool exact_option) const
1343 : {
1344 1606 : auto it(f_options_by_short_name.find(short_name));
1345 1606 : if(it == f_options_by_short_name.end())
1346 : {
1347 1343 : return option_info::pointer_t();
1348 : }
1349 :
1350 : return exact_option
1351 7 : ? it->second
1352 270 : : get_alias_destination(it->second);
1353 : }
1354 :
1355 :
1356 : /** \brief Read parameters of the current option.
1357 : *
1358 : * This function saves the option in the list of options found in this list
1359 : * of arguments. If the option is expected to have parameters, then those
1360 : * are taken from the argv array before the function saves the option in
1361 : * the object list. The index, \p i, is increased accordingly.
1362 : *
1363 : * \warning
1364 : * This function cannot be called properly with the '-' option in case it
1365 : * is viewed as a default parameter. This is because the algorithm expects
1366 : * the index (\p i) to be pointing to the command line option and not the
1367 : * argument to that command.
1368 : *
1369 : * \param[in] opt The concerned option
1370 : * \param[in] i The current position, starting with the option position
1371 : * \param[in] argc The number of arguments in the argv array.
1372 : * \param[in] argv The list of argument strings.
1373 : * \param[in] source Where the value comes from.
1374 : */
1375 333 : void getopt::add_options(
1376 : option_info::pointer_t opt
1377 : , int & i
1378 : , int argc
1379 : , char ** argv
1380 : , option_source_t source)
1381 : {
1382 333 : if(opt->has_flag(GETOPT_FLAG_FLAG))
1383 : {
1384 145 : opt->add_value(opt->get_default(), source);
1385 : }
1386 : else
1387 : {
1388 188 : if(i + 1 < argc && !is_arg(argv[i + 1]))
1389 : {
1390 167 : if(opt->has_flag(GETOPT_FLAG_MULTIPLE))
1391 : {
1392 37 : do
1393 : {
1394 70 : ++i;
1395 70 : opt->add_value(argv[i], source);
1396 : }
1397 70 : while(i + 1 < argc && !is_arg(argv[i + 1]));
1398 : }
1399 : else
1400 : {
1401 134 : ++i;
1402 134 : opt->add_value(argv[i], source);
1403 : }
1404 : }
1405 : else
1406 : {
1407 21 : if(opt->has_flag(GETOPT_FLAG_REQUIRED))
1408 : {
1409 6 : cppthread::log << cppthread::log_level_t::error
1410 3 : << "option --"
1411 3 : << opt->get_name()
1412 3 : << " expects an argument."
1413 6 : << cppthread::end;
1414 : }
1415 : else
1416 : {
1417 : // We need to set something because the value is being
1418 : // set although no argument was specified (but that's
1419 : // legal by this argument's definition)
1420 : //
1421 18 : opt->add_value(std::string(), source);
1422 : }
1423 : }
1424 : }
1425 333 : }
1426 :
1427 :
1428 : /** \brief Add an option with a value string.
1429 : *
1430 : * This function accepts a string as the value. If the option accepts
1431 : * multiple values, then the function makes use of the set_multiple_value()
1432 : * function of the option_info class. This will break the option up in
1433 : * multiple value if possible.
1434 : *
1435 : * \param[in] opt The option receiving a value.
1436 : * \param[in] value The value to assign this option.
1437 : * \param[in] filename The name of a configuration file if the option was
1438 : * read from such.
1439 : * \param[in] source Where the value comes from.
1440 : */
1441 86 : void getopt::add_option_from_string(
1442 : option_info::pointer_t opt
1443 : , std::string const & value
1444 : , std::string const & filename
1445 : , option_source_t source)
1446 : {
1447 : // is the value defined?
1448 : //
1449 86 : if(!value.empty())
1450 : {
1451 84 : if(opt->has_flag(GETOPT_FLAG_FLAG))
1452 : {
1453 2 : cppthread::log << cppthread::log_level_t::error
1454 1 : << "option "
1455 1 : << (filename.empty()
1456 3 : ? "--" + opt->get_name()
1457 3 : : "\"" + boost::replace_all_copy(opt->get_name(), "-", "_") + "\"")
1458 1 : << " cannot be given a value"
1459 1 : << (filename.empty()
1460 3 : ? std::string()
1461 : : " in configuration file \""
1462 2 : + filename
1463 1 : + "\"")
1464 1 : << "."
1465 4 : << cppthread::end;
1466 1 : return;
1467 : }
1468 :
1469 : // does the option support multiple entries?
1470 : //
1471 83 : if(opt->has_flag(GETOPT_FLAG_MULTIPLE))
1472 : {
1473 18 : opt->set_multiple_values(value, source);
1474 : }
1475 : else
1476 : {
1477 65 : opt->set_value(0, value, source);
1478 : }
1479 :
1480 83 : return;
1481 : }
1482 :
1483 : // does the option require a value when used?
1484 : //
1485 2 : if(opt->has_flag(GETOPT_FLAG_REQUIRED))
1486 : {
1487 2 : cppthread::log << cppthread::log_level_t::error
1488 1 : << "option "
1489 1 : << (filename.empty()
1490 2 : ? "--" + opt->get_name()
1491 2 : : "\"" + boost::replace_all_copy(opt->get_name(), "-", "_") + "\"")
1492 1 : << " must be given a value"
1493 1 : << (filename.empty()
1494 2 : ? std::string()
1495 : : " in configuration file \""
1496 1 : + filename
1497 1 : + "\"")
1498 1 : << "."
1499 4 : << cppthread::end;
1500 1 : return;
1501 : }
1502 :
1503 : // accept an empty value otherwise
1504 : //
1505 1 : opt->set_value(0, value, source);
1506 : }
1507 :
1508 :
1509 :
1510 6 : } // namespace advgetopt
1511 : // vim: ts=4 sw=4 et
|