mirror of
https://github.com/CLIUtils/CLI11.git
synced 2025-05-03 05:53:52 +00:00
Add quotes to values containing spaces (#198)
* Add test case for INI output of defaulted options * Add quotes to values with spaces * Fixing formatting * Only fix quote strings, not vectors
This commit is contained in:
parent
bd67a953b2
commit
9cfa0f44a0
@ -54,7 +54,12 @@ ConfigINI::to_config(const App *app, bool default_also, bool write_description,
|
|||||||
}
|
}
|
||||||
out << "; " << detail::fix_newlines("; ", opt->get_description()) << std::endl;
|
out << "; " << detail::fix_newlines("; ", opt->get_description()) << std::endl;
|
||||||
}
|
}
|
||||||
out << name << "=" << value << std::endl;
|
|
||||||
|
// Don't try to quote anything that is not size 1
|
||||||
|
if(opt->get_items_expected() != 1)
|
||||||
|
out << name << "=" << value << std::endl;
|
||||||
|
else
|
||||||
|
out << name << "=" << detail::add_quotes_if_needed(value) << std::endl;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -250,5 +250,17 @@ inline size_t escape_detect(std::string &str, size_t offset) {
|
|||||||
return offset + 1;
|
return offset + 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Add quotes if the string contains spaces
|
||||||
|
inline std::string &add_quotes_if_needed(std::string &str) {
|
||||||
|
if((str.front() != '"' && str.front() != '\'') || str.front() != str.back()) {
|
||||||
|
char quote = str.find('"') < str.find('\'') ? '\'' : '"';
|
||||||
|
if(str.find(' ') != std::string::npos) {
|
||||||
|
str.insert(0, 1, quote);
|
||||||
|
str.append(1, quote);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return str;
|
||||||
|
}
|
||||||
|
|
||||||
} // namespace detail
|
} // namespace detail
|
||||||
} // namespace CLI
|
} // namespace CLI
|
||||||
|
@ -768,3 +768,18 @@ TEST_F(TApp, IniQuotedOutput) {
|
|||||||
EXPECT_THAT(str, HasSubstr("val1=\"I am a string\""));
|
EXPECT_THAT(str, HasSubstr("val1=\"I am a string\""));
|
||||||
EXPECT_THAT(str, HasSubstr("val2='I am a \"confusing\" string'"));
|
EXPECT_THAT(str, HasSubstr("val2='I am a \"confusing\" string'"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TEST_F(TApp, DefaultsIniQuotedOutput) {
|
||||||
|
|
||||||
|
std::string val1{"I am a string"};
|
||||||
|
app.add_option("--val1", val1, "", true);
|
||||||
|
|
||||||
|
std::string val2{R"(I am a "confusing" string)"};
|
||||||
|
app.add_option("--val2", val2, "", true);
|
||||||
|
|
||||||
|
run();
|
||||||
|
|
||||||
|
std::string str = app.config_to_str(true);
|
||||||
|
EXPECT_THAT(str, HasSubstr("val1=\"I am a string\""));
|
||||||
|
EXPECT_THAT(str, HasSubstr("val2='I am a \"confusing\" string'"));
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user