return false;
}
+
+ /**
+ * \inheritdoc
+ */
+ virtual unsigned int valuePosition(const std::string & ) {
+ return 0;
+ }
};
}
* \inheritdoc
*/
virtual bool understand(const std::string & value) {
- size_t start = 0;
+ size_t start = parameter->valuePosition(value);
size_t pos = 0;
bool _understand = true;
+ std::string prefix = "";
+
+ if (start > 0) {
+ prefix = value.substr(0, ++start);// always count: "="
+ }
do {
pos = value.find(separator, start);
- values.push_back(value.substr(start, pos-start));
+ values.push_back(prefix + value.substr(start, pos-start));
_understand &= parameter->understand(values.back());
start = pos + 1;
} while ((pos != std::string::npos) && (start < value.size()));
virtual bool isUsed() {
return parameter->isUsed();
};
+
+ /**
+ * Wrapper method around passed Parameter::valuePosition().
+ *
+ * \inheritdoc
+ */
+ virtual unsigned int valuePosition(const std::string & value) {
+ return parameter->valuePosition(value);
+ }
};
}
}
return false;
}
+
+ /**
+ * \inheritdoc
+ */
+ virtual unsigned int valuePosition(const std::string & argv) {
+ return argv.find("=");
+ }
};
/**
}
return false;
}
+
+ /**
+ * \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");
+ }
};
}
virtual bool isUsed() {
return used;
}
+
+ /**
+ * @return position where value starts in passed string
+ */
+ virtual unsigned int valuePosition(const std::string & ) = 0;
};
}
*/
virtual bool isUsed() {
return parameter->isUsed();
- };
+ }
+
+ /**
+ * Wrapper method around passed Parameter::valuePosition().
+ *
+ * \inheritdoc
+ */
+ virtual unsigned int valuePosition(const std::string & value) {
+ return parameter->valuePosition(value);
+ }
};
}
option_should_match_exact_name.test \
option_should_throw_exception_on_missing_value.test \
required_should_be_required.test \
- multivalue_should_extract_values_by_separator.test
+ multivalue_should_extract_arguments_by_separator.test \
+ multivalue_should_extract_options_by_separator.test
noinst_PROGRAMS = $(TESTS)
required_should_be_required_test_SOURCES = required/should_be_required.cpp
-multivalue_should_extract_values_by_separator_test_SOURCES = multiValue/should_extract_values_by_separator.cpp
+multivalue_should_extract_arguments_by_separator_test_SOURCES = multiValue/should_extract_arguments_by_separator.cpp
+multivalue_should_extract_options_by_separator_test_SOURCES = multiValue/should_extract_options_by_separator.cpp
--- /dev/null
+#include <iostream>
+#include <vector>
+
+#include "option.h"
+#include "multiValue.h"
+
+using namespace std;
+using namespace command;
+
+#define NAME "test"
+#define VALUE "0,1,2,3,4,5,6,7,8,9"
+
+#define OPTION NAME "=" VALUE
+
+typedef int OptionType;
+
+std::vector<OptionType> input;
+
+void function(OptionType value) {
+ input.push_back(value);
+ cout << "Catched value: " << value << "\n";
+}
+
+int main() {
+ Parameter * option = new MultiValue(",", new Option<OptionType>(NAME, "Option as multiValue int", function));
+
+ try {
+ if (option->understand(OPTION)) {
+ option->handle();
+ }
+ else {
+ cout << option->describe() << " should understand multiple int values\n";
+ return 1;
+ }
+ }
+ catch (...) {
+ delete option;
+ cout << option->describe() << " thrown unknown exception\n";
+ return 1;
+ }
+
+ bool test = true;
+ for (int i = 0; i < 10; i++) {
+ test &= (input[i] == i);
+ cout << i << ") input: " << input[i] << "\n";
+ }
+
+ if (test) {
+ cout << option->describe() << " handles boolean (TRUE) values\n";
+ delete option;
+ return 0;
+ }
+
+ cout << option->describe() << " do not handle multiple int values\n";
+ delete option;
+ return 1;
+}
virtual void handle() { }
virtual bool understand(const std::string & ) { return false; }
+
+ virtual unsigned int valuePosition(const std::string & value) {
+ return 0;
+ }
};
\ No newline at end of file