summaryrefslogtreecommitdiffstats
blob: ff6bc7f0e844dd0451c7dac5ba54459d0944b198 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
// Copyright Vladimir Prus 2002-2004.
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt
// or copy at http://www.boost.org/LICENSE_1_0.txt)


#ifndef BOOST_ERRORS_VP_2003_01_02
#define BOOST_ERRORS_VP_2003_01_02

#include <boost/program_options/config.hpp>

#include <string>
#include <stdexcept>
#include <vector>

#if defined(BOOST_MSVC)
#   pragma warning (push)
#   pragma warning (disable:4275) // non dll-interface class 'std::logic_error' used as base for dll-interface class 'boost::program_options::error'
#   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'
#endif

namespace boost { namespace program_options {

    /** Base class for all errors in the library. */
    class BOOST_PROGRAM_OPTIONS_DECL error : public std::logic_error {
    public:
        error(const std::string& xwhat) : std::logic_error(xwhat) {}
    };

    class BOOST_PROGRAM_OPTIONS_DECL invalid_syntax : public error {
    public:
        enum kind_t {
            long_not_allowed = 30,
            long_adjacent_not_allowed,
            short_adjacent_not_allowed,
            empty_adjacent_parameter,
            missing_parameter,
            extra_parameter,
            unrecognized_line
        };
        
        invalid_syntax(const std::string& tokens, kind_t kind);

        // gcc says that throw specification on dtor is loosened
        // without this line
        ~invalid_syntax() throw() {}
        
        kind_t kind() const;
        
        const std::string& tokens() const;
        
    protected:
        /** Used to convert kind_t to a related error text */
        static std::string error_message(kind_t kind);

    private:
        // TODO: copy ctor might throw
        std::string m_tokens;

        kind_t m_kind;
    };

    /** Class thrown when option name is not recognized. */
    class BOOST_PROGRAM_OPTIONS_DECL unknown_option : public error {
    public:
        unknown_option(const std::string& name)
        : error(std::string("unknown option ").append(name)), 
          m_option_name(name)
        {}

        // gcc says that throw specification on dtor is loosened
        // without this line
        ~unknown_option() throw() {}
        
        const std::string& get_option_name() const throw();
        
    private:
        std::string m_option_name;
    };

    /** Class thrown when there's ambiguity amoung several possible options. */
    class BOOST_PROGRAM_OPTIONS_DECL ambiguous_option : public error {
    public:
        ambiguous_option(const std::string& name, 
                         const std::vector<std::string>& xalternatives)
        : error(std::string("ambiguous option ").append(name))
        , m_alternatives(xalternatives)
        , m_option_name(name)
        {}

        ~ambiguous_option() throw() {}
        
        const std::string& get_option_name() const throw();
        
        const std::vector<std::string>& alternatives() const throw();

    private:
        // TODO: copy ctor might throw
        std::vector<std::string> m_alternatives;
        std::string m_option_name;
    };

    /** Class thrown when there are several option values, but
        user called a method which cannot return them all. */
    class BOOST_PROGRAM_OPTIONS_DECL multiple_values : public error {
    public:
        multiple_values() 
         : error("multiple values")
         , m_option_name() {}
         
        ~multiple_values() throw() {}
        
        void set_option_name(const std::string& option);
        
        const std::string& get_option_name() const throw();
        
    private:
        std::string m_option_name; // The name of the option which
                                   // caused the exception.        
    };

    /** Class thrown when there are several occurrences of an
        option, but user called a method which cannot return 
        them all. */
    class BOOST_PROGRAM_OPTIONS_DECL multiple_occurrences : public error {
    public:
        multiple_occurrences() 
         : error("multiple occurrences")
         , m_option_name() {}
         
        ~multiple_occurrences() throw() {}
        
        void set_option_name(const std::string& option);
        
        const std::string& get_option_name() const throw();

    private:        
        std::string m_option_name; // The name of the option which
                                   // caused the exception.
    };

    /** Class thrown when value of option is incorrect. */
    class BOOST_PROGRAM_OPTIONS_DECL validation_error : public error {
    public:
        enum kind_t {
            multiple_values_not_allowed = 30,
            at_least_one_value_required, 
            invalid_bool_value,
            invalid_option_value,
            invalid_option
        };
        
        validation_error(kind_t kind, 
                         const std::string& option_value = "",
                         const std::string& option_name = "");
                         
        ~validation_error() throw() {}

        void set_option_name(const std::string& option);
        
        const std::string& get_option_name() const throw();
        
        const char* what() const throw();
        
    protected:
        /** Used to convert kind_t to a related error text */
        static std::string error_message(kind_t kind);

    private:
        kind_t m_kind;
        std::string m_option_name; // The name of the option which
                                   // caused the exception.
        std::string m_option_value; // Optional: value of the option m_options_name
        mutable std::string m_message; // For on-demand formatting in 'what'

    };

    /** Class thrown if there is an invalid option value givenn */
    class BOOST_PROGRAM_OPTIONS_DECL invalid_option_value 
        : public validation_error
    {
    public:
        invalid_option_value(const std::string& value);
#ifndef BOOST_NO_STD_WSTRING
        invalid_option_value(const std::wstring& value);
#endif
    };

    /** Class thrown when there are too many positional options. 
        This is a programming error.
    */
    class BOOST_PROGRAM_OPTIONS_DECL too_many_positional_options_error : public error {
    public:
        too_many_positional_options_error() 
         : error("too many positional options") 
        {}
    };

    /** Class thrown when there are syntax errors in given command line */
    class BOOST_PROGRAM_OPTIONS_DECL invalid_command_line_syntax : public invalid_syntax {
    public:
        invalid_command_line_syntax(const std::string& tokens, kind_t kind);
    };

    /** Class thrown when there are programming error related to style */
    class BOOST_PROGRAM_OPTIONS_DECL invalid_command_line_style : public error {
    public:
        invalid_command_line_style(const std::string& msg)
        : error(msg)
        {}
    };

    /** Class thrown if config file can not be read */
    class BOOST_PROGRAM_OPTIONS_DECL reading_file : public error {
    public:
        reading_file(const char* filename)
         : error(std::string("can not read file ").append(filename))
        {}
    };
    
     /** Class thrown when a required/mandatory option is missing */
     class BOOST_PROGRAM_OPTIONS_DECL required_option : public error {
     public:
        required_option(const std::string& name)
        : error(std::string("missing required option ").append(name))
        , m_option_name(name)
        {}
 
        ~required_option() throw() {}

        const std::string& get_option_name() const throw();
        
     private:
        std::string m_option_name; // The name of the option which
                                   // caused the exception.
     };
}}

#if defined(BOOST_MSVC)
#   pragma warning (pop)
#endif

#endif