File indexing completed on 2025-01-30 09:58:05
0001
0002
0003
0004
0005
0006
0007 #ifndef BOOST_ERRORS_VP_2003_01_02
0008 #define BOOST_ERRORS_VP_2003_01_02
0009
0010 #include <boost/program_options/config.hpp>
0011
0012 #include <string>
0013 #include <stdexcept>
0014 #include <vector>
0015 #include <map>
0016
0017
0018 #if defined(BOOST_MSVC)
0019 # pragma warning (push)
0020 # pragma warning (disable:4275)
0021 # pragma warning (disable:4251)
0022 #endif
0023
0024 namespace boost { namespace program_options {
0025
0026 inline std::string strip_prefixes(const std::string& text)
0027 {
0028
0029 std::string::size_type i = text.find_first_not_of("-/");
0030 if (i == std::string::npos) {
0031 return text;
0032 } else {
0033 return text.substr(i);
0034 }
0035 }
0036
0037
0038 class BOOST_PROGRAM_OPTIONS_DECL BOOST_SYMBOL_VISIBLE error : public std::logic_error {
0039 public:
0040 error(const std::string& xwhat) : std::logic_error(xwhat) {}
0041 };
0042
0043
0044
0045
0046
0047 class BOOST_PROGRAM_OPTIONS_DECL BOOST_SYMBOL_VISIBLE too_many_positional_options_error : public error {
0048 public:
0049 too_many_positional_options_error()
0050 : error("too many positional options have been specified on the command line")
0051 {}
0052 };
0053
0054
0055 class BOOST_PROGRAM_OPTIONS_DECL BOOST_SYMBOL_VISIBLE invalid_command_line_style : public error {
0056 public:
0057 invalid_command_line_style(const std::string& msg)
0058 : error(msg)
0059 {}
0060 };
0061
0062
0063 class BOOST_PROGRAM_OPTIONS_DECL BOOST_SYMBOL_VISIBLE reading_file : public error {
0064 public:
0065 reading_file(const char* filename)
0066 : error(std::string("can not read options configuration file '").append(filename).append("'"))
0067 {}
0068 };
0069
0070
0071
0072
0073
0074
0075
0076
0077
0078
0079
0080
0081
0082
0083
0084
0085
0086
0087
0088
0089
0090
0091
0092
0093
0094 class BOOST_PROGRAM_OPTIONS_DECL BOOST_SYMBOL_VISIBLE error_with_option_name : public error {
0095
0096 protected:
0097
0098
0099
0100
0101
0102
0103 int m_option_style;
0104
0105
0106
0107
0108 std::map<std::string, std::string> m_substitutions;
0109 typedef std::pair<std::string, std::string> string_pair;
0110 std::map<std::string, string_pair > m_substitution_defaults;
0111
0112 public:
0113
0114 std::string m_error_template;
0115
0116 error_with_option_name(const std::string& template_,
0117 const std::string& option_name = "",
0118 const std::string& original_token = "",
0119 int option_style = 0);
0120
0121
0122
0123
0124 BOOST_DEFAULTED_FUNCTION(~error_with_option_name() throw(), {})
0125
0126
0127
0128
0129
0130
0131
0132
0133
0134
0135
0136
0137
0138
0139
0140
0141
0142
0143
0144
0145
0146
0147 void set_substitute(const std::string& parameter_name, const std::string& value)
0148 { m_substitutions[parameter_name] = value; }
0149
0150
0151
0152 void set_substitute_default(const std::string& parameter_name,
0153 const std::string& from,
0154 const std::string& to)
0155 {
0156 m_substitution_defaults[parameter_name] = std::make_pair(from, to);
0157 }
0158
0159
0160
0161 void add_context(const std::string& option_name,
0162 const std::string& original_token,
0163 int option_style)
0164 {
0165 set_option_name(option_name);
0166 set_original_token(original_token);
0167 set_prefix(option_style);
0168 }
0169
0170 void set_prefix(int option_style)
0171 { m_option_style = option_style;}
0172
0173
0174 virtual void set_option_name(const std::string& option_name)
0175 { set_substitute("option", option_name);}
0176
0177 std::string get_option_name() const
0178 { return get_canonical_option_name(); }
0179
0180 void set_original_token(const std::string& original_token)
0181 { set_substitute("original_token", original_token);}
0182
0183
0184
0185
0186 virtual const char* what() const throw();
0187
0188 protected:
0189
0190 mutable std::string m_message;
0191
0192
0193 virtual void substitute_placeholders(const std::string& error_template) const;
0194
0195
0196 void replace_token(const std::string& from, const std::string& to) const;
0197
0198
0199
0200 std::string get_canonical_option_name() const;
0201 std::string get_canonical_option_prefix() const;
0202 };
0203
0204
0205
0206
0207 class BOOST_PROGRAM_OPTIONS_DECL BOOST_SYMBOL_VISIBLE multiple_values : public error_with_option_name {
0208 public:
0209 multiple_values()
0210 : error_with_option_name("option '%canonical_option%' only takes a single argument"){}
0211
0212 BOOST_DEFAULTED_FUNCTION(~multiple_values() throw(), {})
0213 };
0214
0215
0216
0217
0218 class BOOST_PROGRAM_OPTIONS_DECL BOOST_SYMBOL_VISIBLE multiple_occurrences : public error_with_option_name {
0219 public:
0220 multiple_occurrences()
0221 : error_with_option_name("option '%canonical_option%' cannot be specified more than once"){}
0222
0223 BOOST_DEFAULTED_FUNCTION(~multiple_occurrences() throw(), {})
0224
0225 };
0226
0227
0228 class BOOST_PROGRAM_OPTIONS_DECL BOOST_SYMBOL_VISIBLE required_option : public error_with_option_name {
0229 public:
0230
0231 required_option(const std::string& option_name)
0232 : error_with_option_name("the option '%canonical_option%' is required but missing", "", option_name)
0233 {
0234 }
0235
0236 BOOST_DEFAULTED_FUNCTION(~required_option() throw(), {})
0237 };
0238
0239
0240
0241
0242
0243
0244
0245
0246
0247
0248
0249
0250 class BOOST_PROGRAM_OPTIONS_DECL BOOST_SYMBOL_VISIBLE error_with_no_option_name : public error_with_option_name {
0251 public:
0252 error_with_no_option_name(const std::string& template_,
0253 const std::string& original_token = "")
0254 : error_with_option_name(template_, "", original_token)
0255 {
0256 }
0257
0258
0259 virtual void set_option_name(const std::string&) {}
0260
0261 BOOST_DEFAULTED_FUNCTION(~error_with_no_option_name() throw(), {})
0262 };
0263
0264
0265
0266 class BOOST_PROGRAM_OPTIONS_DECL BOOST_SYMBOL_VISIBLE unknown_option : public error_with_no_option_name {
0267 public:
0268 unknown_option(const std::string& original_token = "")
0269 : error_with_no_option_name("unrecognised option '%canonical_option%'", original_token)
0270 {
0271 }
0272
0273 BOOST_DEFAULTED_FUNCTION(~unknown_option() throw(), {})
0274 };
0275
0276
0277
0278
0279 class BOOST_PROGRAM_OPTIONS_DECL BOOST_SYMBOL_VISIBLE ambiguous_option : public error_with_no_option_name {
0280 public:
0281 ambiguous_option(const std::vector<std::string>& xalternatives)
0282 : error_with_no_option_name("option '%canonical_option%' is ambiguous"),
0283 m_alternatives(xalternatives)
0284 {}
0285
0286 BOOST_DEFAULTED_FUNCTION(~ambiguous_option() throw(), {})
0287
0288 const std::vector<std::string>& alternatives() const throw() {return m_alternatives;}
0289
0290 protected:
0291
0292 virtual void substitute_placeholders(const std::string& error_template) const;
0293 private:
0294
0295 std::vector<std::string> m_alternatives;
0296 };
0297
0298
0299
0300
0301
0302 class BOOST_PROGRAM_OPTIONS_DECL BOOST_SYMBOL_VISIBLE invalid_syntax : public error_with_option_name {
0303 public:
0304 enum kind_t {
0305 long_not_allowed = 30,
0306 long_adjacent_not_allowed,
0307 short_adjacent_not_allowed,
0308 empty_adjacent_parameter,
0309 missing_parameter,
0310 extra_parameter,
0311 unrecognized_line
0312 };
0313
0314 invalid_syntax(kind_t kind,
0315 const std::string& option_name = "",
0316 const std::string& original_token = "",
0317 int option_style = 0):
0318 error_with_option_name(get_template(kind), option_name, original_token, option_style),
0319 m_kind(kind)
0320 {
0321 }
0322
0323 BOOST_DEFAULTED_FUNCTION(~invalid_syntax() throw(), {})
0324
0325 kind_t kind() const {return m_kind;}
0326
0327
0328 virtual std::string tokens() const {return get_option_name(); }
0329 protected:
0330
0331 std::string get_template(kind_t kind);
0332 kind_t m_kind;
0333 };
0334
0335 class BOOST_PROGRAM_OPTIONS_DECL BOOST_SYMBOL_VISIBLE invalid_config_file_syntax : public invalid_syntax {
0336 public:
0337 invalid_config_file_syntax(const std::string& invalid_line, kind_t kind):
0338 invalid_syntax(kind)
0339 {
0340 m_substitutions["invalid_line"] = invalid_line;
0341 }
0342
0343 BOOST_DEFAULTED_FUNCTION(~invalid_config_file_syntax() throw(), {})
0344
0345
0346 virtual std::string tokens() const {return m_substitutions.find("invalid_line")->second; }
0347 };
0348
0349
0350
0351 class BOOST_PROGRAM_OPTIONS_DECL BOOST_SYMBOL_VISIBLE invalid_command_line_syntax : public invalid_syntax {
0352 public:
0353 invalid_command_line_syntax(kind_t kind,
0354 const std::string& option_name = "",
0355 const std::string& original_token = "",
0356 int option_style = 0):
0357 invalid_syntax(kind, option_name, original_token, option_style) {}
0358 BOOST_DEFAULTED_FUNCTION(~invalid_command_line_syntax() throw(), {})
0359 };
0360
0361
0362
0363 class BOOST_PROGRAM_OPTIONS_DECL BOOST_SYMBOL_VISIBLE validation_error : public error_with_option_name {
0364 public:
0365 enum kind_t {
0366 multiple_values_not_allowed = 30,
0367 at_least_one_value_required,
0368 invalid_bool_value,
0369 invalid_option_value,
0370 invalid_option
0371 };
0372
0373 public:
0374 validation_error(kind_t kind,
0375 const std::string& option_name = "",
0376 const std::string& original_token = "",
0377 int option_style = 0):
0378 error_with_option_name(get_template(kind), option_name, original_token, option_style),
0379 m_kind(kind)
0380 {
0381 }
0382
0383 BOOST_DEFAULTED_FUNCTION(~validation_error() throw(), {})
0384
0385 kind_t kind() const { return m_kind; }
0386
0387 protected:
0388
0389 std::string get_template(kind_t kind);
0390 kind_t m_kind;
0391 };
0392
0393
0394 class BOOST_PROGRAM_OPTIONS_DECL BOOST_SYMBOL_VISIBLE invalid_option_value
0395 : public validation_error
0396 {
0397 public:
0398 invalid_option_value(const std::string& value);
0399 #ifndef BOOST_NO_STD_WSTRING
0400 invalid_option_value(const std::wstring& value);
0401 #endif
0402 };
0403
0404
0405 class BOOST_PROGRAM_OPTIONS_DECL BOOST_SYMBOL_VISIBLE invalid_bool_value
0406 : public validation_error
0407 {
0408 public:
0409 invalid_bool_value(const std::string& value);
0410 };
0411
0412
0413
0414
0415
0416
0417
0418 }}
0419
0420 #if defined(BOOST_MSVC)
0421 # pragma warning (pop)
0422 #endif
0423
0424 #endif