ArgException.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. // -*- Mode: c++; c-basic-offset: 4; tab-width: 4; -*-
  2. /******************************************************************************
  3. *
  4. * file: ArgException.h
  5. *
  6. * Copyright (c) 2003, Michael E. Smoot .
  7. * Copyright (c) 2017 Google LLC
  8. * All rights reserved.
  9. *
  10. * See the file COPYING in the top directory of this distribution for
  11. * more information.
  12. *
  13. * THE SOFTWARE IS PROVIDED _AS IS_, WITHOUT WARRANTY OF ANY KIND, EXPRESS
  14. * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  16. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  18. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  19. * DEALINGS IN THE SOFTWARE.
  20. *
  21. *****************************************************************************/
  22. #ifndef TCLAP_ARG_EXCEPTION_H
  23. #define TCLAP_ARG_EXCEPTION_H
  24. #include <string>
  25. #include <exception>
  26. namespace TCLAP {
  27. /**
  28. * A simple class that defines and argument exception. Should be caught
  29. * whenever a CmdLine is created and parsed.
  30. */
  31. class ArgException : public std::exception
  32. {
  33. public:
  34. /**
  35. * Constructor.
  36. * \param text - The text of the exception.
  37. * \param id - The text identifying the argument source.
  38. * \param td - Text describing the type of ArgException it is.
  39. * of the exception.
  40. */
  41. ArgException( const std::string& text = "undefined exception",
  42. const std::string& id = "undefined",
  43. const std::string& td = "Generic ArgException")
  44. : std::exception(),
  45. _errorText(text),
  46. _argId( id ),
  47. _typeDescription(td)
  48. { }
  49. /**
  50. * Destructor.
  51. */
  52. virtual ~ArgException() throw() { }
  53. /**
  54. * Returns the error text.
  55. */
  56. std::string error() const { return ( _errorText ); }
  57. /**
  58. * Returns the argument id.
  59. */
  60. std::string argId() const
  61. {
  62. if ( _argId == "undefined" )
  63. return " ";
  64. else
  65. return ( "Argument: " + _argId );
  66. }
  67. /**
  68. * Returns the arg id and error text.
  69. */
  70. const char* what() const throw()
  71. {
  72. static std::string ex;
  73. ex = _argId + " -- " + _errorText;
  74. return ex.c_str();
  75. }
  76. /**
  77. * Returns the type of the exception. Used to explain and distinguish
  78. * between different child exceptions.
  79. */
  80. std::string typeDescription() const
  81. {
  82. return _typeDescription;
  83. }
  84. private:
  85. /**
  86. * The text of the exception message.
  87. */
  88. std::string _errorText;
  89. /**
  90. * The argument related to this exception.
  91. */
  92. std::string _argId;
  93. /**
  94. * Describes the type of the exception. Used to distinguish
  95. * between different child exceptions.
  96. */
  97. std::string _typeDescription;
  98. };
  99. /**
  100. * Thrown from within the child Arg classes when it fails to properly
  101. * parse the argument it has been passed.
  102. */
  103. class ArgParseException : public ArgException
  104. {
  105. public:
  106. /**
  107. * Constructor.
  108. * \param text - The text of the exception.
  109. * \param id - The text identifying the argument source
  110. * of the exception.
  111. */
  112. ArgParseException( const std::string& text = "undefined exception",
  113. const std::string& id = "undefined" )
  114. : ArgException( text,
  115. id,
  116. std::string( "Exception found while parsing " ) +
  117. std::string( "the value the Arg has been passed." ))
  118. { }
  119. };
  120. /**
  121. * Thrown from CmdLine when the arguments on the command line are not
  122. * properly specified, e.g. too many arguments, required argument missing, etc.
  123. */
  124. class CmdLineParseException : public ArgException
  125. {
  126. public:
  127. /**
  128. * Constructor.
  129. * \param text - The text of the exception.
  130. * \param id - The text identifying the argument source
  131. * of the exception.
  132. */
  133. CmdLineParseException( const std::string& text = "undefined exception",
  134. const std::string& id = "undefined" )
  135. : ArgException( text,
  136. id,
  137. std::string( "Exception found when the values ") +
  138. std::string( "on the command line do not meet ") +
  139. std::string( "the requirements of the defined ") +
  140. std::string( "Args." ))
  141. { }
  142. };
  143. /**
  144. * Thrown from Arg and CmdLine when an Arg is improperly specified, e.g.
  145. * same flag as another Arg, same name, etc.
  146. */
  147. class SpecificationException : public ArgException
  148. {
  149. public:
  150. /**
  151. * Constructor.
  152. * \param text - The text of the exception.
  153. * \param id - The text identifying the argument source
  154. * of the exception.
  155. */
  156. SpecificationException( const std::string& text = "undefined exception",
  157. const std::string& id = "undefined" )
  158. : ArgException( text,
  159. id,
  160. std::string("Exception found when an Arg object ")+
  161. std::string("is improperly defined by the ") +
  162. std::string("developer." ))
  163. { }
  164. };
  165. /**
  166. * Thrown when TCLAP thinks the program should exit.
  167. *
  168. * For example after parse error this exception will be thrown (and
  169. * normally caught). This allows any resource to be clened properly
  170. * before exit.
  171. *
  172. * If exception handling is disabled (CmdLine::setExceptionHandling),
  173. * this exception will propagate to the call site, allowing the
  174. * program to catch it and avoid program termination, or do it's own
  175. * cleanup. See for example, https://sourceforge.net/p/tclap/bugs/29.
  176. */
  177. class ExitException {
  178. public:
  179. ExitException(int estat) : _estat(estat) {}
  180. int getExitStatus() const { return _estat; }
  181. private:
  182. int _estat;
  183. };
  184. } // namespace TCLAP
  185. #endif