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:
parent
6836aaed59
commit
04be61b3a1
@ -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_ != "")
|
||||
|
@ -14,6 +14,24 @@
|
||||
namespace CLI {
|
||||
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 {
|
||||
/// This is the full name with dots
|
||||
|
@ -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'"));
|
||||
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user