Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.
 
 
 

201 righe
4.9 KiB

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