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