Commit 78ed99568552f49e47a43920f55fdfab1e4ee1ad

Authored by Henry Fredrick Schreiner
1 parent b10b9c82

Add multiline ini comments

include/CLI/App.hpp
@@ -847,7 +847,7 @@ class App { @@ -847,7 +847,7 @@ class App {
847 if(out.tellp() != 0) { 847 if(out.tellp() != 0) {
848 out << std::endl; 848 out << std::endl;
849 } 849 }
850 - out << "; " << opt->get_description() << std::endl; 850 + out << "; " << detail::fix_newlines("; ", opt->get_description()) << std::endl;
851 } 851 }
852 out << name << "=" << value << std::endl; 852 out << name << "=" << value << std::endl;
853 } 853 }
include/CLI/StringTools.hpp
@@ -187,5 +187,21 @@ inline std::vector&lt;std::string&gt; split_up(std::string str) { @@ -187,5 +187,21 @@ inline std::vector&lt;std::string&gt; split_up(std::string str) {
187 return output; 187 return output;
188 } 188 }
189 189
  190 +/// Add a leader to the beginning of all new lines (nothing is added
  191 +/// at the start of the first line). `"; "` would be for ini files
  192 +///
  193 +/// Can't use Regex, or this would be a subs.
  194 +inline std::string fix_newlines(std::string leader, std::string input) {
  195 + std::string::size_type n = 0;
  196 + while(n != std::string::npos && n < input.size()) {
  197 + n = input.find('\n', n);
  198 + if(n != std::string::npos) {
  199 + input = input.substr(0, n + 1) + leader + input.substr(n + 1);
  200 + n += leader.size();
  201 + }
  202 + }
  203 + return input;
  204 +}
  205 +
190 } // namespace detail 206 } // namespace detail
191 } // namespace CLI 207 } // namespace CLI
tests/HelpersTest.cpp
@@ -359,3 +359,17 @@ TEST(Types, LexicalCastString) { @@ -359,3 +359,17 @@ TEST(Types, LexicalCastString) {
359 CLI::detail::lexical_cast(input, output); 359 CLI::detail::lexical_cast(input, output);
360 EXPECT_EQ(input, output); 360 EXPECT_EQ(input, output);
361 } 361 }
  362 +
  363 +TEST(FixNewLines, BasicCheck) {
  364 + std::string input = "one\ntwo";
  365 + std::string output = "one\n; two";
  366 + std::string result = CLI::detail::fix_newlines("; ", input);
  367 + EXPECT_EQ(result, output);
  368 +}
  369 +
  370 +TEST(FixNewLines, EdgesCheck) {
  371 + std::string input = "\none\ntwo\n";
  372 + std::string output = "\n; one\n; two\n; ";
  373 + std::string result = CLI::detail::fix_newlines("; ", input);
  374 + EXPECT_EQ(result, output);
  375 +}
tests/IniTest.cpp
@@ -600,6 +600,19 @@ TEST_F(TApp, IniOutputShortDoubleDescription) { @@ -600,6 +600,19 @@ TEST_F(TApp, IniOutputShortDoubleDescription) {
600 EXPECT_EQ(str, "; " + description1 + "\n" + flag1 + "=false\n\n; " + description2 + "\n" + flag2 + "=false\n"); 600 EXPECT_EQ(str, "; " + description1 + "\n" + flag1 + "=false\n\n; " + description2 + "\n" + flag2 + "=false\n");
601 } 601 }
602 602
  603 +TEST_F(TApp, IniOutputMultiLineDescription) {
  604 + std::string flag = "some_flag";
  605 + std::string description = "Some short description.\nThat has lines.";
  606 + app.add_flag("--" + flag, description);
  607 +
  608 + run();
  609 +
  610 + std::string str = app.config_to_str(true, "", true);
  611 + EXPECT_THAT(str, HasSubstr("; Some short description.\n"));
  612 + EXPECT_THAT(str, HasSubstr("; That has lines.\n"));
  613 + EXPECT_THAT(str, HasSubstr(flag + "=false\n"));
  614 +}
  615 +
603 TEST_F(TApp, IniOutputVector) { 616 TEST_F(TApp, IniOutputVector) {
604 617
605 std::vector<int> v; 618 std::vector<int> v;