mirror of
https://github.com/CLIUtils/CLI11.git
synced 2025-04-30 04:33:53 +00:00
Change default delimiter to '\0' instead of space (#221)
This maintains the CLI11 previous way of working. Signed-off-by: Rafi Wiener <rafiw@mellanox.com>
This commit is contained in:
parent
048f968504
commit
59ae97d064
@ -418,17 +418,22 @@ class App {
|
|||||||
Option *add_option(std::string option_name,
|
Option *add_option(std::string option_name,
|
||||||
std::vector<T> &variable, ///< The variable vector to set
|
std::vector<T> &variable, ///< The variable vector to set
|
||||||
std::string description = "",
|
std::string description = "",
|
||||||
char delimiter = ' ') {
|
char delimiter = '\0') {
|
||||||
|
|
||||||
CLI::callback_t fun = [&variable, delimiter](CLI::results_t res) {
|
CLI::callback_t fun = [&variable, delimiter](CLI::results_t res) {
|
||||||
bool retval = true;
|
bool retval = true;
|
||||||
variable.clear();
|
variable.clear();
|
||||||
for(const auto &elem : res) {
|
for(const auto &elem : res) {
|
||||||
for(const auto &var : CLI::detail::split(elem, delimiter)) {
|
if(delimiter != '\0') {
|
||||||
if(!var.empty()) {
|
for(const auto &var : CLI::detail::split(elem, delimiter)) {
|
||||||
variable.emplace_back();
|
if(!var.empty()) {
|
||||||
retval &= detail::lexical_cast(var, variable.back());
|
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;
|
return (!variable.empty()) && retval;
|
||||||
@ -445,17 +450,22 @@ class App {
|
|||||||
std::vector<T> &variable, ///< The variable vector to set
|
std::vector<T> &variable, ///< The variable vector to set
|
||||||
std::string description,
|
std::string description,
|
||||||
bool defaulted,
|
bool defaulted,
|
||||||
char delimiter = ' ') {
|
char delimiter = '\0') {
|
||||||
|
|
||||||
CLI::callback_t fun = [&variable, delimiter](CLI::results_t res) {
|
CLI::callback_t fun = [&variable, delimiter](CLI::results_t res) {
|
||||||
bool retval = true;
|
bool retval = true;
|
||||||
variable.clear();
|
variable.clear();
|
||||||
for(const auto &a : res) {
|
for(const auto &elem : res) {
|
||||||
for(const auto &var : CLI::detail::split(a, delimiter)) {
|
if(delimiter != '\0') {
|
||||||
if(!var.empty()) {
|
for(const auto &var : CLI::detail::split(elem, delimiter)) {
|
||||||
variable.emplace_back();
|
if(!var.empty()) {
|
||||||
retval &= detail::lexical_cast(var, variable.back());
|
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;
|
return (!variable.empty()) && retval;
|
||||||
|
@ -1981,15 +1981,15 @@ TEST_F(TApp, CustomUserSepParse) {
|
|||||||
// #209
|
// #209
|
||||||
TEST_F(TApp, DefaultUserSepParse) {
|
TEST_F(TApp, DefaultUserSepParse) {
|
||||||
|
|
||||||
std::vector<int> vals = {1, 2, 3};
|
std::vector<std::string> vals;
|
||||||
args = {"--idx", "1 2 3"};
|
args = {"--idx", "1 2 3", "4 5 6"};
|
||||||
auto opt = app.add_option("--idx", vals, "");
|
auto opt = app.add_option("--idx", vals, "");
|
||||||
run();
|
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.remove_option(opt);
|
||||||
app.add_option("--idx", vals, "", true);
|
app.add_option("--idx", vals, "", true);
|
||||||
run();
|
run();
|
||||||
EXPECT_EQ(vals, std::vector<int>({1, 2, 3}));
|
EXPECT_EQ(vals, std::vector<std::string>({"1 2 3", "4 5 6"}));
|
||||||
}
|
}
|
||||||
|
|
||||||
// #209
|
// #209
|
||||||
@ -2052,3 +2052,19 @@ TEST_F(TApp, CustomUserSepParse4) {
|
|||||||
run();
|
run();
|
||||||
EXPECT_EQ(vals, std::vector<int>({1, 2}));
|
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"}));
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user