fix the issue with default empty vectors behaviour. update unit test (#460)

This commit is contained in:
Victor Proon
2025-07-18 00:09:03 +03:00
committed by GitHub
parent 9431d03437
commit 5e6d1e29f7
2 changed files with 37 additions and 6 deletions

View File

@@ -1100,22 +1100,19 @@ void parse_value(const std::string& text, char& c)
c = text[0];
}
template<typename T> void add_value(const std::string& text, std::vector<T>& value);
template <typename T>
void
parse_value(const std::string& text, std::vector<T>& value)
{
if (text.empty()) {
T v;
parse_value(text, v);
value.emplace_back(std::move(v));
return;
}
std::stringstream in(text);
std::string token;
while(!in.eof() && std::getline(in, token, CXXOPTS_VECTOR_DELIMITER)) {
T v;
parse_value(token, v);
value.emplace_back(std::move(v));
add_value(token, value);
}
}

View File

@@ -709,6 +709,40 @@ TEST_CASE("std::vector", "[vector]") {
CHECK(vector[3] == 4.5);
}
TEST_CASE("empty std::vector", "[vector]") {
std::vector<double> double_vector;
std::vector<std::string> string_vector;
cxxopts::Options options("empty vector", " - tests behaviour of empty vector options");
SECTION("string vector") {
options.add_options()
("string_vector", "vector of strings", cxxopts::value(string_vector)->default_value(""));
Argv av({"empty vector"});
auto** argv = av.argv();
auto argc = av.argc();
options.parse(argc, argv);
CHECK(string_vector.empty());
}
SECTION("double vector") {
options.add_options()
("double_vector", "vector of doubles", cxxopts::value(double_vector)->default_value(""));
Argv av({"empty vector"});
auto** argv = av.argv();
auto argc = av.argc();
options.parse(argc, argv); // throws
CHECK(double_vector.empty());
}
}
#ifdef CXXOPTS_HAS_OPTIONAL
TEST_CASE("std::optional", "[optional]") {
std::optional<std::string> optional;