errors.hpp 15 KB

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