Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-30 09:58:05

0001 // Copyright Vladimir Prus 2002-2004.
0002 // Distributed under the Boost Software License, Version 1.0.
0003 // (See accompanying file LICENSE_1_0.txt
0004 // or copy at http://www.boost.org/LICENSE_1_0.txt)
0005 
0006 
0007 #ifndef BOOST_ERRORS_VP_2003_01_02
0008 #define BOOST_ERRORS_VP_2003_01_02
0009 
0010 #include <boost/program_options/config.hpp>
0011 
0012 #include <string>
0013 #include <stdexcept>
0014 #include <vector>
0015 #include <map>
0016 
0017 
0018 #if defined(BOOST_MSVC)
0019 #   pragma warning (push)
0020 #   pragma warning (disable:4275) // non dll-interface class 'std::logic_error' used as base for dll-interface class 'boost::program_options::error'
0021 #   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'
0022 #endif
0023 
0024 namespace boost { namespace program_options {
0025 
0026     inline std::string strip_prefixes(const std::string& text)
0027     {
0028         // "--foo-bar" -> "foo-bar"
0029         std::string::size_type i = text.find_first_not_of("-/");
0030         if (i == std::string::npos) {
0031             return text;
0032         } else {
0033             return text.substr(i);
0034         }
0035     }
0036 
0037     /** Base class for all errors in the library. */
0038     class BOOST_PROGRAM_OPTIONS_DECL BOOST_SYMBOL_VISIBLE error : public std::logic_error {
0039     public:
0040         error(const std::string& xwhat) : std::logic_error(xwhat) {}
0041     };
0042 
0043 
0044     /** Class thrown when there are too many positional options. 
0045         This is a programming error.
0046     */
0047     class BOOST_PROGRAM_OPTIONS_DECL BOOST_SYMBOL_VISIBLE too_many_positional_options_error : public error {
0048     public:
0049         too_many_positional_options_error() 
0050          : error("too many positional options have been specified on the command line") 
0051         {}
0052     };
0053 
0054     /** Class thrown when there are programming error related to style */
0055     class BOOST_PROGRAM_OPTIONS_DECL BOOST_SYMBOL_VISIBLE invalid_command_line_style : public error {
0056     public:
0057         invalid_command_line_style(const std::string& msg)
0058         : error(msg)
0059         {}
0060     };
0061 
0062     /** Class thrown if config file can not be read */
0063     class BOOST_PROGRAM_OPTIONS_DECL BOOST_SYMBOL_VISIBLE reading_file : public error {
0064     public:
0065         reading_file(const char* filename)
0066          : error(std::string("can not read options configuration file '").append(filename).append("'"))
0067         {}
0068     };
0069 
0070 
0071     /** Base class for most exceptions in the library.
0072      *  
0073      *  Substitutes the values for the parameter name
0074      *      placeholders in the template to create the human
0075      *      readable error message
0076      *  
0077      *  Placeholders are surrounded by % signs: %example%
0078      *      Poor man's version of boost::format
0079      *  
0080      *  If a parameter name is absent, perform default substitutions
0081      *      instead so ugly placeholders are never left in-place.
0082      *  
0083      *  Options are displayed in "canonical" form
0084      *      This is the most unambiguous form of the
0085      *      *parsed* option name and would correspond to
0086      *      option_description::format_name()
0087      *      i.e. what is shown by print_usage()
0088      *  
0089      *  The "canonical" form depends on whether the option is
0090      *      specified in short or long form, using dashes or slashes
0091      *      or without a prefix (from a configuration file)
0092      *  
0093      *   */
0094     class BOOST_PROGRAM_OPTIONS_DECL BOOST_SYMBOL_VISIBLE error_with_option_name : public error {
0095 
0096     protected:
0097         /** can be
0098          *      0 = no prefix (config file options)
0099          *      allow_long
0100          *      allow_dash_for_short
0101          *      allow_slash_for_short
0102          *      allow_long_disguise */
0103         int m_option_style;
0104 
0105 
0106         /** substitutions
0107          *  from placeholders to values */
0108         std::map<std::string, std::string> m_substitutions;
0109         typedef std::pair<std::string, std::string> string_pair;
0110         std::map<std::string, string_pair > m_substitution_defaults;
0111 
0112     public:
0113         /** template with placeholders */
0114         std::string m_error_template;
0115 
0116         error_with_option_name(const std::string& template_,
0117                                const std::string& option_name = "",
0118                                const std::string& original_token = "",
0119                                int option_style = 0);
0120 
0121         /** gcc says that throw specification on dtor is loosened 
0122          *  without this line                                     
0123          *  */ 
0124         BOOST_DEFAULTED_FUNCTION(~error_with_option_name() throw(), {})
0125 
0126 
0127         //void dump() const
0128         //{
0129         //  std::cerr << "m_substitution_defaults:\n";
0130         //  for (std::map<std::string, string_pair>::const_iterator iter = m_substitution_defaults.begin();
0131         //        iter != m_substitution_defaults.end(); ++iter)
0132         //      std::cerr << "\t" << iter->first << ":" << iter->second.first << "=" << iter->second.second << "\n";
0133         //  std::cerr << "m_substitutions:\n";
0134         //  for (std::map<std::string, std::string>::const_iterator iter = m_substitutions.begin();
0135         //        iter != m_substitutions.end(); ++iter)
0136         //      std::cerr << "\t" << iter->first << "=" << iter->second << "\n";
0137         //  std::cerr << "m_error_template:\n";
0138         //  std::cerr << "\t" << m_error_template << "\n";
0139         //  std::cerr << "canonical_option_prefix:[" << get_canonical_option_prefix() << "]\n";
0140         //  std::cerr << "canonical_option_name:[" << get_canonical_option_name() <<"]\n";
0141         //  std::cerr << "what:[" << what() << "]\n";
0142         //}
0143 
0144         /** Substitute
0145          *      parameter_name->value to create the error message from
0146          *      the error template */
0147         void set_substitute(const std::string& parameter_name,  const std::string& value)
0148         {           m_substitutions[parameter_name] = value;    }
0149 
0150         /** If the parameter is missing, then make the
0151          *      from->to substitution instead */
0152         void set_substitute_default(const std::string& parameter_name, 
0153                                     const std::string& from,  
0154                                     const std::string& to)
0155         {           
0156             m_substitution_defaults[parameter_name] = std::make_pair(from, to); 
0157         }
0158 
0159 
0160         /** Add context to an exception */
0161         void add_context(const std::string& option_name,
0162                          const std::string& original_token,
0163                          int option_style)
0164         {
0165             set_option_name(option_name);
0166             set_original_token(original_token);
0167             set_prefix(option_style);
0168         }
0169 
0170         void set_prefix(int option_style)
0171         {           m_option_style = option_style;}
0172 
0173         /** Overridden in error_with_no_option_name */
0174         virtual void set_option_name(const std::string& option_name)
0175         {           set_substitute("option", option_name);}
0176 
0177         std::string get_option_name() const
0178         {           return get_canonical_option_name();         }
0179 
0180         void set_original_token(const std::string& original_token)
0181         {           set_substitute("original_token", original_token);}
0182 
0183 
0184         /** Creates the error_message on the fly
0185          *      Currently a thin wrapper for substitute_placeholders() */
0186         virtual const char* what() const throw();
0187 
0188     protected:
0189         /** Used to hold the error text returned by what() */
0190         mutable std::string m_message;  // For on-demand formatting in 'what'
0191 
0192         /** Makes all substitutions using the template */
0193         virtual void substitute_placeholders(const std::string& error_template) const;
0194 
0195         // helper function for substitute_placeholders
0196         void replace_token(const std::string& from, const std::string& to) const;
0197 
0198         /** Construct option name in accordance with the appropriate
0199          *  prefix style: i.e. long dash or short slash etc */
0200         std::string get_canonical_option_name() const;
0201         std::string get_canonical_option_prefix() const;
0202     };
0203 
0204 
0205     /** Class thrown when there are several option values, but
0206         user called a method which cannot return them all. */
0207     class BOOST_PROGRAM_OPTIONS_DECL BOOST_SYMBOL_VISIBLE multiple_values : public error_with_option_name {
0208     public:
0209         multiple_values() 
0210          : error_with_option_name("option '%canonical_option%' only takes a single argument"){}
0211 
0212         BOOST_DEFAULTED_FUNCTION(~multiple_values() throw(), {})
0213     };
0214 
0215     /** Class thrown when there are several occurrences of an
0216         option, but user called a method which cannot return 
0217         them all. */
0218     class BOOST_PROGRAM_OPTIONS_DECL BOOST_SYMBOL_VISIBLE multiple_occurrences : public error_with_option_name {
0219     public:
0220         multiple_occurrences() 
0221          : error_with_option_name("option '%canonical_option%' cannot be specified more than once"){}
0222 
0223         BOOST_DEFAULTED_FUNCTION(~multiple_occurrences() throw(), {})
0224 
0225     };
0226 
0227     /** Class thrown when a required/mandatory option is missing */
0228     class BOOST_PROGRAM_OPTIONS_DECL BOOST_SYMBOL_VISIBLE required_option : public error_with_option_name {
0229     public:
0230        // option name is constructed by the option_descriptor and never on the fly
0231        required_option(const std::string& option_name)
0232        : error_with_option_name("the option '%canonical_option%' is required but missing", "", option_name)
0233        {
0234        }
0235 
0236        BOOST_DEFAULTED_FUNCTION(~required_option() throw(), {})
0237     };
0238 
0239     /** Base class of unparsable options,
0240      *  when the desired option cannot be identified.
0241      *  
0242      *  
0243      *  It makes no sense to have an option name, when we can't match an option to the
0244      *      parameter
0245      *  
0246      *  Having this a part of the error_with_option_name hierachy makes error handling
0247      *      a lot easier, even if the name indicates some sort of conceptual dissonance!
0248      *  
0249      *   */
0250     class BOOST_PROGRAM_OPTIONS_DECL BOOST_SYMBOL_VISIBLE error_with_no_option_name : public error_with_option_name {
0251     public:
0252         error_with_no_option_name(const std::string& template_,
0253                               const std::string& original_token = "")
0254         : error_with_option_name(template_, "", original_token)
0255         {
0256         }
0257 
0258         /** Does NOT set option name, because no option name makes sense */
0259         virtual void set_option_name(const std::string&) {}
0260 
0261         BOOST_DEFAULTED_FUNCTION(~error_with_no_option_name() throw(), {})
0262     };
0263 
0264 
0265     /** Class thrown when option name is not recognized. */
0266     class BOOST_PROGRAM_OPTIONS_DECL BOOST_SYMBOL_VISIBLE unknown_option : public error_with_no_option_name {
0267     public:
0268         unknown_option(const std::string& original_token = "")
0269         : error_with_no_option_name("unrecognised option '%canonical_option%'", original_token)
0270         {
0271         }
0272 
0273         BOOST_DEFAULTED_FUNCTION(~unknown_option() throw(), {})
0274     };
0275 
0276 
0277 
0278     /** Class thrown when there's ambiguity amoung several possible options. */
0279     class BOOST_PROGRAM_OPTIONS_DECL BOOST_SYMBOL_VISIBLE ambiguous_option : public error_with_no_option_name {
0280     public:
0281         ambiguous_option(const std::vector<std::string>& xalternatives)
0282         : error_with_no_option_name("option '%canonical_option%' is ambiguous"),
0283             m_alternatives(xalternatives)
0284         {}
0285 
0286         BOOST_DEFAULTED_FUNCTION(~ambiguous_option() throw(), {})
0287 
0288         const std::vector<std::string>& alternatives() const throw() {return m_alternatives;}
0289 
0290     protected:
0291         /** Makes all substitutions using the template */
0292         virtual void substitute_placeholders(const std::string& error_template) const;
0293     private:
0294         // TODO: copy ctor might throw
0295         std::vector<std::string> m_alternatives;
0296     };
0297 
0298 
0299     /** Class thrown when there's syntax error either for command
0300      *  line or config file options. See derived children for
0301      *  concrete classes. */
0302     class BOOST_PROGRAM_OPTIONS_DECL BOOST_SYMBOL_VISIBLE invalid_syntax : public error_with_option_name {
0303     public:
0304         enum kind_t {
0305             long_not_allowed = 30,
0306             long_adjacent_not_allowed,
0307             short_adjacent_not_allowed,
0308             empty_adjacent_parameter,
0309             missing_parameter,
0310             extra_parameter,
0311             unrecognized_line
0312         };
0313 
0314         invalid_syntax(kind_t kind, 
0315                        const std::string& option_name = "",
0316                        const std::string& original_token = "",
0317                        int option_style              = 0):
0318             error_with_option_name(get_template(kind), option_name, original_token, option_style),
0319             m_kind(kind)
0320         {
0321         }
0322 
0323         BOOST_DEFAULTED_FUNCTION(~invalid_syntax() throw(), {})
0324 
0325         kind_t kind() const {return m_kind;}
0326 
0327         /** Convenience functions for backwards compatibility */
0328         virtual std::string tokens() const {return get_option_name();   }
0329     protected:
0330         /** Used to convert kind_t to a related error text */
0331         std::string get_template(kind_t kind);
0332         kind_t m_kind;
0333     };
0334 
0335     class BOOST_PROGRAM_OPTIONS_DECL BOOST_SYMBOL_VISIBLE invalid_config_file_syntax : public invalid_syntax {
0336     public:
0337         invalid_config_file_syntax(const std::string& invalid_line, kind_t kind):
0338             invalid_syntax(kind)
0339         {
0340             m_substitutions["invalid_line"] = invalid_line;
0341         }
0342 
0343         BOOST_DEFAULTED_FUNCTION(~invalid_config_file_syntax() throw(), {})
0344 
0345         /** Convenience functions for backwards compatibility */
0346         virtual std::string tokens() const {return m_substitutions.find("invalid_line")->second;    }
0347     };
0348 
0349 
0350     /** Class thrown when there are syntax errors in given command line */
0351     class BOOST_PROGRAM_OPTIONS_DECL BOOST_SYMBOL_VISIBLE invalid_command_line_syntax : public invalid_syntax {
0352     public:
0353         invalid_command_line_syntax(kind_t kind,
0354                        const std::string& option_name = "",
0355                        const std::string& original_token = "",
0356                        int option_style              = 0):
0357             invalid_syntax(kind, option_name, original_token, option_style) {}
0358         BOOST_DEFAULTED_FUNCTION(~invalid_command_line_syntax() throw(), {})
0359     };
0360 
0361 
0362     /** Class thrown when value of option is incorrect. */
0363     class BOOST_PROGRAM_OPTIONS_DECL BOOST_SYMBOL_VISIBLE validation_error : public error_with_option_name {
0364     public:
0365         enum kind_t {
0366             multiple_values_not_allowed = 30,
0367             at_least_one_value_required, 
0368             invalid_bool_value,
0369             invalid_option_value,
0370             invalid_option
0371         };
0372         
0373     public:
0374         validation_error(kind_t kind, 
0375                    const std::string& option_name = "",
0376                    const std::string& original_token = "",
0377                    int option_style              = 0):
0378         error_with_option_name(get_template(kind), option_name, original_token, option_style),
0379         m_kind(kind)
0380         {
0381         }
0382 
0383         BOOST_DEFAULTED_FUNCTION(~validation_error() throw(), {})
0384 
0385         kind_t kind() const { return m_kind; }
0386 
0387     protected:
0388         /** Used to convert kind_t to a related error text */
0389         std::string get_template(kind_t kind);
0390         kind_t m_kind;
0391     };
0392 
0393     /** Class thrown if there is an invalid option value given */
0394     class BOOST_PROGRAM_OPTIONS_DECL BOOST_SYMBOL_VISIBLE invalid_option_value
0395         : public validation_error
0396     {
0397     public:
0398         invalid_option_value(const std::string& value);
0399 #ifndef BOOST_NO_STD_WSTRING
0400         invalid_option_value(const std::wstring& value);
0401 #endif
0402     };
0403 
0404     /** Class thrown if there is an invalid bool value given */
0405     class BOOST_PROGRAM_OPTIONS_DECL BOOST_SYMBOL_VISIBLE invalid_bool_value
0406         : public validation_error
0407     {
0408     public:
0409         invalid_bool_value(const std::string& value);
0410     };
0411 
0412 
0413 
0414 
0415 
0416     
0417 
0418 }}
0419 
0420 #if defined(BOOST_MSVC)
0421 #   pragma warning (pop)
0422 #endif
0423 
0424 #endif