diff --git a/include/CLI/App.hpp b/include/CLI/App.hpp index b78aab901..6ec49c93f 100644 --- a/include/CLI/App.hpp +++ b/include/CLI/App.hpp @@ -418,17 +418,22 @@ class App { Option *add_option(std::string option_name, std::vector &variable, ///< The variable vector to set std::string description = "", - char delimiter = ' ') { + char delimiter = '\0') { CLI::callback_t fun = [&variable, delimiter](CLI::results_t res) { bool retval = true; variable.clear(); for(const auto &elem : res) { - for(const auto &var : CLI::detail::split(elem, delimiter)) { - if(!var.empty()) { - variable.emplace_back(); - retval &= detail::lexical_cast(var, variable.back()); + if(delimiter != '\0') { + for(const auto &var : CLI::detail::split(elem, delimiter)) { + if(!var.empty()) { + variable.emplace_back(); + retval &= detail::lexical_cast(var, variable.back()); + } } + } else { + variable.emplace_back(); + retval &= detail::lexical_cast(elem, variable.back()); } } return (!variable.empty()) && retval; @@ -445,17 +450,22 @@ class App { std::vector &variable, ///< The variable vector to set std::string description, bool defaulted, - char delimiter = ' ') { + char delimiter = '\0') { CLI::callback_t fun = [&variable, delimiter](CLI::results_t res) { bool retval = true; variable.clear(); - for(const auto &a : res) { - for(const auto &var : CLI::detail::split(a, delimiter)) { - if(!var.empty()) { - variable.emplace_back(); - retval &= detail::lexical_cast(var, variable.back()); + for(const auto &elem : res) { + if(delimiter != '\0') { + for(const auto &var : CLI::detail::split(elem, delimiter)) { + if(!var.empty()) { + variable.emplace_back(); + retval &= detail::lexical_cast(var, variable.back()); + } } + } else { + variable.emplace_back(); + retval &= detail::lexical_cast(elem, variable.back()); } } return (!variable.empty()) && retval; diff --git a/tests/AppTest.cpp b/tests/AppTest.cpp index e8b52dec9..170775bad 100644 --- a/tests/AppTest.cpp +++ b/tests/AppTest.cpp @@ -1981,15 +1981,15 @@ TEST_F(TApp, CustomUserSepParse) { // #209 TEST_F(TApp, DefaultUserSepParse) { - std::vector vals = {1, 2, 3}; - args = {"--idx", "1 2 3"}; + std::vector vals; + args = {"--idx", "1 2 3", "4 5 6"}; auto opt = app.add_option("--idx", vals, ""); run(); - EXPECT_EQ(vals, std::vector({1, 2, 3})); + EXPECT_EQ(vals, std::vector({"1 2 3", "4 5 6"})); app.remove_option(opt); app.add_option("--idx", vals, "", true); run(); - EXPECT_EQ(vals, std::vector({1, 2, 3})); + EXPECT_EQ(vals, std::vector({"1 2 3", "4 5 6"})); } // #209 @@ -2052,3 +2052,19 @@ TEST_F(TApp, CustomUserSepParse4) { run(); EXPECT_EQ(vals, std::vector({1, 2})); } + +// #218 +TEST_F(TApp, CustomUserSepParse5) { + + std::vector bar; + args = {"this", "is", "a", "test"}; + auto opt = app.add_option("bar", bar, "bar"); + run(); + EXPECT_EQ(bar, std::vector({"this", "is", "a", "test"})); + + app.remove_option(opt); + args = {"this", "is", "a", "test"}; + app.add_option("bar", bar, "bar", true); + run(); + EXPECT_EQ(bar, std::vector({"this", "is", "a", "test"})); +}