From: Rafał Długołęcki Date: Tue, 12 May 2015 21:14:25 +0000 (+0200) Subject: Handle missing equal sign for Options with values. X-Git-Tag: v0.2~3 X-Git-Url: https://git.dlugolecki.net.pl/?p=command.git;a=commitdiff_plain;h=77267acae0a5c4b6c80392bbb9e07afbed3da043 Handle missing equal sign for Options with values. --- diff --git a/include/exception/optionValueNotSpecified.h b/include/exception/optionValueNotSpecified.h new file mode 100644 index 0000000..e6fbf54 --- /dev/null +++ b/include/exception/optionValueNotSpecified.h @@ -0,0 +1,26 @@ +#ifndef __COMMAND_EXCEPTION_OPTION_VALUE_NOT_SPECIFIED_H +#define __COMMAND_EXCEPTION_OPTION_VALUE_NOT_SPECIFIED_H + +#include +#include + +namespace command { + +/** + * Exception thrown when Option should have value, but no equal sign specifying + * it has been set + */ +class OptionValueNotSpecified : public std::invalid_argument { +public: + /** \inheritdoc */ + explicit OptionValueNotSpecified(const std::string& what_arg) : + std::invalid_argument(what_arg) { } + + /** \inheritdoc */ + explicit OptionValueNotSpecified(const char* what_arg) : + std::invalid_argument(what_arg) { } +}; + +} + +#endif /* __COMMAND_EXCEPTION_OPTION_VALUE_NOT_SPECIFIED_H */ diff --git a/include/multiValue.h b/include/multiValue.h index 90de29a..62fbf4d 100644 --- a/include/multiValue.h +++ b/include/multiValue.h @@ -2,6 +2,7 @@ #define __COMMAND_MULTIVALUE_H #include +#include #include "parameter.h" diff --git a/include/option.h b/include/option.h index 260659e..d66164d 100644 --- a/include/option.h +++ b/include/option.h @@ -8,6 +8,7 @@ #include "parameter.h" #include "exception/missingOptionValue.h" #include "exception/optionFailedConversion.h" +#include "exception/optionValueNotSpecified.h" namespace command { /** @@ -90,7 +91,7 @@ namespace command { virtual bool understand(const std::string & argv) { if (argv.find(name) == 0) { - std::size_t pos = argv.find("="); + std::size_t pos = this->valuePosition(argv); if (pos != name.size()) { throw MissingOptionValue("Option: " + name + " requires value but no one has been provided"); @@ -112,8 +113,14 @@ namespace command { /** * \inheritdoc */ - virtual unsigned int valuePosition(const std::string & argv) { - return argv.find("="); + virtual unsigned int valuePosition(const std::string & value) { + std::size_t pos = value.find("="); + + if (pos == std::string::npos) { + throw OptionValueNotSpecified("Option: " + name + " requires value to be specified using equal sign"); + } + + return pos; } }; @@ -185,7 +192,7 @@ namespace command { * \inheritdoc */ virtual unsigned int valuePosition(const std::string & ) { - throw new std::invalid_argument(this->describe() + "is void Option, so it does not have value part"); + throw new std::invalid_argument(this->describe() + " is void Option, so it does not have value part"); } }; }