1
0
mirror of https://github.com/CLIUtils/CLI11.git synced 2025-04-30 12:43:52 +00:00

Adding corrected output for inifile

This commit is contained in:
Henry Fredrick Schreiner 2017-03-23 12:42:15 -04:00
parent 6836aaed59
commit 04be61b3a1
3 changed files with 41 additions and 2 deletions

View File

@ -561,7 +561,7 @@ public:
// If the option was found on command line // If the option was found on command line
if(opt->count() > 0) 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 // If the option has a default and is requested by optional argument
else if(default_also && opt->defaultval_ != "") else if(default_also && opt->defaultval_ != "")

View File

@ -14,6 +14,24 @@
namespace CLI { namespace CLI {
namespace detail { namespace detail {
inline std::string inijoin(std::vector<std::string> 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<char>(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 { struct ini_ret_t {
/// This is the full name with dots /// This is the full name with dots

View File

@ -500,7 +500,7 @@ TEST_F(TApp, IniOutputVector) {
run(); run();
std::string str = app.config_to_str(); 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) { TEST_F(TApp, IniOutputFlag) {
@ -563,3 +563,24 @@ TEST_F(TApp, IniOutputSubcom) {
EXPECT_THAT(str, HasSubstr("simple=true")); EXPECT_THAT(str, HasSubstr("simple=true"));
EXPECT_THAT(str, HasSubstr("other.newer=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'"));
}