From c6ddbeb2814ac0eb0331f3e7c10f7b49af91d1c7 Mon Sep 17 00:00:00 2001 From: Henry Fredrick Schreiner Date: Tue, 5 Sep 2017 17:38:02 -0400 Subject: [PATCH] Fix for spaces in names issue --- include/CLI/Split.hpp | 4 ++-- tests/AppTest.cpp | 1 + tests/CreationTest.cpp | 21 +++++++++++++++++++++ tests/HelpersTest.cpp | 3 +++ 4 files changed, 27 insertions(+), 2 deletions(-) diff --git a/include/CLI/Split.hpp b/include/CLI/Split.hpp index f7fb5abd..f2f3ba10 100644 --- a/include/CLI/Split.hpp +++ b/include/CLI/Split.hpp @@ -44,10 +44,10 @@ inline std::vector split_names(std::string current) { std::vector output; size_t val; while((val = current.find(",")) != std::string::npos) { - output.push_back(current.substr(0, val)); + output.push_back(trim_copy(current.substr(0, val))); current = current.substr(val + 1); } - output.push_back(current); + output.push_back(trim_copy(current)); return output; } diff --git a/tests/AppTest.cpp b/tests/AppTest.cpp index 2ddb4be7..c535f943 100644 --- a/tests/AppTest.cpp +++ b/tests/AppTest.cpp @@ -260,6 +260,7 @@ TEST_F(TApp, Positionals) { EXPECT_EQ("thing2", posit2); } + TEST_F(TApp, ForcedPositional) { std::vector posit; auto one = app.add_flag("--one"); diff --git a/tests/CreationTest.cpp b/tests/CreationTest.cpp index c7f391c6..8f1a7337 100644 --- a/tests/CreationTest.cpp +++ b/tests/CreationTest.cpp @@ -230,3 +230,24 @@ TEST_F(TApp, CheckNameNoCase) { EXPECT_TRUE(pos2->check_name("pOs2")); EXPECT_TRUE(pos2->check_name("pos2")); } + + +TEST_F(TApp, PreSpaces) { + int x; + auto myapp = app.add_option(" -a, --long, other", x); + + EXPECT_TRUE(myapp->check_lname("long")); + EXPECT_TRUE(myapp->check_sname("a")); + EXPECT_TRUE(myapp->check_name("other")); + +} + +TEST_F(TApp, AllSpaces) { + int x; + auto myapp = app.add_option(" -a , --long , other ", x); + + EXPECT_TRUE(myapp->check_lname("long")); + EXPECT_TRUE(myapp->check_sname("a")); + EXPECT_TRUE(myapp->check_name("other")); + +} diff --git a/tests/HelpersTest.cpp b/tests/HelpersTest.cpp index 59175f5d..a94583ac 100644 --- a/tests/HelpersTest.cpp +++ b/tests/HelpersTest.cpp @@ -179,6 +179,9 @@ TEST(Split, StringList) { std::vector results{"a", "long", "--lone", "-q"}; EXPECT_EQ(results, CLI::detail::split_names("a,long,--lone,-q")); + EXPECT_EQ(results, CLI::detail::split_names(" a, long, --lone, -q")); + EXPECT_EQ(results, CLI::detail::split_names(" a , long , --lone , -q ")); + EXPECT_EQ(results, CLI::detail::split_names(" a , long , --lone , -q ")); EXPECT_EQ(std::vector({"one"}), CLI::detail::split_names("one")); }