Commit 04be61b3a128d55745e0338d3348b48ee27d755d
1 parent
6836aaed
Adding corrected output for inifile
Showing
3 changed files
with
41 additions
and
2 deletions
include/CLI/App.hpp
| ... | ... | @@ -561,7 +561,7 @@ public: |
| 561 | 561 | |
| 562 | 562 | // If the option was found on command line |
| 563 | 563 | if(opt->count() > 0) |
| 564 | - out << name << "=" << detail::join(opt->results()) << std::endl; | |
| 564 | + out << name << "=" << detail::inijoin(opt->results()) << std::endl; | |
| 565 | 565 | |
| 566 | 566 | // If the option has a default and is requested by optional argument |
| 567 | 567 | else if(default_also && opt->defaultval_ != "") | ... | ... |
include/CLI/Ini.hpp
| ... | ... | @@ -14,6 +14,24 @@ |
| 14 | 14 | namespace CLI { |
| 15 | 15 | namespace detail { |
| 16 | 16 | |
| 17 | +inline std::string inijoin(std::vector<std::string> args) { | |
| 18 | + std::ostringstream s; | |
| 19 | + size_t start = 0; | |
| 20 | + for (const auto& arg : args) { | |
| 21 | + if(start++ > 0) | |
| 22 | + s << " "; | |
| 23 | + | |
| 24 | + auto it = std::find_if(arg.begin(), arg.end(), [](char ch){ return std::isspace<char>(ch , std::locale());}); | |
| 25 | + if(it == arg.end()) | |
| 26 | + s << arg; | |
| 27 | + else if(arg.find("\"") == std::string::npos) | |
| 28 | + s << "\"" << arg << "\""; | |
| 29 | + else | |
| 30 | + s << "\'" << arg << "\'"; | |
| 31 | + } | |
| 32 | + | |
| 33 | + return s.str(); | |
| 34 | +} | |
| 17 | 35 | |
| 18 | 36 | struct ini_ret_t { |
| 19 | 37 | /// This is the full name with dots | ... | ... |
tests/IniTest.cpp
| ... | ... | @@ -500,7 +500,7 @@ TEST_F(TApp, IniOutputVector) { |
| 500 | 500 | run(); |
| 501 | 501 | |
| 502 | 502 | std::string str = app.config_to_str(); |
| 503 | - EXPECT_EQ("vector=1,2,3\n", str); | |
| 503 | + EXPECT_EQ("vector=1 2 3\n", str); | |
| 504 | 504 | } |
| 505 | 505 | |
| 506 | 506 | TEST_F(TApp, IniOutputFlag) { |
| ... | ... | @@ -563,3 +563,24 @@ TEST_F(TApp, IniOutputSubcom) { |
| 563 | 563 | EXPECT_THAT(str, HasSubstr("simple=true")); |
| 564 | 564 | EXPECT_THAT(str, HasSubstr("other.newer=true")); |
| 565 | 565 | } |
| 566 | + | |
| 567 | +TEST_F(TApp, IniQuotedOutput) { | |
| 568 | + | |
| 569 | + std::string val1; | |
| 570 | + app.add_option("--val1", val1); | |
| 571 | + | |
| 572 | + std::string val2; | |
| 573 | + app.add_option("--val2", val2); | |
| 574 | + | |
| 575 | + args = {"--val1", "I am a string", "--val2", "I am a \"confusing\" string"}; | |
| 576 | + | |
| 577 | + run(); | |
| 578 | + | |
| 579 | + EXPECT_EQ("I am a string", val1); | |
| 580 | + EXPECT_EQ("I am a \"confusing\" string", val2); | |
| 581 | + | |
| 582 | + std::string str = app.config_to_str(); | |
| 583 | + EXPECT_THAT(str, HasSubstr("val1=\"I am a string\"")); | |
| 584 | + EXPECT_THAT(str, HasSubstr("val2='I am a \"confusing\" string'")); | |
| 585 | + | |
| 586 | +} | ... | ... |