Commit 97b1def525c42db2257359e2b6d8800e9376b94a

Authored by Jonas Nilsson
1 parent 67cd2e63

Added basic support for writing description to config string.

include/CLI/App.hpp
@@ -809,35 +809,46 @@ class App { @@ -809,35 +809,46 @@ class App {
809 809
810 /// Produce a string that could be read in as a config of the current values of the App. Set default_also to include 810 /// Produce a string that could be read in as a config of the current values of the App. Set default_also to include
811 /// default arguments. Prefix will add a string to the beginning of each option. 811 /// default arguments. Prefix will add a string to the beginning of each option.
812 - std::string config_to_str(bool default_also = false, std::string prefix = "") const { 812 + std::string config_to_str(bool default_also = false, std::string prefix = "", bool write_description = false) const {
813 std::stringstream out; 813 std::stringstream out;
814 for(const Option_p &opt : options_) { 814 for(const Option_p &opt : options_) {
815 815
816 // Only process option with a long-name and configurable 816 // Only process option with a long-name and configurable
817 if(!opt->lnames_.empty() && opt->get_configurable()) { 817 if(!opt->lnames_.empty() && opt->get_configurable()) {
818 std::string name = prefix + opt->lnames_[0]; 818 std::string name = prefix + opt->lnames_[0];
  819 + std::string value;
819 820
820 // Non-flags 821 // Non-flags
821 if(opt->get_expected() != 0) { 822 if(opt->get_expected() != 0) {
822 823
823 // If the option was found on command line 824 // If the option was found on command line
824 if(opt->count() > 0) 825 if(opt->count() > 0)
825 - out << name << "=" << detail::inijoin(opt->results()) << std::endl; 826 + value = detail::inijoin(opt->results());
826 827
827 // If the option has a default and is requested by optional argument 828 // If the option has a default and is requested by optional argument
828 else if(default_also && !opt->defaultval_.empty()) 829 else if(default_also && !opt->defaultval_.empty())
829 - out << name << "=" << opt->defaultval_ << std::endl; 830 + value = opt->defaultval_;
830 // Flag, one passed 831 // Flag, one passed
831 } else if(opt->count() == 1) { 832 } else if(opt->count() == 1) {
832 - out << name << "=true" << std::endl; 833 + value = "true";
833 834
834 // Flag, multiple passed 835 // Flag, multiple passed
835 } else if(opt->count() > 1) { 836 } else if(opt->count() > 1) {
836 - out << name << "=" << opt->count() << std::endl; 837 + value = std::to_string(opt->count());
837 838
838 // Flag, not present 839 // Flag, not present
839 } else if(opt->count() == 0 && default_also) { 840 } else if(opt->count() == 0 && default_also) {
840 - out << name << "=false" << std::endl; 841 + value = "false";
  842 + }
  843 +
  844 + if (value.size() != 0) {
  845 + if (write_description and opt->description_.size() != 0) {
  846 + if (out.tellp() != 0) {
  847 + out << std::endl;
  848 + }
  849 + out << "; " << opt->description_ << std::endl;
  850 + }
  851 + out << name << "=" << value << std::endl;
841 } 852 }
842 } 853 }
843 } 854 }
tests/IniTest.cpp
@@ -575,6 +575,31 @@ TEST_F(TApp, IniOutputNoConfigurable) { @@ -575,6 +575,31 @@ TEST_F(TApp, IniOutputNoConfigurable) {
575 EXPECT_EQ("simple=3\n", str); 575 EXPECT_EQ("simple=3\n", str);
576 } 576 }
577 577
  578 +TEST_F(TApp, IniOutputShortSingleDescription) {
  579 + std::string flag = "some_flag";
  580 + std::string description = "Some short description.";
  581 + app.add_flag("--" + flag, description);
  582 +
  583 + run();
  584 +
  585 + std::string str = app.config_to_str(true, "", true);
  586 + EXPECT_THAT(str, HasSubstr("; " + description + "\n" + flag + "=false\n"));
  587 +}
  588 +
  589 +TEST_F(TApp, IniOutputShortDoubleDescription) {
  590 + std::string flag1 = "flagnr1";
  591 + std::string flag2 = "flagnr2";
  592 + std::string description1 = "First description.";
  593 + std::string description2 = "Second description.";
  594 + app.add_flag("--" + flag1, description1);
  595 + app.add_flag("--" + flag2, description2);
  596 +
  597 + run();
  598 +
  599 + std::string str = app.config_to_str(true, "", true);
  600 + EXPECT_EQ(str, "; " + description1 + "\n" + flag1 + "=false\n\n; " + description2 + "\n" + flag2 + "=false\n");
  601 +}
  602 +
578 TEST_F(TApp, IniOutputVector) { 603 TEST_F(TApp, IniOutputVector) {
579 604
580 std::vector<int> v; 605 std::vector<int> v;