Skip to content

Commit

Permalink
Change default delimiter to '\0' instead of space (#221)
Browse files Browse the repository at this point in the history
This maintains the CLI11 previous way of working.

Signed-off-by: Rafi Wiener <rafiw@mellanox.com>
  • Loading branch information
rafiw authored and henryiii committed Feb 8, 2019
1 parent 048f968 commit 59ae97d
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 15 deletions.
32 changes: 21 additions & 11 deletions include/CLI/App.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -418,17 +418,22 @@ class App {
Option *add_option(std::string option_name,
std::vector<T> &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;
Expand All @@ -445,17 +450,22 @@ class App {
std::vector<T> &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;
Expand Down
24 changes: 20 additions & 4 deletions tests/AppTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1981,15 +1981,15 @@ TEST_F(TApp, CustomUserSepParse) {
// #209
TEST_F(TApp, DefaultUserSepParse) {

std::vector<int> vals = {1, 2, 3};
args = {"--idx", "1 2 3"};
std::vector<std::string> vals;
args = {"--idx", "1 2 3", "4 5 6"};
auto opt = app.add_option("--idx", vals, "");
run();
EXPECT_EQ(vals, std::vector<int>({1, 2, 3}));
EXPECT_EQ(vals, std::vector<std::string>({"1 2 3", "4 5 6"}));
app.remove_option(opt);
app.add_option("--idx", vals, "", true);
run();
EXPECT_EQ(vals, std::vector<int>({1, 2, 3}));
EXPECT_EQ(vals, std::vector<std::string>({"1 2 3", "4 5 6"}));
}

// #209
Expand Down Expand Up @@ -2052,3 +2052,19 @@ TEST_F(TApp, CustomUserSepParse4) {
run();
EXPECT_EQ(vals, std::vector<int>({1, 2}));
}

// #218
TEST_F(TApp, CustomUserSepParse5) {

std::vector<std::string> bar;
args = {"this", "is", "a", "test"};
auto opt = app.add_option("bar", bar, "bar");
run();
EXPECT_EQ(bar, std::vector<std::string>({"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<std::string>({"this", "is", "a", "test"}));
}

0 comments on commit 59ae97d

Please sign in to comment.