Handle missing equal sign for Options with values.
authorRafał Długołęcki <rafal@dlugolecki.net.pl>
Tue, 12 May 2015 21:14:25 +0000 (23:14 +0200)
committerRafał Długołęcki <rafal@dlugolecki.net.pl>
Tue, 12 May 2015 21:14:25 +0000 (23:14 +0200)
include/exception/optionValueNotSpecified.h [new file with mode: 0644]
include/multiValue.h
include/option.h

diff --git a/include/exception/optionValueNotSpecified.h b/include/exception/optionValueNotSpecified.h
new file mode 100644 (file)
index 0000000..e6fbf54
--- /dev/null
@@ -0,0 +1,26 @@
+#ifndef __COMMAND_EXCEPTION_OPTION_VALUE_NOT_SPECIFIED_H
+#define __COMMAND_EXCEPTION_OPTION_VALUE_NOT_SPECIFIED_H
+
+#include <stdexcept>
+#include <string>
+
+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 */
index 90de29a9787d5f229de0935674dc0f9fb4a2e528..62fbf4d150c0dce495cabf6037d5a236586a6946 100644 (file)
@@ -2,6 +2,7 @@
 #define __COMMAND_MULTIVALUE_H
 
 #include <iostream>
+#include <vector>
 
 #include "parameter.h"
 
index 260659e6847efbe8466c7e3c4ab997ebfe48d477..d66164d38d6f622e7f81440c5ba4845bcd4e6a7a 100644 (file)
@@ -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");
         }
     };
 }