xref: /freebsd/contrib/kyua/utils/cmdline/exceptions.cpp (revision ebacd8013fe5f7fdf9f6a5b286f6680dd2891036)
1 // Copyright 2010 The Kyua Authors.
2 // All rights reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
6 // met:
7 //
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.
16 //
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.
28 
29 #include "utils/cmdline/exceptions.hpp"
30 
31 #include "utils/format/macros.hpp"
32 #include "utils/sanity.hpp"
33 
34 namespace cmdline = utils::cmdline;
35 
36 
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);
41 
42 
43 /// Constructs a new error with a plain-text message.
44 ///
45 /// \param message The plain-text error message.
46 cmdline::error::error(const std::string& message) :
47     std::runtime_error(message)
48 {
49 }
50 
51 
52 /// Destructor for the error.
53 cmdline::error::~error(void) throw()
54 {
55 }
56 
57 
58 /// Constructs a new usage_error.
59 ///
60 /// \param message The reason behind the usage error.
61 cmdline::usage_error::usage_error(const std::string& message) :
62     error(message)
63 {
64 }
65 
66 
67 /// Destructor for the error.
68 cmdline::usage_error::~usage_error(void) throw()
69 {
70 }
71 
72 
73 /// Constructs a new missing_option_argument_error.
74 ///
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_),
80     _option(option_)
81 {
82     VALIDATE_OPTION_NAME(option_);
83 }
84 
85 
86 /// Destructor for the error.
87 cmdline::missing_option_argument_error::~missing_option_argument_error(void)
88     throw()
89 {
90 }
91 
92 
93 /// Returns the option name for which no argument was provided.
94 ///
95 /// \return The option name.
96 const std::string&
97 cmdline::missing_option_argument_error::option(void) const
98 {
99     return _option;
100 }
101 
102 
103 /// Constructs a new option_argument_value_error.
104 ///
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_ %
113                 option_ % reason_),
114     _option(option_),
115     _argument(argument_),
116     _reason(reason_)
117 {
118     VALIDATE_OPTION_NAME(option_);
119 }
120 
121 
122 /// Destructor for the error.
123 cmdline::option_argument_value_error::~option_argument_value_error(void)
124     throw()
125 {
126 }
127 
128 
129 /// Returns the option to which the invalid argument was passed.
130 ///
131 /// \return The option name.
132 const std::string&
133 cmdline::option_argument_value_error::option(void) const
134 {
135     return _option;
136 }
137 
138 
139 /// Returns the invalid argument value.
140 ///
141 /// \return The invalid argument.
142 const std::string&
143 cmdline::option_argument_value_error::argument(void) const
144 {
145     return _argument;
146 }
147 
148 
149 /// Constructs a new unknown_option_error.
150 ///
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_),
156     _option(option_)
157 {
158     VALIDATE_OPTION_NAME(option_);
159 }
160 
161 
162 /// Destructor for the error.
163 cmdline::unknown_option_error::~unknown_option_error(void) throw()
164 {
165 }
166 
167 
168 /// Returns the unknown option name.
169 ///
170 /// \return The unknown option.
171 const std::string&
172 cmdline::unknown_option_error::option(void) const
173 {
174     return _option;
175 }
176