1 // Copyright Vladimir Prus 2002-2004.
\r
2 // Distributed under the Boost Software License, Version 1.0.
\r
3 // (See accompanying file LICENSE_1_0.txt
\r
4 // or copy at http://www.boost.org/LICENSE_1_0.txt)
\r
7 #ifndef BOOST_ERRORS_VP_2003_01_02
\r
8 #define BOOST_ERRORS_VP_2003_01_02
\r
10 #include <boost/program_options/config.hpp>
\r
13 #include <stdexcept>
\r
18 namespace boost { namespace program_options {
\r
20 /** Base class for all errors in the library. */
\r
21 class BOOST_PROGRAM_OPTIONS_DECL error : public std::logic_error {
\r
23 error(const std::string& what) : std::logic_error(what) {}
\r
26 class BOOST_PROGRAM_OPTIONS_DECL invalid_syntax : public error {
\r
29 long_not_allowed = 30,
\r
30 long_adjacent_not_allowed,
\r
31 short_adjacent_not_allowed,
\r
32 empty_adjacent_parameter,
\r
38 invalid_syntax(const std::string& tokens, kind_t kind);
\r
40 // gcc says that throw specification on dtor is loosened
\r
41 // without this line
\r
42 ~invalid_syntax() throw() {}
\r
44 kind_t kind() const;
\r
46 const std::string& tokens() const;
\r
49 /** Used to convert kind_t to a related error text */
\r
50 static std::string error_message(kind_t kind);
\r
53 // TODO: copy ctor might throw
\r
54 std::string m_tokens;
\r
59 /** Class thrown when option name is not recognized. */
\r
60 class BOOST_PROGRAM_OPTIONS_DECL unknown_option : public error {
\r
62 unknown_option(const std::string& name)
\r
63 : error(std::string("unknown option ").append(name)),
\r
67 // gcc says that throw specification on dtor is loosened
\r
68 // without this line
\r
69 ~unknown_option() throw() {}
\r
71 const std::string& get_option_name() const throw();
\r
74 std::string m_option_name;
\r
77 /** Class thrown when there's ambiguity amoung several possible options. */
\r
78 class BOOST_PROGRAM_OPTIONS_DECL ambiguous_option : public error {
\r
80 ambiguous_option(const std::string& name,
\r
81 const std::vector<std::string>& alternatives)
\r
82 : error(std::string("ambiguous option ").append(name))
\r
83 , m_alternatives(alternatives)
\r
84 , m_option_name(name)
\r
87 ~ambiguous_option() throw() {}
\r
89 const std::string& get_option_name() const throw();
\r
91 const std::vector<std::string>& alternatives() const throw();
\r
94 // TODO: copy ctor might throw
\r
95 std::vector<std::string> m_alternatives;
\r
96 std::string m_option_name;
\r
99 /** Class thrown when there are several option values, but
\r
100 user called a method which cannot return them all. */
\r
101 class BOOST_PROGRAM_OPTIONS_DECL multiple_values : public error {
\r
104 : error("multiple values")
\r
105 , m_option_name() {}
\r
107 ~multiple_values() throw() {}
\r
109 void set_option_name(const std::string& option);
\r
111 const std::string& get_option_name() const throw();
\r
114 std::string m_option_name; // The name of the option which
\r
115 // caused the exception.
\r
118 /** Class thrown when there are several occurrences of an
\r
119 option, but user called a method which cannot return
\r
121 class BOOST_PROGRAM_OPTIONS_DECL multiple_occurrences : public error {
\r
123 multiple_occurrences()
\r
124 : error("multiple occurrences")
\r
125 , m_option_name() {}
\r
127 ~multiple_occurrences() throw() {}
\r
129 void set_option_name(const std::string& option);
\r
131 const std::string& get_option_name() const throw();
\r
134 std::string m_option_name; // The name of the option which
\r
135 // caused the exception.
\r
138 /** Class thrown when value of option is incorrect. */
\r
139 class BOOST_PROGRAM_OPTIONS_DECL validation_error : public error {
\r
142 multiple_values_not_allowed = 30,
\r
143 at_least_one_value_required,
\r
144 invalid_bool_value,
\r
145 invalid_option_value,
\r
149 validation_error(kind_t kind,
\r
150 const std::string& option_value = "",
\r
151 const std::string& option_name = "");
\r
153 ~validation_error() throw() {}
\r
155 void set_option_name(const std::string& option);
\r
157 const std::string& get_option_name() const throw();
\r
159 const char* what() const throw();
\r
162 /** Used to convert kind_t to a related error text */
\r
163 static std::string error_message(kind_t kind);
\r
167 std::string m_option_name; // The name of the option which
\r
168 // caused the exception.
\r
169 std::string m_option_value; // Optional: value of the option m_options_name
\r
170 mutable std::string m_message; // For on-demand formatting in 'what'
\r
174 /** Class thrown if there is an invalid option value givenn */
\r
175 class BOOST_PROGRAM_OPTIONS_DECL invalid_option_value
\r
176 : public validation_error
\r
179 invalid_option_value(const std::string& value);
\r
180 #ifndef BOOST_NO_STD_WSTRING
\r
181 invalid_option_value(const std::wstring& value);
\r
185 /** Class thrown when there are too many positional options.
\r
186 This is a programming error.
\r
188 class BOOST_PROGRAM_OPTIONS_DECL too_many_positional_options_error : public error {
\r
190 too_many_positional_options_error()
\r
191 : error("too many positional options")
\r
195 /** Class thrown when there are syntax errors in given command line */
\r
196 class BOOST_PROGRAM_OPTIONS_DECL invalid_command_line_syntax : public invalid_syntax {
\r
198 invalid_command_line_syntax(const std::string& tokens, kind_t kind);
\r
201 /** Class thrown when there are programming error related to style */
\r
202 class BOOST_PROGRAM_OPTIONS_DECL invalid_command_line_style : public error {
\r
204 invalid_command_line_style(const std::string& msg)
\r
209 /** Class thrown if config file can not be read */
\r
210 class BOOST_PROGRAM_OPTIONS_DECL reading_file : public error {
\r
212 reading_file(const char* filename)
\r
213 : error(std::string("can not read file ").append(filename))
\r
217 /** Class thrown when a required/mandatory option is missing */
\r
218 class BOOST_PROGRAM_OPTIONS_DECL required_option : public error {
\r
220 required_option(const std::string& name)
\r
221 : error(std::string("missing required option ").append(name))
\r
222 , m_option_name(name)
\r
225 ~required_option() throw() {}
\r
227 const std::string& get_option_name() const throw();
\r
230 std::string m_option_name; // The name of the option which
\r
231 // caused the exception.
\r