From 04be61b3a128d55745e0338d3348b48ee27d755d Mon Sep 17 00:00:00 2001 From: Henry Fredrick Schreiner Date: Thu, 23 Mar 2017 12:42:15 -0400 Subject: [PATCH] Adding corrected output for inifile --- include/CLI/App.hpp | 2 +- include/CLI/Ini.hpp | 18 ++++++++++++++++++ tests/IniTest.cpp | 23 ++++++++++++++++++++++- 3 files changed, 41 insertions(+), 2 deletions(-) diff --git a/include/CLI/App.hpp b/include/CLI/App.hpp index 6adcdee1..613aa717 100644 --- a/include/CLI/App.hpp +++ b/include/CLI/App.hpp @@ -561,7 +561,7 @@ public: // If the option was found on command line if(opt->count() > 0) - out << name << "=" << detail::join(opt->results()) << std::endl; + out << name << "=" << detail::inijoin(opt->results()) << std::endl; // If the option has a default and is requested by optional argument else if(default_also && opt->defaultval_ != "") diff --git a/include/CLI/Ini.hpp b/include/CLI/Ini.hpp index e7026e01..9b04cdad 100644 --- a/include/CLI/Ini.hpp +++ b/include/CLI/Ini.hpp @@ -14,6 +14,24 @@ namespace CLI { namespace detail { +inline std::string inijoin(std::vector args) { + std::ostringstream s; + size_t start = 0; + for (const auto& arg : args) { + if(start++ > 0) + s << " "; + + auto it = std::find_if(arg.begin(), arg.end(), [](char ch){ return std::isspace(ch , std::locale());}); + if(it == arg.end()) + s << arg; + else if(arg.find("\"") == std::string::npos) + s << "\"" << arg << "\""; + else + s << "\'" << arg << "\'"; + } + + return s.str(); +} struct ini_ret_t { /// This is the full name with dots diff --git a/tests/IniTest.cpp b/tests/IniTest.cpp index b1bc12f7..b054136c 100644 --- a/tests/IniTest.cpp +++ b/tests/IniTest.cpp @@ -500,7 +500,7 @@ TEST_F(TApp, IniOutputVector) { run(); std::string str = app.config_to_str(); - EXPECT_EQ("vector=1,2,3\n", str); + EXPECT_EQ("vector=1 2 3\n", str); } TEST_F(TApp, IniOutputFlag) { @@ -563,3 +563,24 @@ TEST_F(TApp, IniOutputSubcom) { EXPECT_THAT(str, HasSubstr("simple=true")); EXPECT_THAT(str, HasSubstr("other.newer=true")); } + +TEST_F(TApp, IniQuotedOutput) { + + std::string val1; + app.add_option("--val1", val1); + + std::string val2; + app.add_option("--val2", val2); + + args = {"--val1", "I am a string", "--val2", "I am a \"confusing\" string"}; + + run(); + + EXPECT_EQ("I am a string", val1); + EXPECT_EQ("I am a \"confusing\" string", val2); + + std::string str = app.config_to_str(); + EXPECT_THAT(str, HasSubstr("val1=\"I am a string\"")); + EXPECT_THAT(str, HasSubstr("val2='I am a \"confusing\" string'")); + +}