Commit 4e69e9794d4f8a91ef860c69bf046a1f484d7041
1 parent
15c6ee5f
Adding ini print and simple tests
Showing
3 changed files
with
46 additions
and
1 deletions
CHANGELOG.md
| ... | ... | @@ -2,7 +2,9 @@ |
| 2 | 2 | |
| 3 | 3 | * Updates to help print |
| 4 | 4 | * Removed `run`, please use `parse` unless you subclass and add it |
| 5 | -* Supports ini files mixed with command line | |
| 5 | +* Supports ini files mixed with command line, tested | |
| 6 | +* Added Range for further Plumbum compatibility | |
| 7 | +* Added function to print out ini file | |
| 6 | 8 | |
| 7 | 9 | ## Version 0.3 |
| 8 | 10 | ... | ... |
include/CLI/App.hpp
| ... | ... | @@ -100,6 +100,15 @@ public: |
| 100 | 100 | return ini_setting; |
| 101 | 101 | } |
| 102 | 102 | |
| 103 | + /// Produce a string that could be read in as a config of the current values of the App | |
| 104 | + std::string config_to_str() const { | |
| 105 | + std::stringstream out; | |
| 106 | + for(const Option_p &opt : options) { | |
| 107 | + if(opt->lnames.size() > 0 && opt->count() > 0) | |
| 108 | + out << opt->lnames[0] << "=" << detail::join(opt->flatten_results()) << std::endl; | |
| 109 | + } | |
| 110 | + return out.str(); | |
| 111 | + } | |
| 103 | 112 | |
| 104 | 113 | /// Add a subcommand. Like the constructor, you can override the help message addition by setting help=false |
| 105 | 114 | App* add_subcommand(std::string name, std::string description="", bool help=true) { | ... | ... |
tests/IniTest.cpp
| ... | ... | @@ -2,6 +2,9 @@ |
| 2 | 2 | |
| 3 | 3 | #include <cstdio> |
| 4 | 4 | #include <sstream> |
| 5 | +#include "gmock/gmock.h" | |
| 6 | + | |
| 7 | +using ::testing::HasSubstr; | |
| 5 | 8 | |
| 6 | 9 | TEST(StringBased, First) { |
| 7 | 10 | std::stringstream ofile; |
| ... | ... | @@ -134,3 +137,34 @@ TEST_F(TApp, IniRequired) { |
| 134 | 137 | EXPECT_THROW(run(), CLI::RequiredError); |
| 135 | 138 | |
| 136 | 139 | } |
| 140 | + | |
| 141 | +TEST_F(TApp, IniOutputSimple) { | |
| 142 | + | |
| 143 | + int v; | |
| 144 | + app.add_option("--simple", v); | |
| 145 | + | |
| 146 | + args = {"--simple=3"}; | |
| 147 | + | |
| 148 | + run(); | |
| 149 | + | |
| 150 | + std::string str = app.config_to_str(); | |
| 151 | + EXPECT_EQ("simple=3\n", str); | |
| 152 | + | |
| 153 | +} | |
| 154 | + | |
| 155 | + | |
| 156 | +TEST_F(TApp, IniOutputFlag) { | |
| 157 | + | |
| 158 | + int v; | |
| 159 | + app.add_option("--simple", v); | |
| 160 | + app.add_flag("--nothing"); | |
| 161 | + | |
| 162 | + args = {"--simple=3", "--nothing"}; | |
| 163 | + | |
| 164 | + run(); | |
| 165 | + | |
| 166 | + std::string str = app.config_to_str(); | |
| 167 | + EXPECT_THAT(str, HasSubstr("simple=3")); | |
| 168 | + EXPECT_THAT(str, HasSubstr("nothing=")); | |
| 169 | + | |
| 170 | +} | ... | ... |