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
|
//
// Copyright (c) Benjamin Kaufmann 2004
//
// This is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This file is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//
//
// NOTE: ProgramOptions is inspired by Boost.Program_options
// see: www.boost.org/libs/program_options
//
#ifndef PROGRAM_OPTIONS_ERRORS_H_INCLUDED
#define PROGRAM_OPTIONS_ERRORS_H_INCLUDED
#include <stdexcept>
#include <string>
namespace ProgramOptions {
//! Base class for all exceptions.
class Error : public std::logic_error {
public:
explicit Error(const std::string& what) : std::logic_error(what) {}
};
//! Used for signaling errors on command-line and in declaring options.
class SyntaxError : public Error {
public:
enum Type {
missing_value,
extra_value,
invalid_format
};
SyntaxError(Type t, const std::string& key);
~SyntaxError() throw () {}
Type type() const { return type_; }
const std::string& key() const { return key_; }
private:
std::string key_;
Type type_;
};
//! Used for signaling errors in OptionContext.
class ContextError : public Error {
public:
enum Type {
duplicate_option,
unknown_option,
ambiguous_option,
unknown_group,
};
ContextError(const std::string& ctx, Type t, const std::string& key, const std::string& desc = "");
~ContextError() throw () {}
Type type() const { return type_; }
const std::string& key() const { return key_; }
const std::string& ctx() const { return ctx_; }
private:
std::string ctx_;
std::string key_;
Type type_;
};
class DuplicateOption : public ContextError {
public:
DuplicateOption(const std::string& ctx, const std::string& key) : ContextError(ctx, ContextError::duplicate_option, key) {}
~DuplicateOption() throw () {}
};
class UnknownOption : public ContextError {
public:
UnknownOption(const std::string& ctx, const std::string& key) : ContextError(ctx, ContextError::unknown_option, key) {}
~UnknownOption() throw () {}
};
class AmbiguousOption : public ContextError {
public:
AmbiguousOption(const std::string& ctx, const std::string& key, const std::string& alt) : ContextError(ctx, ContextError::ambiguous_option, key, alt) {}
~AmbiguousOption() throw () {}
};
//! Used for signaling validation errors when trying to assign option values.
class ValueError : public Error {
public:
enum Type {
multiple_occurences,
invalid_default,
invalid_value
};
ValueError(const std::string& ctx, Type t, const std::string& opt, const std::string& value);
~ValueError() throw () {}
Type type() const { return type_; }
const std::string& key() const { return key_; }
const std::string& ctx() const { return ctx_; }
const std::string& value()const { return value_;}
private:
std::string ctx_;
std::string key_;
std::string value_;
Type type_;
};
}
#endif
|