1 //
2 // Copyright (c) 2004-2017 Benjamin Kaufmann
3 //
4 // Permission is hereby granted, free of charge, to any person obtaining a copy
5 // of this software and associated documentation files (the "Software"), to
6 // deal in the Software without restriction, including without limitation the
7 // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
8 // sell copies of the Software, and to permit persons to whom the Software is
9 // furnished to do so, subject to the following conditions:
10 //
11 // The above copyright notice and this permission notice shall be included in
12 // all copies or substantial portions of the Software.
13 //
14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
20 // IN THE SOFTWARE.
21 //
22 // NOTE: ProgramOptions is inspired by Boost.Program_options
23 //       see: www.boost.org/libs/program_options
24 //
25 #ifndef PROGRAM_OPTIONS_ERRORS_H_INCLUDED
26 #define PROGRAM_OPTIONS_ERRORS_H_INCLUDED
27 #include <stdexcept>
28 #include <string>
29 namespace Potassco { namespace ProgramOptions {
30 
31 //! Base class for all exceptions.
32 class Error : public std::logic_error {
33 public:
Error(const std::string & what)34 	explicit Error(const std::string& what) : std::logic_error(what) {}
35 };
36 
37 //! Used for signaling errors on command-line and in declaring options.
38 class SyntaxError : public Error {
39 public:
40 	enum Type {
41 		missing_value,
42 		extra_value,
43 		invalid_format
44 	};
45 	SyntaxError(Type t, const std::string& key);
throw()46 	~SyntaxError() throw () {}
type()47 	Type               type() const { return type_; }
key()48 	const std::string& key()  const { return key_; }
49 private:
50 	std::string key_;
51 	Type        type_;
52 };
53 
54 //! Used for signaling errors in OptionContext.
55 class ContextError : public Error {
56 public:
57 	enum Type {
58 		duplicate_option,
59 		unknown_option,
60 		ambiguous_option,
61 		unknown_group
62 	};
63 	ContextError(const std::string& ctx, Type t, const std::string& key, const std::string& desc = "");
throw()64 	~ContextError() throw () {}
type()65 	Type               type() const { return type_; }
key()66 	const std::string& key()  const { return key_; }
ctx()67 	const std::string& ctx()  const { return ctx_; }
68 private:
69 	std::string ctx_;
70 	std::string key_;
71 	Type        type_;
72 };
73 
74 class DuplicateOption : public ContextError {
75 public:
DuplicateOption(const std::string & ctx,const std::string & key)76 	DuplicateOption(const std::string& ctx, const std::string& key) : ContextError(ctx, ContextError::duplicate_option, key) {}
throw()77 	~DuplicateOption() throw () {}
78 };
79 class UnknownOption : public ContextError {
80 public:
UnknownOption(const std::string & ctx,const std::string & key)81 	UnknownOption(const std::string& ctx, const std::string& key) : ContextError(ctx, ContextError::unknown_option, key) {}
throw()82 	~UnknownOption() throw () {}
83 };
84 class AmbiguousOption : public ContextError {
85 public:
AmbiguousOption(const std::string & ctx,const std::string & key,const std::string & alt)86 	AmbiguousOption(const std::string& ctx, const std::string& key, const std::string& alt) : ContextError(ctx, ContextError::ambiguous_option, key, alt) {}
throw()87 	~AmbiguousOption() throw () {}
88 };
89 
90 //! Used for signaling validation errors when trying to assign option values.
91 class ValueError : public Error {
92 public:
93 	enum Type {
94 		multiple_occurrences,
95 		invalid_default,
96 		invalid_value
97 	};
98 	ValueError(const std::string& ctx, Type t, const std::string& opt, const std::string& value);
throw()99 	~ValueError() throw () {}
type()100 	Type               type() const { return type_; }
key()101 	const std::string& key()  const { return key_; }
ctx()102 	const std::string& ctx()  const { return ctx_; }
value()103 	const std::string& value()const { return value_; }
104 private:
105 	std::string ctx_;
106 	std::string key_;
107 	std::string value_;
108 	Type        type_;
109 };
110 
111 
112 } // namespace ProgramOptions
113 } // namespace Potassco
114 #endif
115