Chris@16: // Copyright Vladimir Prus 2002-2004. Chris@16: // Distributed under the Boost Software License, Version 1.0. Chris@16: // (See accompanying file LICENSE_1_0.txt Chris@16: // or copy at http://www.boost.org/LICENSE_1_0.txt) Chris@16: Chris@16: Chris@16: #ifndef BOOST_ERRORS_VP_2003_01_02 Chris@16: #define BOOST_ERRORS_VP_2003_01_02 Chris@16: Chris@16: #include Chris@16: Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: Chris@16: Chris@16: #if defined(BOOST_MSVC) Chris@16: # pragma warning (push) Chris@16: # pragma warning (disable:4275) // non dll-interface class 'std::logic_error' used as base for dll-interface class 'boost::program_options::error' Chris@16: # 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' Chris@16: #endif Chris@16: Chris@16: namespace boost { namespace program_options { Chris@16: Chris@16: inline std::string strip_prefixes(const std::string& text) Chris@16: { Chris@101: // "--foo-bar" -> "foo-bar" Chris@101: return text.substr(text.find_first_not_of("-/")); Chris@16: } Chris@16: Chris@16: /** Base class for all errors in the library. */ Chris@16: class BOOST_PROGRAM_OPTIONS_DECL error : public std::logic_error { Chris@16: public: Chris@16: error(const std::string& xwhat) : std::logic_error(xwhat) {} Chris@16: }; Chris@16: Chris@16: Chris@16: /** Class thrown when there are too many positional options. Chris@16: This is a programming error. Chris@16: */ Chris@16: class BOOST_PROGRAM_OPTIONS_DECL too_many_positional_options_error : public error { Chris@16: public: Chris@16: too_many_positional_options_error() Chris@16: : error("too many positional options have been specified on the command line") Chris@16: {} Chris@16: }; Chris@16: Chris@16: /** Class thrown when there are programming error related to style */ Chris@16: class BOOST_PROGRAM_OPTIONS_DECL invalid_command_line_style : public error { Chris@16: public: Chris@16: invalid_command_line_style(const std::string& msg) Chris@16: : error(msg) Chris@16: {} Chris@16: }; Chris@16: Chris@16: /** Class thrown if config file can not be read */ Chris@16: class BOOST_PROGRAM_OPTIONS_DECL reading_file : public error { Chris@16: public: Chris@16: reading_file(const char* filename) Chris@16: : error(std::string("can not read options configuration file '").append(filename).append("'")) Chris@16: {} Chris@16: }; Chris@16: Chris@16: Chris@16: /** Base class for most exceptions in the library. Chris@16: * Chris@16: * Substitutes the values for the parameter name Chris@16: * placeholders in the template to create the human Chris@16: * readable error message Chris@16: * Chris@16: * Placeholders are surrounded by % signs: %example% Chris@16: * Poor man's version of boost::format Chris@16: * Chris@16: * If a parameter name is absent, perform default substitutions Chris@16: * instead so ugly placeholders are never left in-place. Chris@16: * Chris@16: * Options are displayed in "canonical" form Chris@16: * This is the most unambiguous form of the Chris@16: * *parsed* option name and would correspond to Chris@16: * option_description::format_name() Chris@16: * i.e. what is shown by print_usage() Chris@16: * Chris@16: * The "canonical" form depends on whether the option is Chris@16: * specified in short or long form, using dashes or slashes Chris@16: * or without a prefix (from a configuration file) Chris@16: * Chris@16: * */ Chris@16: class BOOST_PROGRAM_OPTIONS_DECL error_with_option_name : public error { Chris@16: Chris@16: protected: Chris@16: /** can be Chris@16: * 0 = no prefix (config file options) Chris@16: * allow_long Chris@16: * allow_dash_for_short Chris@16: * allow_slash_for_short Chris@16: * allow_long_disguise */ Chris@16: int m_option_style; Chris@16: Chris@16: Chris@16: /** substitutions Chris@16: * from placeholders to values */ Chris@16: std::map m_substitutions; Chris@16: typedef std::pair string_pair; Chris@16: std::map m_substitution_defaults; Chris@16: Chris@16: public: Chris@101: /** template with placeholders */ Chris@101: std::string m_error_template; Chris@16: Chris@101: error_with_option_name(const std::string& template_, Chris@101: const std::string& option_name = "", Chris@101: const std::string& original_token = "", Chris@101: int option_style = 0); Chris@16: Chris@16: /** gcc says that throw specification on dtor is loosened Chris@16: * without this line Chris@16: * */ Chris@16: ~error_with_option_name() throw() {} Chris@16: Chris@16: Chris@16: //void dump() const Chris@16: //{ Chris@16: // std::cerr << "m_substitution_defaults:\n"; Chris@16: // for (std::map::const_iterator iter = m_substitution_defaults.begin(); Chris@16: // iter != m_substitution_defaults.end(); ++iter) Chris@16: // std::cerr << "\t" << iter->first << ":" << iter->second.first << "=" << iter->second.second << "\n"; Chris@16: // std::cerr << "m_substitutions:\n"; Chris@16: // for (std::map::const_iterator iter = m_substitutions.begin(); Chris@16: // iter != m_substitutions.end(); ++iter) Chris@16: // std::cerr << "\t" << iter->first << "=" << iter->second << "\n"; Chris@16: // std::cerr << "m_error_template:\n"; Chris@16: // std::cerr << "\t" << m_error_template << "\n"; Chris@16: // std::cerr << "canonical_option_prefix:[" << get_canonical_option_prefix() << "]\n"; Chris@16: // std::cerr << "canonical_option_name:[" << get_canonical_option_name() <<"]\n"; Chris@16: // std::cerr << "what:[" << what() << "]\n"; Chris@16: //} Chris@16: Chris@16: /** Substitute Chris@16: * parameter_name->value to create the error message from Chris@16: * the error template */ Chris@16: void set_substitute(const std::string& parameter_name, const std::string& value) Chris@16: { m_substitutions[parameter_name] = value; } Chris@16: Chris@16: /** If the parameter is missing, then make the Chris@16: * from->to substitution instead */ Chris@16: void set_substitute_default(const std::string& parameter_name, Chris@16: const std::string& from, Chris@16: const std::string& to) Chris@16: { Chris@16: m_substitution_defaults[parameter_name] = std::make_pair(from, to); Chris@16: } Chris@16: Chris@16: Chris@16: /** Add context to an exception */ Chris@16: void add_context(const std::string& option_name, Chris@16: const std::string& original_token, Chris@16: int option_style) Chris@16: { Chris@16: set_option_name(option_name); Chris@16: set_original_token(original_token); Chris@16: set_prefix(option_style); Chris@16: } Chris@16: Chris@16: void set_prefix(int option_style) Chris@16: { m_option_style = option_style;} Chris@16: Chris@16: /** Overridden in error_with_no_option_name */ Chris@16: virtual void set_option_name(const std::string& option_name) Chris@16: { set_substitute("option", option_name);} Chris@16: Chris@16: std::string get_option_name() const throw() Chris@16: { return get_canonical_option_name(); } Chris@16: Chris@16: void set_original_token(const std::string& original_token) Chris@16: { set_substitute("original_token", original_token);} Chris@16: Chris@16: Chris@16: /** Creates the error_message on the fly Chris@16: * Currently a thin wrapper for substitute_placeholders() */ Chris@16: virtual const char* what() const throw(); Chris@16: Chris@16: protected: Chris@16: /** Used to hold the error text returned by what() */ Chris@16: mutable std::string m_message; // For on-demand formatting in 'what' Chris@16: Chris@16: /** Makes all substitutions using the template */ Chris@16: virtual void substitute_placeholders(const std::string& error_template) const; Chris@16: Chris@16: // helper function for substitute_placeholders Chris@16: void replace_token(const std::string& from, const std::string& to) const; Chris@16: Chris@16: /** Construct option name in accordance with the appropriate Chris@16: * prefix style: i.e. long dash or short slash etc */ Chris@16: std::string get_canonical_option_name() const; Chris@16: std::string get_canonical_option_prefix() const; Chris@16: }; Chris@16: Chris@16: Chris@16: /** Class thrown when there are several option values, but Chris@16: user called a method which cannot return them all. */ Chris@16: class BOOST_PROGRAM_OPTIONS_DECL multiple_values : public error_with_option_name { Chris@16: public: Chris@16: multiple_values() Chris@16: : error_with_option_name("option '%canonical_option%' only takes a single argument"){} Chris@16: Chris@16: ~multiple_values() throw() {} Chris@16: }; Chris@16: Chris@16: /** Class thrown when there are several occurrences of an Chris@16: option, but user called a method which cannot return Chris@16: them all. */ Chris@16: class BOOST_PROGRAM_OPTIONS_DECL multiple_occurrences : public error_with_option_name { Chris@16: public: Chris@16: multiple_occurrences() Chris@16: : error_with_option_name("option '%canonical_option%' cannot be specified more than once"){} Chris@16: Chris@16: ~multiple_occurrences() throw() {} Chris@16: Chris@16: }; Chris@16: Chris@16: /** Class thrown when a required/mandatory option is missing */ Chris@16: class BOOST_PROGRAM_OPTIONS_DECL required_option : public error_with_option_name { Chris@16: public: Chris@16: // option name is constructed by the option_descriptor and never on the fly Chris@16: required_option(const std::string& option_name) Chris@16: : error_with_option_name("the option '%canonical_option%' is required but missing", "", option_name) Chris@16: { Chris@16: } Chris@16: Chris@16: ~required_option() throw() {} Chris@16: }; Chris@16: Chris@16: /** Base class of unparsable options, Chris@16: * when the desired option cannot be identified. Chris@16: * Chris@16: * Chris@16: * It makes no sense to have an option name, when we can't match an option to the Chris@16: * parameter Chris@16: * Chris@16: * Having this a part of the error_with_option_name hierachy makes error handling Chris@16: * a lot easier, even if the name indicates some sort of conceptual dissonance! Chris@16: * Chris@16: * */ Chris@16: class BOOST_PROGRAM_OPTIONS_DECL error_with_no_option_name : public error_with_option_name { Chris@16: public: Chris@16: error_with_no_option_name(const std::string& template_, Chris@16: const std::string& original_token = "") Chris@16: : error_with_option_name(template_, "", original_token) Chris@16: { Chris@16: } Chris@16: Chris@16: /** Does NOT set option name, because no option name makes sense */ Chris@16: virtual void set_option_name(const std::string&) {} Chris@16: Chris@16: ~error_with_no_option_name() throw() {} Chris@16: }; Chris@16: Chris@16: Chris@16: /** Class thrown when option name is not recognized. */ Chris@16: class BOOST_PROGRAM_OPTIONS_DECL unknown_option : public error_with_no_option_name { Chris@16: public: Chris@16: unknown_option(const std::string& original_token = "") Chris@16: : error_with_no_option_name("unrecognised option '%canonical_option%'", original_token) Chris@16: { Chris@16: } Chris@16: Chris@16: ~unknown_option() throw() {} Chris@16: }; Chris@16: Chris@16: Chris@16: Chris@16: /** Class thrown when there's ambiguity amoung several possible options. */ Chris@16: class BOOST_PROGRAM_OPTIONS_DECL ambiguous_option : public error_with_no_option_name { Chris@16: public: Chris@16: ambiguous_option(const std::vector& xalternatives) Chris@16: : error_with_no_option_name("option '%canonical_option%' is ambiguous"), Chris@16: m_alternatives(xalternatives) Chris@16: {} Chris@16: Chris@16: ~ambiguous_option() throw() {} Chris@16: Chris@16: const std::vector& alternatives() const throw() {return m_alternatives;} Chris@16: Chris@16: protected: Chris@16: /** Makes all substitutions using the template */ Chris@16: virtual void substitute_placeholders(const std::string& error_template) const; Chris@16: private: Chris@16: // TODO: copy ctor might throw Chris@16: std::vector m_alternatives; Chris@16: }; Chris@16: Chris@16: Chris@16: /** Class thrown when there's syntax error either for command Chris@16: * line or config file options. See derived children for Chris@16: * concrete classes. */ Chris@16: class BOOST_PROGRAM_OPTIONS_DECL invalid_syntax : public error_with_option_name { Chris@16: public: Chris@16: enum kind_t { Chris@16: long_not_allowed = 30, Chris@16: long_adjacent_not_allowed, Chris@16: short_adjacent_not_allowed, Chris@16: empty_adjacent_parameter, Chris@16: missing_parameter, Chris@16: extra_parameter, Chris@16: unrecognized_line Chris@16: }; Chris@16: Chris@16: invalid_syntax(kind_t kind, Chris@16: const std::string& option_name = "", Chris@16: const std::string& original_token = "", Chris@16: int option_style = 0): Chris@16: error_with_option_name(get_template(kind), option_name, original_token, option_style), Chris@16: m_kind(kind) Chris@16: { Chris@16: } Chris@16: Chris@16: ~invalid_syntax() throw() {} Chris@16: Chris@16: kind_t kind() const {return m_kind;} Chris@16: Chris@16: /** Convenience functions for backwards compatibility */ Chris@16: virtual std::string tokens() const {return get_option_name(); } Chris@16: protected: Chris@16: /** Used to convert kind_t to a related error text */ Chris@16: std::string get_template(kind_t kind); Chris@16: kind_t m_kind; Chris@16: }; Chris@16: Chris@16: class BOOST_PROGRAM_OPTIONS_DECL invalid_config_file_syntax : public invalid_syntax { Chris@16: public: Chris@16: invalid_config_file_syntax(const std::string& invalid_line, kind_t kind): Chris@16: invalid_syntax(kind) Chris@16: { Chris@16: m_substitutions["invalid_line"] = invalid_line; Chris@16: } Chris@16: Chris@16: ~invalid_config_file_syntax() throw() {} Chris@16: Chris@16: /** Convenience functions for backwards compatibility */ Chris@16: virtual std::string tokens() const {return m_substitutions.find("invalid_line")->second; } Chris@16: }; Chris@16: Chris@16: Chris@16: /** Class thrown when there are syntax errors in given command line */ Chris@16: class BOOST_PROGRAM_OPTIONS_DECL invalid_command_line_syntax : public invalid_syntax { Chris@16: public: Chris@16: invalid_command_line_syntax(kind_t kind, Chris@16: const std::string& option_name = "", Chris@16: const std::string& original_token = "", Chris@16: int option_style = 0): Chris@16: invalid_syntax(kind, option_name, original_token, option_style) {} Chris@16: ~invalid_command_line_syntax() throw() {} Chris@16: }; Chris@16: Chris@16: Chris@16: /** Class thrown when value of option is incorrect. */ Chris@16: class BOOST_PROGRAM_OPTIONS_DECL validation_error : public error_with_option_name { Chris@16: public: Chris@16: enum kind_t { Chris@16: multiple_values_not_allowed = 30, Chris@16: at_least_one_value_required, Chris@16: invalid_bool_value, Chris@16: invalid_option_value, Chris@16: invalid_option Chris@16: }; Chris@16: Chris@16: public: Chris@16: validation_error(kind_t kind, Chris@16: const std::string& option_name = "", Chris@16: const std::string& original_token = "", Chris@16: int option_style = 0): Chris@16: error_with_option_name(get_template(kind), option_name, original_token, option_style) Chris@16: { Chris@16: } Chris@16: Chris@16: ~validation_error() throw() {} Chris@16: Chris@16: protected: Chris@16: /** Used to convert kind_t to a related error text */ Chris@16: std::string get_template(kind_t kind); Chris@16: kind_t m_kind; Chris@16: }; Chris@16: Chris@16: /** Class thrown if there is an invalid option value given */ Chris@16: class BOOST_PROGRAM_OPTIONS_DECL invalid_option_value Chris@16: : public validation_error Chris@16: { Chris@16: public: Chris@16: invalid_option_value(const std::string& value); Chris@16: #ifndef BOOST_NO_STD_WSTRING Chris@16: invalid_option_value(const std::wstring& value); Chris@16: #endif Chris@16: }; Chris@16: Chris@16: /** Class thrown if there is an invalid bool value given */ Chris@16: class BOOST_PROGRAM_OPTIONS_DECL invalid_bool_value Chris@16: : public validation_error Chris@16: { Chris@16: public: Chris@16: invalid_bool_value(const std::string& value); Chris@16: }; Chris@16: Chris@16: Chris@16: Chris@16: Chris@16: Chris@16: Chris@16: Chris@16: }} Chris@16: Chris@16: #if defined(BOOST_MSVC) Chris@16: # pragma warning (pop) Chris@16: #endif Chris@16: Chris@16: #endif