errors.hpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  1. // Copyright Vladimir Prus 2002-2004.
  2. // Distributed under the Boost Software License, Version 1.0.
  3. // (See accompanying file LICENSE_1_0.txt
  4. // or copy at http://www.boost.org/LICENSE_1_0.txt)
  5. #ifndef BOOST_ERRORS_VP_2003_01_02
  6. #define BOOST_ERRORS_VP_2003_01_02
  7. #include <boost/program_options/config.hpp>
  8. #include <string>
  9. #include <stdexcept>
  10. #include <vector>
  11. #include <map>
  12. #if defined(BOOST_MSVC)
  13. # pragma warning (push)
  14. # pragma warning (disable:4275) // non dll-interface class 'std::logic_error' used as base for dll-interface class 'boost::program_options::error'
  15. # 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'
  16. #endif
  17. namespace boost { namespace program_options {
  18. inline std::string strip_prefixes(const std::string& text)
  19. {
  20. return text.substr(text.find_last_of("-/") + 1);
  21. }
  22. /** Base class for all errors in the library. */
  23. class BOOST_PROGRAM_OPTIONS_DECL error : public std::logic_error {
  24. public:
  25. error(const std::string& xwhat) : std::logic_error(xwhat) {}
  26. };
  27. /** Class thrown when there are too many positional options.
  28. This is a programming error.
  29. */
  30. class BOOST_PROGRAM_OPTIONS_DECL too_many_positional_options_error : public error {
  31. public:
  32. too_many_positional_options_error()
  33. : error("too many positional options have been specified on the command line")
  34. {}
  35. };
  36. /** Class thrown when there are programming error related to style */
  37. class BOOST_PROGRAM_OPTIONS_DECL invalid_command_line_style : public error {
  38. public:
  39. invalid_command_line_style(const std::string& msg)
  40. : error(msg)
  41. {}
  42. };
  43. /** Class thrown if config file can not be read */
  44. class BOOST_PROGRAM_OPTIONS_DECL reading_file : public error {
  45. public:
  46. reading_file(const char* filename)
  47. : error(std::string("can not read options configuration file '").append(filename).append("'"))
  48. {}
  49. };
  50. /** Base class for most exceptions in the library.
  51. *
  52. * Substitutes the values for the parameter name
  53. * placeholders in the template to create the human
  54. * readable error message
  55. *
  56. * Placeholders are surrounded by % signs: %example%
  57. * Poor man's version of boost::format
  58. *
  59. * If a parameter name is absent, perform default substitutions
  60. * instead so ugly placeholders are never left in-place.
  61. *
  62. * Options are displayed in "canonical" form
  63. * This is the most unambiguous form of the
  64. * *parsed* option name and would correspond to
  65. * option_description::format_name()
  66. * i.e. what is shown by print_usage()
  67. *
  68. * The "canonical" form depends on whether the option is
  69. * specified in short or long form, using dashes or slashes
  70. * or without a prefix (from a configuration file)
  71. *
  72. * */
  73. class BOOST_PROGRAM_OPTIONS_DECL error_with_option_name : public error {
  74. protected:
  75. /** can be
  76. * 0 = no prefix (config file options)
  77. * allow_long
  78. * allow_dash_for_short
  79. * allow_slash_for_short
  80. * allow_long_disguise */
  81. int m_option_style;
  82. /** substitutions
  83. * from placeholders to values */
  84. std::map<std::string, std::string> m_substitutions;
  85. typedef std::pair<std::string, std::string> string_pair;
  86. std::map<std::string, string_pair > m_substitution_defaults;
  87. public:
  88. /** template with placeholders */
  89. std::string m_error_template;
  90. error_with_option_name(const std::string& template_,
  91. const std::string& option_name = "",
  92. const std::string& original_token = "",
  93. int option_style = 0);
  94. /** gcc says that throw specification on dtor is loosened
  95. * without this line
  96. * */
  97. ~error_with_option_name() throw() {}
  98. //void dump() const
  99. //{
  100. // std::cerr << "m_substitution_defaults:\n";
  101. // for (std::map<std::string, string_pair>::const_iterator iter = m_substitution_defaults.begin();
  102. // iter != m_substitution_defaults.end(); ++iter)
  103. // std::cerr << "\t" << iter->first << ":" << iter->second.first << "=" << iter->second.second << "\n";
  104. // std::cerr << "m_substitutions:\n";
  105. // for (std::map<std::string, std::string>::const_iterator iter = m_substitutions.begin();
  106. // iter != m_substitutions.end(); ++iter)
  107. // std::cerr << "\t" << iter->first << "=" << iter->second << "\n";
  108. // std::cerr << "m_error_template:\n";
  109. // std::cerr << "\t" << m_error_template << "\n";
  110. // std::cerr << "canonical_option_prefix:[" << get_canonical_option_prefix() << "]\n";
  111. // std::cerr << "canonical_option_name:[" << get_canonical_option_name() <<"]\n";
  112. // std::cerr << "what:[" << what() << "]\n";
  113. //}
  114. /** Substitute
  115. * parameter_name->value to create the error message from
  116. * the error template */
  117. void set_substitute(const std::string& parameter_name, const std::string& value)
  118. { m_substitutions[parameter_name] = value; }
  119. /** If the parameter is missing, then make the
  120. * from->to substitution instead */
  121. void set_substitute_default(const std::string& parameter_name,
  122. const std::string& from,
  123. const std::string& to)
  124. {
  125. m_substitution_defaults[parameter_name] = std::make_pair(from, to);
  126. }
  127. /** Add context to an exception */
  128. void add_context(const std::string& option_name,
  129. const std::string& original_token,
  130. int option_style)
  131. {
  132. set_option_name(option_name);
  133. set_original_token(original_token);
  134. set_prefix(option_style);
  135. }
  136. void set_prefix(int option_style)
  137. { m_option_style = option_style;}
  138. /** Overridden in error_with_no_option_name */
  139. virtual void set_option_name(const std::string& option_name)
  140. { set_substitute("option", option_name);}
  141. std::string get_option_name() const throw()
  142. { return get_canonical_option_name(); }
  143. void set_original_token(const std::string& original_token)
  144. { set_substitute("original_token", original_token);}
  145. /** Creates the error_message on the fly
  146. * Currently a thin wrapper for substitute_placeholders() */
  147. virtual const char* what() const throw();
  148. protected:
  149. /** Used to hold the error text returned by what() */
  150. mutable std::string m_message; // For on-demand formatting in 'what'
  151. /** Makes all substitutions using the template */
  152. virtual void substitute_placeholders(const std::string& error_template) const;
  153. // helper function for substitute_placeholders
  154. void replace_token(const std::string& from, const std::string& to) const;
  155. /** Construct option name in accordance with the appropriate
  156. * prefix style: i.e. long dash or short slash etc */
  157. std::string get_canonical_option_name() const;
  158. std::string get_canonical_option_prefix() const;
  159. };
  160. /** Class thrown when there are several option values, but
  161. user called a method which cannot return them all. */
  162. class BOOST_PROGRAM_OPTIONS_DECL multiple_values : public error_with_option_name {
  163. public:
  164. multiple_values()
  165. : error_with_option_name("option '%canonical_option%' only takes a single argument"){}
  166. ~multiple_values() throw() {}
  167. };
  168. /** Class thrown when there are several occurrences of an
  169. option, but user called a method which cannot return
  170. them all. */
  171. class BOOST_PROGRAM_OPTIONS_DECL multiple_occurrences : public error_with_option_name {
  172. public:
  173. multiple_occurrences()
  174. : error_with_option_name("option '%canonical_option%' cannot be specified more than once"){}
  175. ~multiple_occurrences() throw() {}
  176. };
  177. /** Class thrown when a required/mandatory option is missing */
  178. class BOOST_PROGRAM_OPTIONS_DECL required_option : public error_with_option_name {
  179. public:
  180. // option name is constructed by the option_descriptor and never on the fly
  181. required_option(const std::string& option_name)
  182. : error_with_option_name("the option '%canonical_option%' is required but missing", "", option_name)
  183. {
  184. }
  185. ~required_option() throw() {}
  186. };
  187. /** Base class of unparsable options,
  188. * when the desired option cannot be identified.
  189. *
  190. *
  191. * It makes no sense to have an option name, when we can't match an option to the
  192. * parameter
  193. *
  194. * Having this a part of the error_with_option_name hierachy makes error handling
  195. * a lot easier, even if the name indicates some sort of conceptual dissonance!
  196. *
  197. * */
  198. class BOOST_PROGRAM_OPTIONS_DECL error_with_no_option_name : public error_with_option_name {
  199. public:
  200. error_with_no_option_name(const std::string& template_,
  201. const std::string& original_token = "")
  202. : error_with_option_name(template_, "", original_token)
  203. {
  204. }
  205. /** Does NOT set option name, because no option name makes sense */
  206. virtual void set_option_name(const std::string&) {}
  207. ~error_with_no_option_name() throw() {}
  208. };
  209. /** Class thrown when option name is not recognized. */
  210. class BOOST_PROGRAM_OPTIONS_DECL unknown_option : public error_with_no_option_name {
  211. public:
  212. unknown_option(const std::string& original_token = "")
  213. : error_with_no_option_name("unrecognised option '%canonical_option%'", original_token)
  214. {
  215. }
  216. ~unknown_option() throw() {}
  217. };
  218. /** Class thrown when there's ambiguity amoung several possible options. */
  219. class BOOST_PROGRAM_OPTIONS_DECL ambiguous_option : public error_with_no_option_name {
  220. public:
  221. ambiguous_option(const std::vector<std::string>& xalternatives)
  222. : error_with_no_option_name("option '%canonical_option%' is ambiguous"),
  223. m_alternatives(xalternatives)
  224. {}
  225. ~ambiguous_option() throw() {}
  226. const std::vector<std::string>& alternatives() const throw() {return m_alternatives;}
  227. protected:
  228. /** Makes all substitutions using the template */
  229. virtual void substitute_placeholders(const std::string& error_template) const;
  230. private:
  231. // TODO: copy ctor might throw
  232. std::vector<std::string> m_alternatives;
  233. };
  234. /** Class thrown when there's syntax error either for command
  235. * line or config file options. See derived children for
  236. * concrete classes. */
  237. class BOOST_PROGRAM_OPTIONS_DECL invalid_syntax : public error_with_option_name {
  238. public:
  239. enum kind_t {
  240. long_not_allowed = 30,
  241. long_adjacent_not_allowed,
  242. short_adjacent_not_allowed,
  243. empty_adjacent_parameter,
  244. missing_parameter,
  245. extra_parameter,
  246. unrecognized_line
  247. };
  248. invalid_syntax(kind_t kind,
  249. const std::string& option_name = "",
  250. const std::string& original_token = "",
  251. int option_style = 0):
  252. error_with_option_name(get_template(kind), option_name, original_token, option_style),
  253. m_kind(kind)
  254. {
  255. }
  256. ~invalid_syntax() throw() {}
  257. kind_t kind() const {return m_kind;}
  258. /** Convenience functions for backwards compatibility */
  259. virtual std::string tokens() const {return get_option_name(); }
  260. protected:
  261. /** Used to convert kind_t to a related error text */
  262. std::string get_template(kind_t kind);
  263. kind_t m_kind;
  264. };
  265. class BOOST_PROGRAM_OPTIONS_DECL invalid_config_file_syntax : public invalid_syntax {
  266. public:
  267. invalid_config_file_syntax(const std::string& invalid_line, kind_t kind):
  268. invalid_syntax(kind)
  269. {
  270. m_substitutions["invalid_line"] = invalid_line;
  271. }
  272. ~invalid_config_file_syntax() throw() {}
  273. /** Convenience functions for backwards compatibility */
  274. virtual std::string tokens() const {return m_substitutions.find("invalid_line")->second; }
  275. };
  276. /** Class thrown when there are syntax errors in given command line */
  277. class BOOST_PROGRAM_OPTIONS_DECL invalid_command_line_syntax : public invalid_syntax {
  278. public:
  279. invalid_command_line_syntax(kind_t kind,
  280. const std::string& option_name = "",
  281. const std::string& original_token = "",
  282. int option_style = 0):
  283. invalid_syntax(kind, option_name, original_token, option_style) {}
  284. ~invalid_command_line_syntax() throw() {}
  285. };
  286. /** Class thrown when value of option is incorrect. */
  287. class BOOST_PROGRAM_OPTIONS_DECL validation_error : public error_with_option_name {
  288. public:
  289. enum kind_t {
  290. multiple_values_not_allowed = 30,
  291. at_least_one_value_required,
  292. invalid_bool_value,
  293. invalid_option_value,
  294. invalid_option
  295. };
  296. public:
  297. validation_error(kind_t kind,
  298. const std::string& option_name = "",
  299. const std::string& original_token = "",
  300. int option_style = 0):
  301. error_with_option_name(get_template(kind), option_name, original_token, option_style)
  302. {
  303. }
  304. ~validation_error() throw() {}
  305. protected:
  306. /** Used to convert kind_t to a related error text */
  307. std::string get_template(kind_t kind);
  308. kind_t m_kind;
  309. };
  310. /** Class thrown if there is an invalid option value given */
  311. class BOOST_PROGRAM_OPTIONS_DECL invalid_option_value
  312. : public validation_error
  313. {
  314. public:
  315. invalid_option_value(const std::string& value);
  316. #ifndef BOOST_NO_STD_WSTRING
  317. invalid_option_value(const std::wstring& value);
  318. #endif
  319. };
  320. /** Class thrown if there is an invalid bool value given */
  321. class BOOST_PROGRAM_OPTIONS_DECL invalid_bool_value
  322. : public validation_error
  323. {
  324. public:
  325. invalid_bool_value(const std::string& value);
  326. };
  327. }}
  328. #if defined(BOOST_MSVC)
  329. # pragma warning (pop)
  330. #endif
  331. #endif