1 // Copyright 2010 Google Inc.
2 // All rights reserved.
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
8 // * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above copyright
11 // notice, this list of conditions and the following disclaimer in the
12 // documentation and/or other materials provided with the distribution.
13 // * Neither the name of Google Inc. nor the names of its contributors
14 // may be used to endorse or promote products derived from this software
15 // without specific prior written permission.
17 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 #include "utils/cmdline/options.hpp"
34 #include "utils/cmdline/exceptions.hpp"
35 #include "utils/defs.hpp"
36 #include "utils/format/macros.hpp"
37 #include "utils/fs/exceptions.hpp"
38 #include "utils/sanity.hpp"
39 #include "utils/text/operations.ipp"
41 namespace cmdline
= utils::cmdline
;
42 namespace text
= utils::text
;
44 #if defined(__minix) && defined(NDEBUG)
46 #define PRE_MSG(expr, msg) \
49 utils::sanity_failure(utils::precondition, __FILE__, __LINE__, msg); \
51 #endif /* defined(__minix) && defined(NDEBUG) */
53 /// Constructs a generic option with both a short and a long name.
55 /// \param short_name_ The short name for the option.
56 /// \param long_name_ The long name for the option.
57 /// \param description_ A user-friendly description for the option.
58 /// \param arg_name_ If not NULL, specifies that the option must receive an
59 /// argument and specifies the name of such argument for documentation
61 /// \param default_value_ If not NULL, specifies that the option has a default
62 /// value for the mandatory argument.
63 cmdline::base_option::base_option(const char short_name_
,
64 const char* long_name_
,
65 const char* description_
,
66 const char* arg_name_
,
67 const char* default_value_
) :
68 _short_name(short_name_
),
69 _long_name(long_name_
),
70 _description(description_
),
71 _arg_name(arg_name_
== NULL
? "" : arg_name_
),
72 _has_default_value(default_value_
!= NULL
),
73 _default_value(default_value_
== NULL
? "" : default_value_
)
75 INV(short_name_
!= '\0');
79 /// Constructs a generic option with a long name only.
81 /// \param long_name_ The long name for the option.
82 /// \param description_ A user-friendly description for the option.
83 /// \param arg_name_ If not NULL, specifies that the option must receive an
84 /// argument and specifies the name of such argument for documentation
86 /// \param default_value_ If not NULL, specifies that the option has a default
87 /// value for the mandatory argument.
88 cmdline::base_option::base_option(const char* long_name_
,
89 const char* description_
,
90 const char* arg_name_
,
91 const char* default_value_
) :
93 _long_name(long_name_
),
94 _description(description_
),
95 _arg_name(arg_name_
== NULL
? "" : arg_name_
),
96 _has_default_value(default_value_
!= NULL
),
97 _default_value(default_value_
== NULL
? "" : default_value_
)
102 /// Destructor for the option.
103 cmdline::base_option::~base_option(void)
108 /// Checks whether the option has a short name or not.
110 /// \return True if the option has a short name, false otherwise.
112 cmdline::base_option::has_short_name(void) const
114 return _short_name
!= '\0';
118 /// Returns the short name of the option.
120 /// \pre has_short_name() must be true.
122 /// \return The short name.
124 cmdline::base_option::short_name(void) const
126 PRE(has_short_name());
131 /// Returns the long name of the option.
133 /// \return The long name.
135 cmdline::base_option::long_name(void) const
141 /// Returns the description of the option.
143 /// \return The description.
145 cmdline::base_option::description(void) const
151 /// Checks whether the option needs an argument or not.
153 /// \return True if the option needs an argument, false otherwise.
155 cmdline::base_option::needs_arg(void) const
157 return !_arg_name
.empty();
161 /// Returns the argument name of the option for documentation purposes.
163 /// \pre needs_arg() must be true.
165 /// \return The argument name.
167 cmdline::base_option::arg_name(void) const
174 /// Checks whether the option has a default value for its argument.
176 /// \pre needs_arg() must be true.
178 /// \return True if the option has a default value, false otherwise.
180 cmdline::base_option::has_default_value(void) const
183 return _has_default_value
;
187 /// Returns the default value for the argument to the option.
189 /// \pre has_default_value() must be true.
191 /// \return The default value.
193 cmdline::base_option::default_value(void) const
195 INV(has_default_value());
196 return _default_value
;;
200 /// Formats the short name of the option for documentation purposes.
202 /// \return A string describing the option's short name.
204 cmdline::base_option::format_short_name(void) const
206 PRE(has_short_name());
209 return F("-%s %s") % short_name() % arg_name();
211 return F("-%s") % short_name();
216 /// Formats the long name of the option for documentation purposes.
218 /// \return A string describing the option's long name.
220 cmdline::base_option::format_long_name(void) const
223 return F("--%s=%s") % long_name() % arg_name();
225 return F("--%s") % long_name();
231 /// Ensures that an argument passed to the option is valid.
233 /// This must be reimplemented by subclasses that describe options with
236 /// \param unused_str The argument to validate as provided by the user in the
239 /// \throw cmdline::option_argument_value_error Subclasses must raise this
240 /// exception to indicate the cases in which str is invalid.
242 cmdline::base_option::validate(const std::string
& UTILS_UNUSED_PARAM(str
)) const
244 UNREACHABLE_MSG("Option does not support an argument");
248 /// Constructs a boolean option with both a short and a long name.
250 /// \param short_name_ The short name for the option.
251 /// \param long_name_ The long name for the option.
252 /// \param description_ A user-friendly description for the option.
253 cmdline::bool_option::bool_option(const char short_name_
,
254 const char* long_name_
,
255 const char* description_
) :
256 base_option(short_name_
, long_name_
, description_
)
261 /// Constructs a boolean option with a long name only.
263 /// \param long_name_ The long name for the option.
264 /// \param description_ A user-friendly description for the option.
265 cmdline::bool_option::bool_option(const char* long_name_
,
266 const char* description_
) :
267 base_option(long_name_
, description_
)
272 /// Constructs an integer option with both a short and a long name.
274 /// \param short_name_ The short name for the option.
275 /// \param long_name_ The long name for the option.
276 /// \param description_ A user-friendly description for the option.
277 /// \param arg_name_ The name of the mandatory argument, for documentation
279 /// \param default_value_ If not NULL, the default value for the mandatory
281 cmdline::int_option::int_option(const char short_name_
,
282 const char* long_name_
,
283 const char* description_
,
284 const char* arg_name_
,
285 const char* default_value_
) :
286 base_option(short_name_
, long_name_
, description_
, arg_name_
,
292 /// Constructs an integer option with a long name only.
294 /// \param long_name_ The long name for the option.
295 /// \param description_ A user-friendly description for the option.
296 /// \param arg_name_ The name of the mandatory argument, for documentation
298 /// \param default_value_ If not NULL, the default value for the mandatory
300 cmdline::int_option::int_option(const char* long_name_
,
301 const char* description_
,
302 const char* arg_name_
,
303 const char* default_value_
) :
304 base_option(long_name_
, description_
, arg_name_
, default_value_
)
309 /// Ensures that an integer argument passed to the int_option is valid.
311 /// \param raw_value The argument representing an integer as provided by the
314 /// \throw cmdline::option_argument_value_error If the integer provided in
315 /// raw_value is invalid.
317 cmdline::int_option::validate(const std::string
& raw_value
) const
320 (void)text::to_type
< int >(raw_value
);
321 } catch (const std::runtime_error
& e
) {
322 throw cmdline::option_argument_value_error(
323 F("--%s") % long_name(), raw_value
, "Not a valid integer");
328 /// Converts an integer argument to a native integer.
330 /// \param raw_value The argument representing an integer as provided by the
333 /// \return The integer.
335 /// \pre validate(raw_value) must be true.
337 cmdline::int_option::convert(const std::string
& raw_value
)
340 return text::to_type
< int >(raw_value
);
341 } catch (const std::runtime_error
& e
) {
342 PRE_MSG(false, F("Raw value '%s' for int option not properly "
343 "validated: %s") % raw_value
% e
.what());
348 /// Constructs a list option with both a short and a long name.
350 /// \param short_name_ The short name for the option.
351 /// \param long_name_ The long name for the option.
352 /// \param description_ A user-friendly description for the option.
353 /// \param arg_name_ The name of the mandatory argument, for documentation
355 /// \param default_value_ If not NULL, the default value for the mandatory
357 cmdline::list_option::list_option(const char short_name_
,
358 const char* long_name_
,
359 const char* description_
,
360 const char* arg_name_
,
361 const char* default_value_
) :
362 base_option(short_name_
, long_name_
, description_
, arg_name_
,
368 /// Constructs a list option with a long name only.
370 /// \param long_name_ The long name for the option.
371 /// \param description_ A user-friendly description for the option.
372 /// \param arg_name_ The name of the mandatory argument, for documentation
374 /// \param default_value_ If not NULL, the default value for the mandatory
376 cmdline::list_option::list_option(const char* long_name_
,
377 const char* description_
,
378 const char* arg_name_
,
379 const char* default_value_
) :
380 base_option(long_name_
, description_
, arg_name_
, default_value_
)
385 /// Ensures that a lisstring argument passed to the list_option is valid.
387 /// \param unused_raw_value The argument representing a list as provided by the
390 cmdline::list_option::validate(
391 const std::string
& UTILS_UNUSED_PARAM(raw_value
)) const
393 // Any list is potentially valid; the caller must check for semantics.
397 /// Converts a string argument to a vector.
399 /// \param raw_value The argument representing a list as provided by the user.
401 /// \return The list.
403 /// \pre validate(raw_value) must be true.
404 cmdline::list_option::option_type
405 cmdline::list_option::convert(const std::string
& raw_value
)
408 return text::split(raw_value
, ',');
409 } catch (const std::runtime_error
& e
) {
410 PRE_MSG(false, F("Raw value '%s' for list option not properly "
411 "validated: %s") % raw_value
% e
.what());
416 /// Constructs a path option with both a short and a long name.
418 /// \param short_name_ The short name for the option.
419 /// \param long_name_ The long name for the option.
420 /// \param description_ A user-friendly description for the option.
421 /// \param arg_name_ The name of the mandatory argument, for documentation
423 /// \param default_value_ If not NULL, the default value for the mandatory
425 cmdline::path_option::path_option(const char short_name_
,
426 const char* long_name_
,
427 const char* description_
,
428 const char* arg_name_
,
429 const char* default_value_
) :
430 base_option(short_name_
, long_name_
, description_
, arg_name_
,
436 /// Constructs a path option with a long name only.
438 /// \param long_name_ The long name for the option.
439 /// \param description_ A user-friendly description for the option.
440 /// \param arg_name_ The name of the mandatory argument, for documentation
442 /// \param default_value_ If not NULL, the default value for the mandatory
444 cmdline::path_option::path_option(const char* long_name_
,
445 const char* description_
,
446 const char* arg_name_
,
447 const char* default_value_
) :
448 base_option(long_name_
, description_
, arg_name_
, default_value_
)
453 /// Ensures that a path argument passed to the path_option is valid.
455 /// \param raw_value The argument representing a path as provided by the user.
457 /// \throw cmdline::option_argument_value_error If the path provided in
458 /// raw_value is invalid.
460 cmdline::path_option::validate(const std::string
& raw_value
) const
463 (void)utils::fs::path(raw_value
);
464 } catch (const utils::fs::error
& e
) {
465 throw cmdline::option_argument_value_error(F("--%s") % long_name(),
466 raw_value
, e
.what());
471 /// Converts a path argument to a utils::fs::path.
473 /// \param raw_value The argument representing a path as provided by the user.
475 /// \return The path.
477 /// \pre validate(raw_value) must be true.
479 cmdline::path_option::convert(const std::string
& raw_value
)
482 return utils::fs::path(raw_value
);
483 } catch (const std::runtime_error
& e
) {
484 PRE_MSG(false, F("Raw value '%s' for path option not properly "
485 "validated: %s") % raw_value
% e
.what());
490 /// Constructs a property option with both a short and a long name.
492 /// \param short_name_ The short name for the option.
493 /// \param long_name_ The long name for the option.
494 /// \param description_ A user-friendly description for the option.
495 /// \param arg_name_ The name of the mandatory argument, for documentation
496 /// purposes. Must include the '=' delimiter.
497 cmdline::property_option::property_option(const char short_name_
,
498 const char* long_name_
,
499 const char* description_
,
500 const char* arg_name_
) :
501 base_option(short_name_
, long_name_
, description_
, arg_name_
)
503 PRE(arg_name().find('=') != std::string::npos
);
507 /// Constructs a property option with a long name only.
509 /// \param long_name_ The long name for the option.
510 /// \param description_ A user-friendly description for the option.
511 /// \param arg_name_ The name of the mandatory argument, for documentation
512 /// purposes. Must include the '=' delimiter.
513 cmdline::property_option::property_option(const char* long_name_
,
514 const char* description_
,
515 const char* arg_name_
) :
516 base_option(long_name_
, description_
, arg_name_
)
518 PRE(arg_name().find('=') != std::string::npos
);
522 /// Validates the argument to a property option.
524 /// \param raw_value The argument provided by the user.
526 cmdline::property_option::validate(const std::string
& raw_value
) const
528 const std::string::size_type pos
= raw_value
.find('=');
529 if (pos
== std::string::npos
)
530 throw cmdline::option_argument_value_error(
531 F("--%s") % long_name(), raw_value
,
532 F("Argument does not have the form '%s'") % arg_name());
534 const std::string key
= raw_value
.substr(0, pos
);
536 throw cmdline::option_argument_value_error(
537 F("--%s") % long_name(), raw_value
, "Empty property name");
539 const std::string value
= raw_value
.substr(pos
+ 1);
541 throw cmdline::option_argument_value_error(
542 F("--%s") % long_name(), raw_value
, "Empty value");
546 /// Returns the property option in a key/value pair form.
548 /// \param raw_value The argument provided by the user.
550 /// \return raw_value The key/value pair representation of the property.
552 /// \pre validate(raw_value) must be true.
553 cmdline::property_option::option_type
554 cmdline::property_option::convert(const std::string
& raw_value
)
556 const std::string::size_type pos
= raw_value
.find('=');
557 return std::make_pair(raw_value
.substr(0, pos
), raw_value
.substr(pos
+ 1));
561 /// Constructs a string option with both a short and a long name.
563 /// \param short_name_ The short name for the option.
564 /// \param long_name_ The long name for the option.
565 /// \param description_ A user-friendly description for the option.
566 /// \param arg_name_ The name of the mandatory argument, for documentation
568 /// \param default_value_ If not NULL, the default value for the mandatory
570 cmdline::string_option::string_option(const char short_name_
,
571 const char* long_name_
,
572 const char* description_
,
573 const char* arg_name_
,
574 const char* default_value_
) :
575 base_option(short_name_
, long_name_
, description_
, arg_name_
,
581 /// Constructs a string option with a long name only.
583 /// \param long_name_ The long name for the option.
584 /// \param description_ A user-friendly description for the option.
585 /// \param arg_name_ The name of the mandatory argument, for documentation
587 /// \param default_value_ If not NULL, the default value for the mandatory
589 cmdline::string_option::string_option(const char* long_name_
,
590 const char* description_
,
591 const char* arg_name_
,
592 const char* default_value_
) :
593 base_option(long_name_
, description_
, arg_name_
, default_value_
)
598 /// Does nothing; all string values are valid arguments to a string_option.
600 /// \param unused_raw_value The argument provided by the user.
602 cmdline::string_option::validate(
603 const std::string
& UTILS_UNUSED_PARAM(raw_value
)) const
609 /// Returns the string unmodified.
611 /// \param raw_value The argument provided by the user.
613 /// \return raw_value
615 /// \pre validate(raw_value) must be true.
617 cmdline::string_option::convert(const std::string
& raw_value
)