#include <typeinfo>
#include "parameter.h"
+#include "exception/missingRequiredParameter.h"
namespace command {
/**
try {
matchArguments(argc, argv);
}
- catch(std::invalid_argument exception) {
+ catch(const std::invalid_argument & exception) {
releaseMemory();
throw;
}
- catch(std::logic_error exception) {
+ catch(const std::logic_error & exception) {
releaseMemory();
throw;
}
}
for(Parameter *param : parameters) {
if (param->isRequired() && !param->isUsed()) {
- throw std::logic_error(param->describe() + " is required but it was not passed");
+ throw MissingRequiredParameter(param->describe() + " is required but it was not passed");
}
}
}
namespace command {
/**
- * Helper template class used for releasing resources.
+ * Exception thrown used when Option should have value, but no one has been set
*/
class MissingOptionValue : public std::invalid_argument {
-private:
- std::string message;
public:
+ /** \inheritdoc */
explicit MissingOptionValue(const std::string& what_arg) :
- std::invalid_argument(what_arg), message(what_arg) { }
+ std::invalid_argument(what_arg) { }
+ /** \inheritdoc */
explicit MissingOptionValue(const char* what_arg) :
- std::invalid_argument(what_arg), message(what_arg) { }
-
- virtual const char* what() const throw() {
- return message.c_str();
- }
+ std::invalid_argument(what_arg) { }
};
}
--- /dev/null
+#ifndef __COMMAND_EXCEPTION_MISSING_REQUIRED_PARAMETER_H
+#define __COMMAND_EXCEPTION_MISSING_REQUIRED_PARAMETER_H
+
+#include <stdexcept>
+#include <string>
+
+namespace command {
+
+/**
+ * Exception thrown when required argument was not set
+ */
+class MissingRequiredParameter : public std::logic_error {
+public:
+ /** \inheritdoc */
+ explicit MissingRequiredParameter(const std::string& what_arg) :
+ std::logic_error(what_arg) { }
+
+ /** \inheritdoc */
+ explicit MissingRequiredParameter(const char* what_arg) :
+ std::logic_error(what_arg) { }
+};
+
+}
+
+#endif /* __COMMAND_EXCEPTION_MISSING_REQUIRED_PARAMETER_H */
namespace command {
/**
- * Helper template class used for releasing resources.
+ * Exception thrown used when Option's value failed conversion to specific type
+ *
+ * e.g.:
+ * "a" -> int
*/
class OptionFailedConversion : public std::invalid_argument {
-protected:
- std::string message;
public:
+ /** \inheritdoc */
explicit OptionFailedConversion(const std::string& what_arg) :
- std::invalid_argument(what_arg), message(what_arg) { }
+ std::invalid_argument(what_arg) { }
+ /** \inheritdoc */
explicit OptionFailedConversion(const char* what_arg) :
- std::invalid_argument(what_arg), message(what_arg) { }
-
- virtual const char* what() const throw() {
- return message.c_str();
- }
+ std::invalid_argument(what_arg) { }
};
}