xref: /minix3/external/bsd/kyua-cli/dist/utils/cmdline/options.cpp (revision 3260d16f34636651fb9ef891a1ffe03fd23b513d)
111be35a1SLionel Sambuc // Copyright 2010 Google Inc.
211be35a1SLionel Sambuc // All rights reserved.
311be35a1SLionel Sambuc //
411be35a1SLionel Sambuc // Redistribution and use in source and binary forms, with or without
511be35a1SLionel Sambuc // modification, are permitted provided that the following conditions are
611be35a1SLionel Sambuc // met:
711be35a1SLionel Sambuc //
811be35a1SLionel Sambuc // * Redistributions of source code must retain the above copyright
911be35a1SLionel Sambuc //   notice, this list of conditions and the following disclaimer.
1011be35a1SLionel Sambuc // * Redistributions in binary form must reproduce the above copyright
1111be35a1SLionel Sambuc //   notice, this list of conditions and the following disclaimer in the
1211be35a1SLionel Sambuc //   documentation and/or other materials provided with the distribution.
1311be35a1SLionel Sambuc // * Neither the name of Google Inc. nor the names of its contributors
1411be35a1SLionel Sambuc //   may be used to endorse or promote products derived from this software
1511be35a1SLionel Sambuc //   without specific prior written permission.
1611be35a1SLionel Sambuc //
1711be35a1SLionel Sambuc // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
1811be35a1SLionel Sambuc // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
1911be35a1SLionel Sambuc // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
2011be35a1SLionel Sambuc // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
2111be35a1SLionel Sambuc // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
2211be35a1SLionel Sambuc // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
2311be35a1SLionel Sambuc // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2411be35a1SLionel Sambuc // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2511be35a1SLionel Sambuc // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2611be35a1SLionel Sambuc // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
2711be35a1SLionel Sambuc // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2811be35a1SLionel Sambuc 
2911be35a1SLionel Sambuc #include "utils/cmdline/options.hpp"
3011be35a1SLionel Sambuc 
3111be35a1SLionel Sambuc #include <stdexcept>
3211be35a1SLionel Sambuc #include <vector>
3311be35a1SLionel Sambuc 
3411be35a1SLionel Sambuc #include "utils/cmdline/exceptions.hpp"
3511be35a1SLionel Sambuc #include "utils/defs.hpp"
3611be35a1SLionel Sambuc #include "utils/format/macros.hpp"
3711be35a1SLionel Sambuc #include "utils/fs/exceptions.hpp"
3811be35a1SLionel Sambuc #include "utils/sanity.hpp"
3911be35a1SLionel Sambuc #include "utils/text/operations.ipp"
4011be35a1SLionel Sambuc 
4111be35a1SLionel Sambuc namespace cmdline = utils::cmdline;
4211be35a1SLionel Sambuc namespace text = utils::text;
4311be35a1SLionel Sambuc 
44*3260d16fSLionel Sambuc #if defined(__minix) && defined(NDEBUG)
45*3260d16fSLionel Sambuc #undef PRE_MSG
46*3260d16fSLionel Sambuc #define PRE_MSG(expr, msg) \
47*3260d16fSLionel Sambuc     do { \
48*3260d16fSLionel Sambuc         if (!(expr)) \
49*3260d16fSLionel Sambuc             utils::sanity_failure(utils::precondition, __FILE__, __LINE__, msg); \
50*3260d16fSLionel Sambuc     } while (0)
51*3260d16fSLionel Sambuc #endif /* defined(__minix) && defined(NDEBUG) */
5211be35a1SLionel Sambuc 
5311be35a1SLionel Sambuc /// Constructs a generic option with both a short and a long name.
5411be35a1SLionel Sambuc ///
5511be35a1SLionel Sambuc /// \param short_name_ The short name for the option.
5611be35a1SLionel Sambuc /// \param long_name_ The long name for the option.
5711be35a1SLionel Sambuc /// \param description_ A user-friendly description for the option.
5811be35a1SLionel Sambuc /// \param arg_name_ If not NULL, specifies that the option must receive an
5911be35a1SLionel Sambuc ///     argument and specifies the name of such argument for documentation
6011be35a1SLionel Sambuc ///     purposes.
6111be35a1SLionel Sambuc /// \param default_value_ If not NULL, specifies that the option has a default
6211be35a1SLionel Sambuc ///     value for the mandatory argument.
base_option(const char short_name_,const char * long_name_,const char * description_,const char * arg_name_,const char * default_value_)6311be35a1SLionel Sambuc cmdline::base_option::base_option(const char short_name_,
6411be35a1SLionel Sambuc                                   const char* long_name_,
6511be35a1SLionel Sambuc                                   const char* description_,
6611be35a1SLionel Sambuc                                   const char* arg_name_,
6711be35a1SLionel Sambuc                                   const char* default_value_) :
6811be35a1SLionel Sambuc     _short_name(short_name_),
6911be35a1SLionel Sambuc     _long_name(long_name_),
7011be35a1SLionel Sambuc     _description(description_),
7111be35a1SLionel Sambuc     _arg_name(arg_name_ == NULL ? "" : arg_name_),
7211be35a1SLionel Sambuc     _has_default_value(default_value_ != NULL),
7311be35a1SLionel Sambuc     _default_value(default_value_ == NULL ? "" : default_value_)
7411be35a1SLionel Sambuc {
7511be35a1SLionel Sambuc     INV(short_name_ != '\0');
7611be35a1SLionel Sambuc }
7711be35a1SLionel Sambuc 
7811be35a1SLionel Sambuc 
7911be35a1SLionel Sambuc /// Constructs a generic option with a long name only.
8011be35a1SLionel Sambuc ///
8111be35a1SLionel Sambuc /// \param long_name_ The long name for the option.
8211be35a1SLionel Sambuc /// \param description_ A user-friendly description for the option.
8311be35a1SLionel Sambuc /// \param arg_name_ If not NULL, specifies that the option must receive an
8411be35a1SLionel Sambuc ///     argument and specifies the name of such argument for documentation
8511be35a1SLionel Sambuc ///     purposes.
8611be35a1SLionel Sambuc /// \param default_value_ If not NULL, specifies that the option has a default
8711be35a1SLionel Sambuc ///     value for the mandatory argument.
base_option(const char * long_name_,const char * description_,const char * arg_name_,const char * default_value_)8811be35a1SLionel Sambuc cmdline::base_option::base_option(const char* long_name_,
8911be35a1SLionel Sambuc                                   const char* description_,
9011be35a1SLionel Sambuc                                   const char* arg_name_,
9111be35a1SLionel Sambuc                                   const char* default_value_) :
9211be35a1SLionel Sambuc     _short_name('\0'),
9311be35a1SLionel Sambuc     _long_name(long_name_),
9411be35a1SLionel Sambuc     _description(description_),
9511be35a1SLionel Sambuc     _arg_name(arg_name_ == NULL ? "" : arg_name_),
9611be35a1SLionel Sambuc     _has_default_value(default_value_ != NULL),
9711be35a1SLionel Sambuc     _default_value(default_value_ == NULL ? "" : default_value_)
9811be35a1SLionel Sambuc {
9911be35a1SLionel Sambuc }
10011be35a1SLionel Sambuc 
10111be35a1SLionel Sambuc 
10211be35a1SLionel Sambuc /// Destructor for the option.
~base_option(void)10311be35a1SLionel Sambuc cmdline::base_option::~base_option(void)
10411be35a1SLionel Sambuc {
10511be35a1SLionel Sambuc }
10611be35a1SLionel Sambuc 
10711be35a1SLionel Sambuc 
10811be35a1SLionel Sambuc /// Checks whether the option has a short name or not.
10911be35a1SLionel Sambuc ///
11011be35a1SLionel Sambuc /// \return True if the option has a short name, false otherwise.
11111be35a1SLionel Sambuc bool
has_short_name(void) const11211be35a1SLionel Sambuc cmdline::base_option::has_short_name(void) const
11311be35a1SLionel Sambuc {
11411be35a1SLionel Sambuc     return _short_name != '\0';
11511be35a1SLionel Sambuc }
11611be35a1SLionel Sambuc 
11711be35a1SLionel Sambuc 
11811be35a1SLionel Sambuc /// Returns the short name of the option.
11911be35a1SLionel Sambuc ///
12011be35a1SLionel Sambuc /// \pre has_short_name() must be true.
12111be35a1SLionel Sambuc ///
12211be35a1SLionel Sambuc /// \return The short name.
12311be35a1SLionel Sambuc char
short_name(void) const12411be35a1SLionel Sambuc cmdline::base_option::short_name(void) const
12511be35a1SLionel Sambuc {
12611be35a1SLionel Sambuc     PRE(has_short_name());
12711be35a1SLionel Sambuc     return _short_name;
12811be35a1SLionel Sambuc }
12911be35a1SLionel Sambuc 
13011be35a1SLionel Sambuc 
13111be35a1SLionel Sambuc /// Returns the long name of the option.
13211be35a1SLionel Sambuc ///
13311be35a1SLionel Sambuc /// \return The long name.
13411be35a1SLionel Sambuc const std::string&
long_name(void) const13511be35a1SLionel Sambuc cmdline::base_option::long_name(void) const
13611be35a1SLionel Sambuc {
13711be35a1SLionel Sambuc     return _long_name;
13811be35a1SLionel Sambuc }
13911be35a1SLionel Sambuc 
14011be35a1SLionel Sambuc 
14111be35a1SLionel Sambuc /// Returns the description of the option.
14211be35a1SLionel Sambuc ///
14311be35a1SLionel Sambuc /// \return The description.
14411be35a1SLionel Sambuc const std::string&
description(void) const14511be35a1SLionel Sambuc cmdline::base_option::description(void) const
14611be35a1SLionel Sambuc {
14711be35a1SLionel Sambuc     return _description;
14811be35a1SLionel Sambuc }
14911be35a1SLionel Sambuc 
15011be35a1SLionel Sambuc 
15111be35a1SLionel Sambuc /// Checks whether the option needs an argument or not.
15211be35a1SLionel Sambuc ///
15311be35a1SLionel Sambuc /// \return True if the option needs an argument, false otherwise.
15411be35a1SLionel Sambuc bool
needs_arg(void) const15511be35a1SLionel Sambuc cmdline::base_option::needs_arg(void) const
15611be35a1SLionel Sambuc {
15711be35a1SLionel Sambuc     return !_arg_name.empty();
15811be35a1SLionel Sambuc }
15911be35a1SLionel Sambuc 
16011be35a1SLionel Sambuc 
16111be35a1SLionel Sambuc /// Returns the argument name of the option for documentation purposes.
16211be35a1SLionel Sambuc ///
16311be35a1SLionel Sambuc /// \pre needs_arg() must be true.
16411be35a1SLionel Sambuc ///
16511be35a1SLionel Sambuc /// \return The argument name.
16611be35a1SLionel Sambuc const std::string&
arg_name(void) const16711be35a1SLionel Sambuc cmdline::base_option::arg_name(void) const
16811be35a1SLionel Sambuc {
16911be35a1SLionel Sambuc     INV(needs_arg());
17011be35a1SLionel Sambuc     return _arg_name;
17111be35a1SLionel Sambuc }
17211be35a1SLionel Sambuc 
17311be35a1SLionel Sambuc 
17411be35a1SLionel Sambuc /// Checks whether the option has a default value for its argument.
17511be35a1SLionel Sambuc ///
17611be35a1SLionel Sambuc /// \pre needs_arg() must be true.
17711be35a1SLionel Sambuc ///
17811be35a1SLionel Sambuc /// \return True if the option has a default value, false otherwise.
17911be35a1SLionel Sambuc bool
has_default_value(void) const18011be35a1SLionel Sambuc cmdline::base_option::has_default_value(void) const
18111be35a1SLionel Sambuc {
18211be35a1SLionel Sambuc     PRE(needs_arg());
18311be35a1SLionel Sambuc     return _has_default_value;
18411be35a1SLionel Sambuc }
18511be35a1SLionel Sambuc 
18611be35a1SLionel Sambuc 
18711be35a1SLionel Sambuc /// Returns the default value for the argument to the option.
18811be35a1SLionel Sambuc ///
18911be35a1SLionel Sambuc /// \pre has_default_value() must be true.
19011be35a1SLionel Sambuc ///
19111be35a1SLionel Sambuc /// \return The default value.
19211be35a1SLionel Sambuc const std::string&
default_value(void) const19311be35a1SLionel Sambuc cmdline::base_option::default_value(void) const
19411be35a1SLionel Sambuc {
19511be35a1SLionel Sambuc     INV(has_default_value());
19611be35a1SLionel Sambuc     return _default_value;;
19711be35a1SLionel Sambuc }
19811be35a1SLionel Sambuc 
19911be35a1SLionel Sambuc 
20011be35a1SLionel Sambuc /// Formats the short name of the option for documentation purposes.
20111be35a1SLionel Sambuc ///
20211be35a1SLionel Sambuc /// \return A string describing the option's short name.
20311be35a1SLionel Sambuc std::string
format_short_name(void) const20411be35a1SLionel Sambuc cmdline::base_option::format_short_name(void) const
20511be35a1SLionel Sambuc {
20611be35a1SLionel Sambuc     PRE(has_short_name());
20711be35a1SLionel Sambuc 
20811be35a1SLionel Sambuc     if (needs_arg()) {
20911be35a1SLionel Sambuc         return F("-%s %s") % short_name() % arg_name();
21011be35a1SLionel Sambuc     } else {
21111be35a1SLionel Sambuc         return F("-%s") % short_name();
21211be35a1SLionel Sambuc     }
21311be35a1SLionel Sambuc }
21411be35a1SLionel Sambuc 
21511be35a1SLionel Sambuc 
21611be35a1SLionel Sambuc /// Formats the long name of the option for documentation purposes.
21711be35a1SLionel Sambuc ///
21811be35a1SLionel Sambuc /// \return A string describing the option's long name.
21911be35a1SLionel Sambuc std::string
format_long_name(void) const22011be35a1SLionel Sambuc cmdline::base_option::format_long_name(void) const
22111be35a1SLionel Sambuc {
22211be35a1SLionel Sambuc     if (needs_arg()) {
22311be35a1SLionel Sambuc         return F("--%s=%s") % long_name() % arg_name();
22411be35a1SLionel Sambuc     } else {
22511be35a1SLionel Sambuc         return F("--%s") % long_name();
22611be35a1SLionel Sambuc     }
22711be35a1SLionel Sambuc }
22811be35a1SLionel Sambuc 
22911be35a1SLionel Sambuc 
23011be35a1SLionel Sambuc 
23111be35a1SLionel Sambuc /// Ensures that an argument passed to the option is valid.
23211be35a1SLionel Sambuc ///
23311be35a1SLionel Sambuc /// This must be reimplemented by subclasses that describe options with
23411be35a1SLionel Sambuc /// arguments.
23511be35a1SLionel Sambuc ///
23611be35a1SLionel Sambuc /// \param unused_str The argument to validate as provided by the user in the
23711be35a1SLionel Sambuc ///     command line.
23811be35a1SLionel Sambuc ///
23911be35a1SLionel Sambuc /// \throw cmdline::option_argument_value_error Subclasses must raise this
24011be35a1SLionel Sambuc ///     exception to indicate the cases in which str is invalid.
24111be35a1SLionel Sambuc void
validate(const std::string & UTILS_UNUSED_PARAM (str)) const24211be35a1SLionel Sambuc cmdline::base_option::validate(const std::string& UTILS_UNUSED_PARAM(str)) const
24311be35a1SLionel Sambuc {
24411be35a1SLionel Sambuc     UNREACHABLE_MSG("Option does not support an argument");
24511be35a1SLionel Sambuc }
24611be35a1SLionel Sambuc 
24711be35a1SLionel Sambuc 
24811be35a1SLionel Sambuc /// Constructs a boolean option with both a short and a long name.
24911be35a1SLionel Sambuc ///
25011be35a1SLionel Sambuc /// \param short_name_ The short name for the option.
25111be35a1SLionel Sambuc /// \param long_name_ The long name for the option.
25211be35a1SLionel Sambuc /// \param description_ A user-friendly description for the option.
bool_option(const char short_name_,const char * long_name_,const char * description_)25311be35a1SLionel Sambuc cmdline::bool_option::bool_option(const char short_name_,
25411be35a1SLionel Sambuc                                   const char* long_name_,
25511be35a1SLionel Sambuc                                   const char* description_) :
25611be35a1SLionel Sambuc     base_option(short_name_, long_name_, description_)
25711be35a1SLionel Sambuc {
25811be35a1SLionel Sambuc }
25911be35a1SLionel Sambuc 
26011be35a1SLionel Sambuc 
26111be35a1SLionel Sambuc /// Constructs a boolean option with a long name only.
26211be35a1SLionel Sambuc ///
26311be35a1SLionel Sambuc /// \param long_name_ The long name for the option.
26411be35a1SLionel Sambuc /// \param description_ A user-friendly description for the option.
bool_option(const char * long_name_,const char * description_)26511be35a1SLionel Sambuc cmdline::bool_option::bool_option(const char* long_name_,
26611be35a1SLionel Sambuc                                   const char* description_) :
26711be35a1SLionel Sambuc     base_option(long_name_, description_)
26811be35a1SLionel Sambuc {
26911be35a1SLionel Sambuc }
27011be35a1SLionel Sambuc 
27111be35a1SLionel Sambuc 
27211be35a1SLionel Sambuc /// Constructs an integer option with both a short and a long name.
27311be35a1SLionel Sambuc ///
27411be35a1SLionel Sambuc /// \param short_name_ The short name for the option.
27511be35a1SLionel Sambuc /// \param long_name_ The long name for the option.
27611be35a1SLionel Sambuc /// \param description_ A user-friendly description for the option.
27711be35a1SLionel Sambuc /// \param arg_name_ The name of the mandatory argument, for documentation
27811be35a1SLionel Sambuc ///     purposes.
27911be35a1SLionel Sambuc /// \param default_value_ If not NULL, the default value for the mandatory
28011be35a1SLionel Sambuc ///     argument.
int_option(const char short_name_,const char * long_name_,const char * description_,const char * arg_name_,const char * default_value_)28111be35a1SLionel Sambuc cmdline::int_option::int_option(const char short_name_,
28211be35a1SLionel Sambuc                                 const char* long_name_,
28311be35a1SLionel Sambuc                                 const char* description_,
28411be35a1SLionel Sambuc                                 const char* arg_name_,
28511be35a1SLionel Sambuc                                 const char* default_value_) :
28611be35a1SLionel Sambuc     base_option(short_name_, long_name_, description_, arg_name_,
28711be35a1SLionel Sambuc                 default_value_)
28811be35a1SLionel Sambuc {
28911be35a1SLionel Sambuc }
29011be35a1SLionel Sambuc 
29111be35a1SLionel Sambuc 
29211be35a1SLionel Sambuc /// Constructs an integer option with a long name only.
29311be35a1SLionel Sambuc ///
29411be35a1SLionel Sambuc /// \param long_name_ The long name for the option.
29511be35a1SLionel Sambuc /// \param description_ A user-friendly description for the option.
29611be35a1SLionel Sambuc /// \param arg_name_ The name of the mandatory argument, for documentation
29711be35a1SLionel Sambuc ///     purposes.
29811be35a1SLionel Sambuc /// \param default_value_ If not NULL, the default value for the mandatory
29911be35a1SLionel Sambuc ///     argument.
int_option(const char * long_name_,const char * description_,const char * arg_name_,const char * default_value_)30011be35a1SLionel Sambuc cmdline::int_option::int_option(const char* long_name_,
30111be35a1SLionel Sambuc                                 const char* description_,
30211be35a1SLionel Sambuc                                 const char* arg_name_,
30311be35a1SLionel Sambuc                                 const char* default_value_) :
30411be35a1SLionel Sambuc     base_option(long_name_, description_, arg_name_, default_value_)
30511be35a1SLionel Sambuc {
30611be35a1SLionel Sambuc }
30711be35a1SLionel Sambuc 
30811be35a1SLionel Sambuc 
30911be35a1SLionel Sambuc /// Ensures that an integer argument passed to the int_option is valid.
31011be35a1SLionel Sambuc ///
31111be35a1SLionel Sambuc /// \param raw_value The argument representing an integer as provided by the
31211be35a1SLionel Sambuc ///     user.
31311be35a1SLionel Sambuc ///
31411be35a1SLionel Sambuc /// \throw cmdline::option_argument_value_error If the integer provided in
31511be35a1SLionel Sambuc ///     raw_value is invalid.
31611be35a1SLionel Sambuc void
validate(const std::string & raw_value) const31711be35a1SLionel Sambuc cmdline::int_option::validate(const std::string& raw_value) const
31811be35a1SLionel Sambuc {
31911be35a1SLionel Sambuc     try {
32011be35a1SLionel Sambuc         (void)text::to_type< int >(raw_value);
32111be35a1SLionel Sambuc     } catch (const std::runtime_error& e) {
32211be35a1SLionel Sambuc         throw cmdline::option_argument_value_error(
32311be35a1SLionel Sambuc             F("--%s") % long_name(), raw_value, "Not a valid integer");
32411be35a1SLionel Sambuc     }
32511be35a1SLionel Sambuc }
32611be35a1SLionel Sambuc 
32711be35a1SLionel Sambuc 
32811be35a1SLionel Sambuc /// Converts an integer argument to a native integer.
32911be35a1SLionel Sambuc ///
33011be35a1SLionel Sambuc /// \param raw_value The argument representing an integer as provided by the
33111be35a1SLionel Sambuc ///     user.
33211be35a1SLionel Sambuc ///
33311be35a1SLionel Sambuc /// \return The integer.
33411be35a1SLionel Sambuc ///
33511be35a1SLionel Sambuc /// \pre validate(raw_value) must be true.
33611be35a1SLionel Sambuc int
convert(const std::string & raw_value)33711be35a1SLionel Sambuc cmdline::int_option::convert(const std::string& raw_value)
33811be35a1SLionel Sambuc {
33911be35a1SLionel Sambuc     try {
34011be35a1SLionel Sambuc         return text::to_type< int >(raw_value);
34111be35a1SLionel Sambuc     } catch (const std::runtime_error& e) {
34211be35a1SLionel Sambuc         PRE_MSG(false, F("Raw value '%s' for int option not properly "
34311be35a1SLionel Sambuc                          "validated: %s") % raw_value % e.what());
34411be35a1SLionel Sambuc     }
34511be35a1SLionel Sambuc }
34611be35a1SLionel Sambuc 
34711be35a1SLionel Sambuc 
34811be35a1SLionel Sambuc /// Constructs a list option with both a short and a long name.
34911be35a1SLionel Sambuc ///
35011be35a1SLionel Sambuc /// \param short_name_ The short name for the option.
35111be35a1SLionel Sambuc /// \param long_name_ The long name for the option.
35211be35a1SLionel Sambuc /// \param description_ A user-friendly description for the option.
35311be35a1SLionel Sambuc /// \param arg_name_ The name of the mandatory argument, for documentation
35411be35a1SLionel Sambuc ///     purposes.
35511be35a1SLionel Sambuc /// \param default_value_ If not NULL, the default value for the mandatory
35611be35a1SLionel Sambuc ///     argument.
list_option(const char short_name_,const char * long_name_,const char * description_,const char * arg_name_,const char * default_value_)35711be35a1SLionel Sambuc cmdline::list_option::list_option(const char short_name_,
35811be35a1SLionel Sambuc                                   const char* long_name_,
35911be35a1SLionel Sambuc                                   const char* description_,
36011be35a1SLionel Sambuc                                   const char* arg_name_,
36111be35a1SLionel Sambuc                                   const char* default_value_) :
36211be35a1SLionel Sambuc     base_option(short_name_, long_name_, description_, arg_name_,
36311be35a1SLionel Sambuc                 default_value_)
36411be35a1SLionel Sambuc {
36511be35a1SLionel Sambuc }
36611be35a1SLionel Sambuc 
36711be35a1SLionel Sambuc 
36811be35a1SLionel Sambuc /// Constructs a list option with a long name only.
36911be35a1SLionel Sambuc ///
37011be35a1SLionel Sambuc /// \param long_name_ The long name for the option.
37111be35a1SLionel Sambuc /// \param description_ A user-friendly description for the option.
37211be35a1SLionel Sambuc /// \param arg_name_ The name of the mandatory argument, for documentation
37311be35a1SLionel Sambuc ///     purposes.
37411be35a1SLionel Sambuc /// \param default_value_ If not NULL, the default value for the mandatory
37511be35a1SLionel Sambuc ///     argument.
list_option(const char * long_name_,const char * description_,const char * arg_name_,const char * default_value_)37611be35a1SLionel Sambuc cmdline::list_option::list_option(const char* long_name_,
37711be35a1SLionel Sambuc                                   const char* description_,
37811be35a1SLionel Sambuc                                   const char* arg_name_,
37911be35a1SLionel Sambuc                                   const char* default_value_) :
38011be35a1SLionel Sambuc     base_option(long_name_, description_, arg_name_, default_value_)
38111be35a1SLionel Sambuc {
38211be35a1SLionel Sambuc }
38311be35a1SLionel Sambuc 
38411be35a1SLionel Sambuc 
38511be35a1SLionel Sambuc /// Ensures that a lisstring argument passed to the list_option is valid.
38611be35a1SLionel Sambuc ///
38711be35a1SLionel Sambuc /// \param unused_raw_value The argument representing a list as provided by the
38811be35a1SLionel Sambuc ///     user.
38911be35a1SLionel Sambuc void
validate(const std::string & UTILS_UNUSED_PARAM (raw_value)) const39011be35a1SLionel Sambuc cmdline::list_option::validate(
39111be35a1SLionel Sambuc     const std::string& UTILS_UNUSED_PARAM(raw_value)) const
39211be35a1SLionel Sambuc {
39311be35a1SLionel Sambuc     // Any list is potentially valid; the caller must check for semantics.
39411be35a1SLionel Sambuc }
39511be35a1SLionel Sambuc 
39611be35a1SLionel Sambuc 
39711be35a1SLionel Sambuc /// Converts a string argument to a vector.
39811be35a1SLionel Sambuc ///
39911be35a1SLionel Sambuc /// \param raw_value The argument representing a list as provided by the user.
40011be35a1SLionel Sambuc ///
40111be35a1SLionel Sambuc /// \return The list.
40211be35a1SLionel Sambuc ///
40311be35a1SLionel Sambuc /// \pre validate(raw_value) must be true.
40411be35a1SLionel Sambuc cmdline::list_option::option_type
convert(const std::string & raw_value)40511be35a1SLionel Sambuc cmdline::list_option::convert(const std::string& raw_value)
40611be35a1SLionel Sambuc {
40711be35a1SLionel Sambuc     try {
40811be35a1SLionel Sambuc         return text::split(raw_value, ',');
40911be35a1SLionel Sambuc     } catch (const std::runtime_error& e) {
41011be35a1SLionel Sambuc         PRE_MSG(false, F("Raw value '%s' for list option not properly "
41111be35a1SLionel Sambuc                          "validated: %s") % raw_value % e.what());
41211be35a1SLionel Sambuc     }
41311be35a1SLionel Sambuc }
41411be35a1SLionel Sambuc 
41511be35a1SLionel Sambuc 
41611be35a1SLionel Sambuc /// Constructs a path option with both a short and a long name.
41711be35a1SLionel Sambuc ///
41811be35a1SLionel Sambuc /// \param short_name_ The short name for the option.
41911be35a1SLionel Sambuc /// \param long_name_ The long name for the option.
42011be35a1SLionel Sambuc /// \param description_ A user-friendly description for the option.
42111be35a1SLionel Sambuc /// \param arg_name_ The name of the mandatory argument, for documentation
42211be35a1SLionel Sambuc ///     purposes.
42311be35a1SLionel Sambuc /// \param default_value_ If not NULL, the default value for the mandatory
42411be35a1SLionel Sambuc ///     argument.
path_option(const char short_name_,const char * long_name_,const char * description_,const char * arg_name_,const char * default_value_)42511be35a1SLionel Sambuc cmdline::path_option::path_option(const char short_name_,
42611be35a1SLionel Sambuc                                   const char* long_name_,
42711be35a1SLionel Sambuc                                   const char* description_,
42811be35a1SLionel Sambuc                                   const char* arg_name_,
42911be35a1SLionel Sambuc                                   const char* default_value_) :
43011be35a1SLionel Sambuc     base_option(short_name_, long_name_, description_, arg_name_,
43111be35a1SLionel Sambuc                 default_value_)
43211be35a1SLionel Sambuc {
43311be35a1SLionel Sambuc }
43411be35a1SLionel Sambuc 
43511be35a1SLionel Sambuc 
43611be35a1SLionel Sambuc /// Constructs a path option with a long name only.
43711be35a1SLionel Sambuc ///
43811be35a1SLionel Sambuc /// \param long_name_ The long name for the option.
43911be35a1SLionel Sambuc /// \param description_ A user-friendly description for the option.
44011be35a1SLionel Sambuc /// \param arg_name_ The name of the mandatory argument, for documentation
44111be35a1SLionel Sambuc ///     purposes.
44211be35a1SLionel Sambuc /// \param default_value_ If not NULL, the default value for the mandatory
44311be35a1SLionel Sambuc ///     argument.
path_option(const char * long_name_,const char * description_,const char * arg_name_,const char * default_value_)44411be35a1SLionel Sambuc cmdline::path_option::path_option(const char* long_name_,
44511be35a1SLionel Sambuc                                   const char* description_,
44611be35a1SLionel Sambuc                                   const char* arg_name_,
44711be35a1SLionel Sambuc                                   const char* default_value_) :
44811be35a1SLionel Sambuc     base_option(long_name_, description_, arg_name_, default_value_)
44911be35a1SLionel Sambuc {
45011be35a1SLionel Sambuc }
45111be35a1SLionel Sambuc 
45211be35a1SLionel Sambuc 
45311be35a1SLionel Sambuc /// Ensures that a path argument passed to the path_option is valid.
45411be35a1SLionel Sambuc ///
45511be35a1SLionel Sambuc /// \param raw_value The argument representing a path as provided by the user.
45611be35a1SLionel Sambuc ///
45711be35a1SLionel Sambuc /// \throw cmdline::option_argument_value_error If the path provided in
45811be35a1SLionel Sambuc ///     raw_value is invalid.
45911be35a1SLionel Sambuc void
validate(const std::string & raw_value) const46011be35a1SLionel Sambuc cmdline::path_option::validate(const std::string& raw_value) const
46111be35a1SLionel Sambuc {
46211be35a1SLionel Sambuc     try {
46311be35a1SLionel Sambuc         (void)utils::fs::path(raw_value);
46411be35a1SLionel Sambuc     } catch (const utils::fs::error& e) {
46511be35a1SLionel Sambuc         throw cmdline::option_argument_value_error(F("--%s") % long_name(),
46611be35a1SLionel Sambuc                                                    raw_value, e.what());
46711be35a1SLionel Sambuc     }
46811be35a1SLionel Sambuc }
46911be35a1SLionel Sambuc 
47011be35a1SLionel Sambuc 
47111be35a1SLionel Sambuc /// Converts a path argument to a utils::fs::path.
47211be35a1SLionel Sambuc ///
47311be35a1SLionel Sambuc /// \param raw_value The argument representing a path as provided by the user.
47411be35a1SLionel Sambuc ///
47511be35a1SLionel Sambuc /// \return The path.
47611be35a1SLionel Sambuc ///
47711be35a1SLionel Sambuc /// \pre validate(raw_value) must be true.
47811be35a1SLionel Sambuc utils::fs::path
convert(const std::string & raw_value)47911be35a1SLionel Sambuc cmdline::path_option::convert(const std::string& raw_value)
48011be35a1SLionel Sambuc {
48111be35a1SLionel Sambuc     try {
48211be35a1SLionel Sambuc         return utils::fs::path(raw_value);
48311be35a1SLionel Sambuc     } catch (const std::runtime_error& e) {
48411be35a1SLionel Sambuc         PRE_MSG(false, F("Raw value '%s' for path option not properly "
48511be35a1SLionel Sambuc                          "validated: %s") % raw_value % e.what());
48611be35a1SLionel Sambuc     }
48711be35a1SLionel Sambuc }
48811be35a1SLionel Sambuc 
48911be35a1SLionel Sambuc 
49011be35a1SLionel Sambuc /// Constructs a property option with both a short and a long name.
49111be35a1SLionel Sambuc ///
49211be35a1SLionel Sambuc /// \param short_name_ The short name for the option.
49311be35a1SLionel Sambuc /// \param long_name_ The long name for the option.
49411be35a1SLionel Sambuc /// \param description_ A user-friendly description for the option.
49511be35a1SLionel Sambuc /// \param arg_name_ The name of the mandatory argument, for documentation
49611be35a1SLionel Sambuc ///     purposes.  Must include the '=' delimiter.
property_option(const char short_name_,const char * long_name_,const char * description_,const char * arg_name_)49711be35a1SLionel Sambuc cmdline::property_option::property_option(const char short_name_,
49811be35a1SLionel Sambuc                                           const char* long_name_,
49911be35a1SLionel Sambuc                                           const char* description_,
50011be35a1SLionel Sambuc                                           const char* arg_name_) :
50111be35a1SLionel Sambuc     base_option(short_name_, long_name_, description_, arg_name_)
50211be35a1SLionel Sambuc {
50311be35a1SLionel Sambuc     PRE(arg_name().find('=') != std::string::npos);
50411be35a1SLionel Sambuc }
50511be35a1SLionel Sambuc 
50611be35a1SLionel Sambuc 
50711be35a1SLionel Sambuc /// Constructs a property option with a long name only.
50811be35a1SLionel Sambuc ///
50911be35a1SLionel Sambuc /// \param long_name_ The long name for the option.
51011be35a1SLionel Sambuc /// \param description_ A user-friendly description for the option.
51111be35a1SLionel Sambuc /// \param arg_name_ The name of the mandatory argument, for documentation
51211be35a1SLionel Sambuc ///     purposes.  Must include the '=' delimiter.
property_option(const char * long_name_,const char * description_,const char * arg_name_)51311be35a1SLionel Sambuc cmdline::property_option::property_option(const char* long_name_,
51411be35a1SLionel Sambuc                                           const char* description_,
51511be35a1SLionel Sambuc                                           const char* arg_name_) :
51611be35a1SLionel Sambuc     base_option(long_name_, description_, arg_name_)
51711be35a1SLionel Sambuc {
51811be35a1SLionel Sambuc     PRE(arg_name().find('=') != std::string::npos);
51911be35a1SLionel Sambuc }
52011be35a1SLionel Sambuc 
52111be35a1SLionel Sambuc 
52211be35a1SLionel Sambuc /// Validates the argument to a property option.
52311be35a1SLionel Sambuc ///
52411be35a1SLionel Sambuc /// \param raw_value The argument provided by the user.
52511be35a1SLionel Sambuc void
validate(const std::string & raw_value) const52611be35a1SLionel Sambuc cmdline::property_option::validate(const std::string& raw_value) const
52711be35a1SLionel Sambuc {
52811be35a1SLionel Sambuc     const std::string::size_type pos = raw_value.find('=');
52911be35a1SLionel Sambuc     if (pos == std::string::npos)
53011be35a1SLionel Sambuc         throw cmdline::option_argument_value_error(
53111be35a1SLionel Sambuc             F("--%s") % long_name(), raw_value,
53211be35a1SLionel Sambuc             F("Argument does not have the form '%s'") % arg_name());
53311be35a1SLionel Sambuc 
53411be35a1SLionel Sambuc     const std::string key = raw_value.substr(0, pos);
53511be35a1SLionel Sambuc     if (key.empty())
53611be35a1SLionel Sambuc         throw cmdline::option_argument_value_error(
53711be35a1SLionel Sambuc             F("--%s") % long_name(), raw_value, "Empty property name");
53811be35a1SLionel Sambuc 
53911be35a1SLionel Sambuc     const std::string value = raw_value.substr(pos + 1);
54011be35a1SLionel Sambuc     if (value.empty())
54111be35a1SLionel Sambuc         throw cmdline::option_argument_value_error(
54211be35a1SLionel Sambuc             F("--%s") % long_name(), raw_value, "Empty value");
54311be35a1SLionel Sambuc }
54411be35a1SLionel Sambuc 
54511be35a1SLionel Sambuc 
54611be35a1SLionel Sambuc /// Returns the property option in a key/value pair form.
54711be35a1SLionel Sambuc ///
54811be35a1SLionel Sambuc /// \param raw_value The argument provided by the user.
54911be35a1SLionel Sambuc ///
55011be35a1SLionel Sambuc /// \return raw_value The key/value pair representation of the property.
55111be35a1SLionel Sambuc ///
55211be35a1SLionel Sambuc /// \pre validate(raw_value) must be true.
55311be35a1SLionel Sambuc cmdline::property_option::option_type
convert(const std::string & raw_value)55411be35a1SLionel Sambuc cmdline::property_option::convert(const std::string& raw_value)
55511be35a1SLionel Sambuc {
55611be35a1SLionel Sambuc     const std::string::size_type pos = raw_value.find('=');
55711be35a1SLionel Sambuc     return std::make_pair(raw_value.substr(0, pos), raw_value.substr(pos + 1));
55811be35a1SLionel Sambuc }
55911be35a1SLionel Sambuc 
56011be35a1SLionel Sambuc 
56111be35a1SLionel Sambuc /// Constructs a string option with both a short and a long name.
56211be35a1SLionel Sambuc ///
56311be35a1SLionel Sambuc /// \param short_name_ The short name for the option.
56411be35a1SLionel Sambuc /// \param long_name_ The long name for the option.
56511be35a1SLionel Sambuc /// \param description_ A user-friendly description for the option.
56611be35a1SLionel Sambuc /// \param arg_name_ The name of the mandatory argument, for documentation
56711be35a1SLionel Sambuc ///     purposes.
56811be35a1SLionel Sambuc /// \param default_value_ If not NULL, the default value for the mandatory
56911be35a1SLionel Sambuc ///     argument.
string_option(const char short_name_,const char * long_name_,const char * description_,const char * arg_name_,const char * default_value_)57011be35a1SLionel Sambuc cmdline::string_option::string_option(const char short_name_,
57111be35a1SLionel Sambuc                                       const char* long_name_,
57211be35a1SLionel Sambuc                                       const char* description_,
57311be35a1SLionel Sambuc                                       const char* arg_name_,
57411be35a1SLionel Sambuc                                       const char* default_value_) :
57511be35a1SLionel Sambuc     base_option(short_name_, long_name_, description_, arg_name_,
57611be35a1SLionel Sambuc                 default_value_)
57711be35a1SLionel Sambuc {
57811be35a1SLionel Sambuc }
57911be35a1SLionel Sambuc 
58011be35a1SLionel Sambuc 
58111be35a1SLionel Sambuc /// Constructs a string option with a long name only.
58211be35a1SLionel Sambuc ///
58311be35a1SLionel Sambuc /// \param long_name_ The long name for the option.
58411be35a1SLionel Sambuc /// \param description_ A user-friendly description for the option.
58511be35a1SLionel Sambuc /// \param arg_name_ The name of the mandatory argument, for documentation
58611be35a1SLionel Sambuc ///     purposes.
58711be35a1SLionel Sambuc /// \param default_value_ If not NULL, the default value for the mandatory
58811be35a1SLionel Sambuc ///     argument.
string_option(const char * long_name_,const char * description_,const char * arg_name_,const char * default_value_)58911be35a1SLionel Sambuc cmdline::string_option::string_option(const char* long_name_,
59011be35a1SLionel Sambuc                                       const char* description_,
59111be35a1SLionel Sambuc                                       const char* arg_name_,
59211be35a1SLionel Sambuc                                       const char* default_value_) :
59311be35a1SLionel Sambuc     base_option(long_name_, description_, arg_name_, default_value_)
59411be35a1SLionel Sambuc {
59511be35a1SLionel Sambuc }
59611be35a1SLionel Sambuc 
59711be35a1SLionel Sambuc 
59811be35a1SLionel Sambuc /// Does nothing; all string values are valid arguments to a string_option.
59911be35a1SLionel Sambuc ///
60011be35a1SLionel Sambuc /// \param unused_raw_value The argument provided by the user.
60111be35a1SLionel Sambuc void
validate(const std::string & UTILS_UNUSED_PARAM (raw_value)) const60211be35a1SLionel Sambuc cmdline::string_option::validate(
60311be35a1SLionel Sambuc     const std::string& UTILS_UNUSED_PARAM(raw_value)) const
60411be35a1SLionel Sambuc {
60511be35a1SLionel Sambuc     // Nothing to do.
60611be35a1SLionel Sambuc }
60711be35a1SLionel Sambuc 
60811be35a1SLionel Sambuc 
60911be35a1SLionel Sambuc /// Returns the string unmodified.
61011be35a1SLionel Sambuc ///
61111be35a1SLionel Sambuc /// \param raw_value The argument provided by the user.
61211be35a1SLionel Sambuc ///
61311be35a1SLionel Sambuc /// \return raw_value
61411be35a1SLionel Sambuc ///
61511be35a1SLionel Sambuc /// \pre validate(raw_value) must be true.
61611be35a1SLionel Sambuc std::string
convert(const std::string & raw_value)61711be35a1SLionel Sambuc cmdline::string_option::convert(const std::string& raw_value)
61811be35a1SLionel Sambuc {
61911be35a1SLionel Sambuc     return raw_value;
62011be35a1SLionel Sambuc }
621