| 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 | |
|---|
| 6 | |
|---|
| 7 | #ifndef BOOST_ERRORS_VP_2003_01_02 |
|---|
| 8 | #define BOOST_ERRORS_VP_2003_01_02 |
|---|
| 9 | |
|---|
| 10 | #include <boost/program_options/config.hpp> |
|---|
| 11 | |
|---|
| 12 | #include <string> |
|---|
| 13 | #include <stdexcept> |
|---|
| 14 | #include <vector> |
|---|
| 15 | |
|---|
| 16 | |
|---|
| 17 | |
|---|
| 18 | namespace boost { namespace program_options { |
|---|
| 19 | |
|---|
| 20 | /** Base class for all errors in the library. */ |
|---|
| 21 | class BOOST_PROGRAM_OPTIONS_DECL error : public std::logic_error { |
|---|
| 22 | public: |
|---|
| 23 | error(const std::string& what) : std::logic_error(what) {} |
|---|
| 24 | }; |
|---|
| 25 | |
|---|
| 26 | class BOOST_PROGRAM_OPTIONS_DECL invalid_syntax : public error { |
|---|
| 27 | public: |
|---|
| 28 | invalid_syntax(const std::string& tokens, const std::string& msg) |
|---|
| 29 | : error(std::string(msg).append(" in '").append(tokens).append("'")), |
|---|
| 30 | tokens(tokens), msg(msg) |
|---|
| 31 | {} |
|---|
| 32 | |
|---|
| 33 | // gcc says that throw specification on dtor is loosened |
|---|
| 34 | // without this line |
|---|
| 35 | ~invalid_syntax() throw() {} |
|---|
| 36 | |
|---|
| 37 | // TODO: copy ctor might throw |
|---|
| 38 | std::string tokens, msg; |
|---|
| 39 | }; |
|---|
| 40 | |
|---|
| 41 | /** Class thrown when option name is not recognized. */ |
|---|
| 42 | class BOOST_PROGRAM_OPTIONS_DECL unknown_option : public error { |
|---|
| 43 | public: |
|---|
| 44 | unknown_option(const std::string& name) |
|---|
| 45 | : error(std::string("unknown option ").append(name)) |
|---|
| 46 | {} |
|---|
| 47 | }; |
|---|
| 48 | |
|---|
| 49 | /** Class thrown when there's ambiguity amoung several possible options. */ |
|---|
| 50 | class BOOST_PROGRAM_OPTIONS_DECL ambiguous_option : public error { |
|---|
| 51 | public: |
|---|
| 52 | ambiguous_option(const std::string& name, |
|---|
| 53 | const std::vector<std::string>& alternatives) |
|---|
| 54 | : error(std::string("ambiguous option ").append(name)), |
|---|
| 55 | alternatives(alternatives) |
|---|
| 56 | {} |
|---|
| 57 | |
|---|
| 58 | ~ambiguous_option() throw() {} |
|---|
| 59 | |
|---|
| 60 | // TODO: copy ctor might throw |
|---|
| 61 | std::vector<std::string> alternatives; |
|---|
| 62 | }; |
|---|
| 63 | |
|---|
| 64 | /** Class thrown when there are several option values, but |
|---|
| 65 | user called a method which cannot return them all. */ |
|---|
| 66 | class BOOST_PROGRAM_OPTIONS_DECL multiple_values : public error { |
|---|
| 67 | public: |
|---|
| 68 | multiple_values(const std::string& what) : error(what) {} |
|---|
| 69 | }; |
|---|
| 70 | |
|---|
| 71 | /** Class thrown when there are several occurrences of an |
|---|
| 72 | option, but user called a method which cannot return |
|---|
| 73 | them all. */ |
|---|
| 74 | class BOOST_PROGRAM_OPTIONS_DECL multiple_occurrences : public error { |
|---|
| 75 | public: |
|---|
| 76 | multiple_occurrences(const std::string& what) : error(what) {} |
|---|
| 77 | }; |
|---|
| 78 | |
|---|
| 79 | /** Class thrown when value of option is incorrect. */ |
|---|
| 80 | class BOOST_PROGRAM_OPTIONS_DECL validation_error : public error { |
|---|
| 81 | public: |
|---|
| 82 | validation_error(const std::string& what) : error(what) {} |
|---|
| 83 | ~validation_error() throw() {} |
|---|
| 84 | void set_option_name(const std::string& option); |
|---|
| 85 | private: |
|---|
| 86 | mutable std::string m_message; // For on-demand formatting in 'what' |
|---|
| 87 | std::string m_option_name; // The name of the option which |
|---|
| 88 | // caused the exception. |
|---|
| 89 | const char* what() const throw(); |
|---|
| 90 | }; |
|---|
| 91 | |
|---|
| 92 | class BOOST_PROGRAM_OPTIONS_DECL invalid_option_value |
|---|
| 93 | : public validation_error |
|---|
| 94 | { |
|---|
| 95 | public: |
|---|
| 96 | invalid_option_value(const std::string& value); |
|---|
| 97 | #ifndef BOOST_NO_STD_WSTRING |
|---|
| 98 | invalid_option_value(const std::wstring& value); |
|---|
| 99 | #endif |
|---|
| 100 | }; |
|---|
| 101 | |
|---|
| 102 | /** Class thrown when there are too many positional options. */ |
|---|
| 103 | class BOOST_PROGRAM_OPTIONS_DECL too_many_positional_options_error : public error { |
|---|
| 104 | public: |
|---|
| 105 | too_many_positional_options_error(const std::string& what) |
|---|
| 106 | : error(what) {} |
|---|
| 107 | }; |
|---|
| 108 | |
|---|
| 109 | /** Class thrown when there are too few positional options. */ |
|---|
| 110 | class BOOST_PROGRAM_OPTIONS_DECL too_few_positional_options_error : public error { |
|---|
| 111 | public: |
|---|
| 112 | too_few_positional_options_error(const std::string& what) |
|---|
| 113 | : error(what) {} |
|---|
| 114 | }; |
|---|
| 115 | |
|---|
| 116 | class BOOST_PROGRAM_OPTIONS_DECL invalid_command_line_syntax : public invalid_syntax { |
|---|
| 117 | public: |
|---|
| 118 | enum kind_t { |
|---|
| 119 | long_not_allowed = 30, |
|---|
| 120 | long_adjacent_not_allowed, |
|---|
| 121 | short_adjacent_not_allowed, |
|---|
| 122 | empty_adjacent_parameter, |
|---|
| 123 | missing_parameter, |
|---|
| 124 | extra_parameter |
|---|
| 125 | }; |
|---|
| 126 | |
|---|
| 127 | invalid_command_line_syntax(const std::string& tokens, kind_t kind); |
|---|
| 128 | kind_t kind() const; |
|---|
| 129 | protected: |
|---|
| 130 | static std::string error_message(kind_t kind); |
|---|
| 131 | private: |
|---|
| 132 | kind_t m_kind; |
|---|
| 133 | }; |
|---|
| 134 | |
|---|
| 135 | class BOOST_PROGRAM_OPTIONS_DECL invalid_command_line_style : public error { |
|---|
| 136 | public: |
|---|
| 137 | invalid_command_line_style(const std::string& msg) |
|---|
| 138 | : error(msg) |
|---|
| 139 | {} |
|---|
| 140 | }; |
|---|
| 141 | |
|---|
| 142 | }} |
|---|
| 143 | |
|---|
| 144 | |
|---|
| 145 | #endif |
|---|