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/exceptions.hpp"
31 #include "utils/format/macros.hpp"
32 #include "utils/sanity.hpp"
34 namespace cmdline
= utils::cmdline
;
37 #define VALIDATE_OPTION_NAME(option) PRE_MSG( \
38 (option.length() == 2 && (option[0] == '-' && option[1] != '-')) || \
39 (option.length() > 2 && (option[0] == '-' && option[1] == '-')), \
40 F("The option name %s must be fully specified") % option);
43 /// Constructs a new error with a plain-text message.
45 /// \param message The plain-text error message.
46 cmdline::error::error(const std::string
& message
) :
47 std::runtime_error(message
)
52 /// Destructor for the error.
53 cmdline::error::~error(void) throw()
58 /// Constructs a new usage_error.
60 /// \param message The reason behind the usage error.
61 cmdline::usage_error::usage_error(const std::string
& message
) :
67 /// Destructor for the error.
68 cmdline::usage_error::~usage_error(void) throw()
73 /// Constructs a new missing_option_argument_error.
75 /// \param option_ The option for which no argument was provided. The option
76 /// name must be fully specified (with - or -- in front).
77 cmdline::missing_option_argument_error::missing_option_argument_error(
78 const std::string
& option_
) :
79 usage_error(F("Missing required argument for option %s") % option_
),
82 VALIDATE_OPTION_NAME(option_
);
86 /// Destructor for the error.
87 cmdline::missing_option_argument_error::~missing_option_argument_error(void)
93 /// Returns the option name for which no argument was provided.
95 /// \return The option name.
97 cmdline::missing_option_argument_error::option(void) const
103 /// Constructs a new option_argument_value_error.
105 /// \param option_ The option to which an invalid argument was passed. The
106 /// option name must be fully specified (with - or -- in front).
107 /// \param argument_ The invalid argument.
108 /// \param reason_ The reason describing why the argument is invalid.
109 cmdline::option_argument_value_error::option_argument_value_error(
110 const std::string
& option_
, const std::string
& argument_
,
111 const std::string
& reason_
) :
112 usage_error(F("Invalid argument '%s' for option %s: %s") % argument_
%
115 _argument(argument_
),
118 VALIDATE_OPTION_NAME(option_
);
122 /// Destructor for the error.
123 cmdline::option_argument_value_error::~option_argument_value_error(void)
129 /// Returns the option to which the invalid argument was passed.
131 /// \return The option name.
133 cmdline::option_argument_value_error::option(void) const
139 /// Returns the invalid argument value.
141 /// \return The invalid argument.
143 cmdline::option_argument_value_error::argument(void) const
149 /// Constructs a new unknown_option_error.
151 /// \param option_ The unknown option. The option name must be fully specified
152 /// (with - or -- in front).
153 cmdline::unknown_option_error::unknown_option_error(
154 const std::string
& option_
) :
155 usage_error(F("Unknown option %s") % option_
),
158 VALIDATE_OPTION_NAME(option_
);
162 /// Destructor for the error.
163 cmdline::unknown_option_error::~unknown_option_error(void) throw()
168 /// Returns the unknown option name.
170 /// \return The unknown option.
172 cmdline::unknown_option_error::option(void) const