diff --git a/CHANGELOG.md b/CHANGELOG.md index beb1c553..9017a4fc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,13 +1,13 @@ ## Version 0.7 (in progress) * Allow comments in ini files (lines starting with `;`) -* Ini files support flags (only read) +* Ini files support flags +* Ini files support vectors * Ini files support subcommands (only read) -* Ini files support vectors (only read) * Added CodeCov code coverage reports * Lots of small bugfixes related to adding tests to increase coverage * Error handling now uses scoped enum in errors * Reparsing rules changed a little to accommodate Ini files. Callbacks are now called when parsing INI, and reset any time results are added. -* Adding extra utilities in seperate files version only, `Timer` (not needed for parsing, but useful for general CLI applications). +* Adding extra utilities in full version only, `Timer` (not needed for parsing, but useful for general CLI applications). ## Version 0.6 diff --git a/include/CLI/App.hpp b/include/CLI/App.hpp index 7d1862d1..36948bdf 100644 --- a/include/CLI/App.hpp +++ b/include/CLI/App.hpp @@ -547,12 +547,33 @@ public: /// @name Help ///@{ - /// Produce a string that could be read in as a config of the current values of the App - std::string config_to_str() const { + /// Produce a string that could be read in as a config of the current values of the App. Set default_also to include default arguments. + std::string config_to_str(bool default_also=false) const { std::stringstream out; for(const Option_p &opt : options_) { - if(opt->lnames_.size() > 0 && opt->count() > 0 && opt->get_expected() > 0) - out << opt->lnames_[0] << "=" << detail::join(opt->results()) << std::endl; + + // Only process option with a long-name + if(opt->lnames_.size() > 0) { + + // Non-flags + if(opt->get_expected() != 0) { + + // If the option was found on command line + if(opt->count() > 0) + out << opt->lnames_[0] << "=" << detail::join(opt->results()) << std::endl; + + // If the option has a default and is requested by optional argument + else if(default_also && opt->defaultval_ != "") + out << opt->lnames_[0] << "=" << opt->defaultval_ << std::endl; + // Flag, one passed + } else if(opt->count() == 1) { + out << opt->lnames_[0] << "=true" << std::endl; + + // Flag, multiple passed + } else if(opt->count() > 1) { + out << opt->lnames_[0] << "=" << opt->count() << std::endl; + } + } } return out.str(); } diff --git a/tests/IniTest.cpp b/tests/IniTest.cpp index 0bb22d92..3ed735fe 100644 --- a/tests/IniTest.cpp +++ b/tests/IniTest.cpp @@ -488,22 +488,64 @@ TEST_F(TApp, IniOutputSimple) { std::string str = app.config_to_str(); EXPECT_EQ("simple=3\n", str); - } -/// Flags should not show up in config file +TEST_F(TApp, IniOutputVector) { + + std::vector v; + app.add_option("--vector", v); + + args = {"--vector", "1", "2", "3"}; + + run(); + + std::string str = app.config_to_str(); + EXPECT_EQ("vector=1,2,3\n", str); +} + TEST_F(TApp, IniOutputFlag) { - int v; + int v, q; app.add_option("--simple", v); app.add_flag("--nothing"); + app.add_flag("--onething"); + app.add_flag("--something", q); - args = {"--simple=3", "--nothing"}; + args = {"--simple=3", "--onething", "--something", "--something"}; run(); std::string str = app.config_to_str(); EXPECT_THAT(str, HasSubstr("simple=3")); - EXPECT_THAT(str, Not(HasSubstr("nothing="))); - + EXPECT_THAT(str, Not(HasSubstr("nothing"))); + EXPECT_THAT(str, HasSubstr("onething=true")); + EXPECT_THAT(str, HasSubstr("something=2")); +} + +TEST_F(TApp, IniOutputSet) { + + int v; + app.add_set("--simple", v, {1,2,3}); + + args = {"--simple=2"}; + + run(); + + std::string str = app.config_to_str(); + EXPECT_THAT(str, HasSubstr("simple=2")); +} + + +TEST_F(TApp, IniOutputDefault) { + + int v=7; + app.add_option("--simple", v, "", true); + + run(); + + std::string str = app.config_to_str(); + EXPECT_THAT(str, Not(HasSubstr("simple=7"))); + + str = app.config_to_str(true); + EXPECT_THAT(str, HasSubstr("simple=7")); }