/Src/Dependencies/Boost/boost/program_options/errors.hpp

http://hadesmem.googlecode.com/ · C++ Header · 243 lines · 156 code · 54 blank · 33 comment · 0 complexity · 0d5c5e98d2e23f934f6c7a4685761edc MD5 · raw file

  1. // Copyright Vladimir Prus 2002-2004.
  2. // Distributed under the Boost Software License, Version 1.0.
  3. // (See accompanying file LICENSE_1_0.txt
  4. // or copy at http://www.boost.org/LICENSE_1_0.txt)
  5. #ifndef BOOST_ERRORS_VP_2003_01_02
  6. #define BOOST_ERRORS_VP_2003_01_02
  7. #include <boost/program_options/config.hpp>
  8. #include <string>
  9. #include <stdexcept>
  10. #include <vector>
  11. #if defined(BOOST_MSVC)
  12. # pragma warning (push)
  13. # pragma warning (disable:4275) // non dll-interface class 'std::logic_error' used as base for dll-interface class 'boost::program_options::error'
  14. # pragma warning (disable:4251) // class 'std::vector<_Ty>' needs to have dll-interface to be used by clients of class 'boost::program_options::ambiguous_option'
  15. #endif
  16. namespace boost { namespace program_options {
  17. /** Base class for all errors in the library. */
  18. class BOOST_PROGRAM_OPTIONS_DECL error : public std::logic_error {
  19. public:
  20. error(const std::string& xwhat) : std::logic_error(xwhat) {}
  21. };
  22. class BOOST_PROGRAM_OPTIONS_DECL invalid_syntax : public error {
  23. public:
  24. enum kind_t {
  25. long_not_allowed = 30,
  26. long_adjacent_not_allowed,
  27. short_adjacent_not_allowed,
  28. empty_adjacent_parameter,
  29. missing_parameter,
  30. extra_parameter,
  31. unrecognized_line
  32. };
  33. invalid_syntax(const std::string& tokens, kind_t kind);
  34. // gcc says that throw specification on dtor is loosened
  35. // without this line
  36. ~invalid_syntax() throw() {}
  37. kind_t kind() const;
  38. const std::string& tokens() const;
  39. protected:
  40. /** Used to convert kind_t to a related error text */
  41. static std::string error_message(kind_t kind);
  42. private:
  43. // TODO: copy ctor might throw
  44. std::string m_tokens;
  45. kind_t m_kind;
  46. };
  47. /** Class thrown when option name is not recognized. */
  48. class BOOST_PROGRAM_OPTIONS_DECL unknown_option : public error {
  49. public:
  50. unknown_option(const std::string& name)
  51. : error(std::string("unknown option ").append(name)),
  52. m_option_name(name)
  53. {}
  54. // gcc says that throw specification on dtor is loosened
  55. // without this line
  56. ~unknown_option() throw() {}
  57. const std::string& get_option_name() const throw();
  58. private:
  59. std::string m_option_name;
  60. };
  61. /** Class thrown when there's ambiguity amoung several possible options. */
  62. class BOOST_PROGRAM_OPTIONS_DECL ambiguous_option : public error {
  63. public:
  64. ambiguous_option(const std::string& name,
  65. const std::vector<std::string>& xalternatives)
  66. : error(std::string("ambiguous option ").append(name))
  67. , m_alternatives(xalternatives)
  68. , m_option_name(name)
  69. {}
  70. ~ambiguous_option() throw() {}
  71. const std::string& get_option_name() const throw();
  72. const std::vector<std::string>& alternatives() const throw();
  73. private:
  74. // TODO: copy ctor might throw
  75. std::vector<std::string> m_alternatives;
  76. std::string m_option_name;
  77. };
  78. /** Class thrown when there are several option values, but
  79. user called a method which cannot return them all. */
  80. class BOOST_PROGRAM_OPTIONS_DECL multiple_values : public error {
  81. public:
  82. multiple_values()
  83. : error("multiple values")
  84. , m_option_name() {}
  85. ~multiple_values() throw() {}
  86. void set_option_name(const std::string& option);
  87. const std::string& get_option_name() const throw();
  88. private:
  89. std::string m_option_name; // The name of the option which
  90. // caused the exception.
  91. };
  92. /** Class thrown when there are several occurrences of an
  93. option, but user called a method which cannot return
  94. them all. */
  95. class BOOST_PROGRAM_OPTIONS_DECL multiple_occurrences : public error {
  96. public:
  97. multiple_occurrences()
  98. : error("multiple occurrences")
  99. , m_option_name() {}
  100. ~multiple_occurrences() throw() {}
  101. void set_option_name(const std::string& option);
  102. const std::string& get_option_name() const throw();
  103. private:
  104. std::string m_option_name; // The name of the option which
  105. // caused the exception.
  106. };
  107. /** Class thrown when value of option is incorrect. */
  108. class BOOST_PROGRAM_OPTIONS_DECL validation_error : public error {
  109. public:
  110. enum kind_t {
  111. multiple_values_not_allowed = 30,
  112. at_least_one_value_required,
  113. invalid_bool_value,
  114. invalid_option_value,
  115. invalid_option
  116. };
  117. validation_error(kind_t kind,
  118. const std::string& option_value = "",
  119. const std::string& option_name = "");
  120. ~validation_error() throw() {}
  121. void set_option_name(const std::string& option);
  122. const std::string& get_option_name() const throw();
  123. const char* what() const throw();
  124. protected:
  125. /** Used to convert kind_t to a related error text */
  126. static std::string error_message(kind_t kind);
  127. private:
  128. kind_t m_kind;
  129. std::string m_option_name; // The name of the option which
  130. // caused the exception.
  131. std::string m_option_value; // Optional: value of the option m_options_name
  132. mutable std::string m_message; // For on-demand formatting in 'what'
  133. };
  134. /** Class thrown if there is an invalid option value givenn */
  135. class BOOST_PROGRAM_OPTIONS_DECL invalid_option_value
  136. : public validation_error
  137. {
  138. public:
  139. invalid_option_value(const std::string& value);
  140. #ifndef BOOST_NO_STD_WSTRING
  141. invalid_option_value(const std::wstring& value);
  142. #endif
  143. };
  144. /** Class thrown when there are too many positional options.
  145. This is a programming error.
  146. */
  147. class BOOST_PROGRAM_OPTIONS_DECL too_many_positional_options_error : public error {
  148. public:
  149. too_many_positional_options_error()
  150. : error("too many positional options")
  151. {}
  152. };
  153. /** Class thrown when there are syntax errors in given command line */
  154. class BOOST_PROGRAM_OPTIONS_DECL invalid_command_line_syntax : public invalid_syntax {
  155. public:
  156. invalid_command_line_syntax(const std::string& tokens, kind_t kind);
  157. };
  158. /** Class thrown when there are programming error related to style */
  159. class BOOST_PROGRAM_OPTIONS_DECL invalid_command_line_style : public error {
  160. public:
  161. invalid_command_line_style(const std::string& msg)
  162. : error(msg)
  163. {}
  164. };
  165. /** Class thrown if config file can not be read */
  166. class BOOST_PROGRAM_OPTIONS_DECL reading_file : public error {
  167. public:
  168. reading_file(const char* filename)
  169. : error(std::string("can not read file ").append(filename))
  170. {}
  171. };
  172. /** Class thrown when a required/mandatory option is missing */
  173. class BOOST_PROGRAM_OPTIONS_DECL required_option : public error {
  174. public:
  175. required_option(const std::string& name)
  176. : error(std::string("missing required option ").append(name))
  177. , m_option_name(name)
  178. {}
  179. ~required_option() throw() {}
  180. const std::string& get_option_name() const throw();
  181. private:
  182. std::string m_option_name; // The name of the option which
  183. // caused the exception.
  184. };
  185. }}
  186. #if defined(BOOST_MSVC)
  187. # pragma warning (pop)
  188. #endif
  189. #endif