diff --git a/CHANGELOG.md b/CHANGELOG.md index 016e5409..953cdcc5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,7 +2,9 @@ * Updates to help print * Removed `run`, please use `parse` unless you subclass and add it -* Supports ini files mixed with command line +* Supports ini files mixed with command line, tested +* Added Range for further Plumbum compatibility +* Added function to print out ini file ## Version 0.3 diff --git a/include/CLI/App.hpp b/include/CLI/App.hpp index 7c91e100..a4f7e5f2 100644 --- a/include/CLI/App.hpp +++ b/include/CLI/App.hpp @@ -100,6 +100,15 @@ public: return ini_setting; } + /// Produce a string that could be read in as a config of the current values of the App + std::string config_to_str() const { + std::stringstream out; + for(const Option_p &opt : options) { + if(opt->lnames.size() > 0 && opt->count() > 0) + out << opt->lnames[0] << "=" << detail::join(opt->flatten_results()) << std::endl; + } + return out.str(); + } /// Add a subcommand. Like the constructor, you can override the help message addition by setting help=false App* add_subcommand(std::string name, std::string description="", bool help=true) { diff --git a/tests/IniTest.cpp b/tests/IniTest.cpp index 29cacd0f..d7de9e17 100644 --- a/tests/IniTest.cpp +++ b/tests/IniTest.cpp @@ -2,6 +2,9 @@ #include #include +#include "gmock/gmock.h" + +using ::testing::HasSubstr; TEST(StringBased, First) { std::stringstream ofile; @@ -134,3 +137,34 @@ TEST_F(TApp, IniRequired) { EXPECT_THROW(run(), CLI::RequiredError); } + +TEST_F(TApp, IniOutputSimple) { + + int v; + app.add_option("--simple", v); + + args = {"--simple=3"}; + + run(); + + std::string str = app.config_to_str(); + EXPECT_EQ("simple=3\n", str); + +} + + +TEST_F(TApp, IniOutputFlag) { + + int v; + app.add_option("--simple", v); + app.add_flag("--nothing"); + + args = {"--simple=3", "--nothing"}; + + run(); + + std::string str = app.config_to_str(); + EXPECT_THAT(str, HasSubstr("simple=3")); + EXPECT_THAT(str, HasSubstr("nothing=")); + +}