Skip to content

Commit e6c878c

Browse files
committed
warning and error fixes
1 parent 396e350 commit e6c878c

File tree

3 files changed

+39
-45
lines changed

3 files changed

+39
-45
lines changed

book/chapters/config.md

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,6 @@ These can be modified via setter functions
163163
* `ConfigBase *section(const std::string &sectionName)` : specify the section name to use to get the option values, only this section will be processed
164164
* `ConfigBase *index(uint16_t sectionIndex)` : specify an index section to use for processing if multiple TOML sections of the same name are present `[[section]]`
165165

166-
167166
For example, to specify reading a configure file that used `:` to separate name and values:
168167

169168
```cpp
@@ -198,21 +197,23 @@ app.config_formatter(std::make_shared<NewConfig>());
198197

199198
See [`examples/json.cpp`](https://github.com/CLIUtils/CLI11/blob/master/examples/json.cpp) for a complete JSON config example.
200199

201-
#### Trivial JSON configuration example
200+
### Trivial JSON configuration example
202201

203202
```JSON
204203
{
205-
"test": 56,
206-
"testb": "test",
207-
"flag": true
204+
"test": 56,
205+
"testb": "test",
206+
"flag": true
208207
}
209208
```
209+
210210
The parser can handle these structures with only a minor tweak
211+
211212
```cpp
212213
app.get_config_formatter_base()->valueSeparator(':');
213214
```
214-
The open and close brackets must be on a separate line and the comma gets interpreted as an array separator but since no values are after the comma they get ignored as well. This will not support multiple layers or sections or any other moderately complex JSON, but can work if the input file is simple.
215215
216+
The open and close brackets must be on a separate line and the comma gets interpreted as an array separator but since no values are after the comma they get ignored as well. This will not support multiple layers or sections or any other moderately complex JSON, but can work if the input file is simple.
216217
217218
## Triggering Subcommands
218219

include/CLI/Config.hpp

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ checkParentSegments(std::vector<ConfigItem> &output, const std::string &currentS
171171

172172
inline std::vector<ConfigItem> ConfigBase::from_config(std::istream &input) const {
173173
std::string line;
174-
std::string section = "default";
174+
std::string currentSection = "default";
175175
std::string previousSection = "default";
176176
std::vector<ConfigItem> output;
177177
bool isDefaultArray = (arrayStart == '[' && arrayEnd == ']' && arraySeparator == ',');
@@ -192,28 +192,28 @@ inline std::vector<ConfigItem> ConfigBase::from_config(std::istream &input) cons
192192
continue;
193193
}
194194
if(line.front() == '[' && line.back() == ']') {
195-
if(section != "default") {
195+
if(currentSection != "default") {
196196
// insert a section end which is just an empty items_buffer
197197
output.emplace_back();
198-
output.back().parents = detail::generate_parents(section, name, parentSeparatorChar);
198+
output.back().parents = detail::generate_parents(currentSection, name, parentSeparatorChar);
199199
output.back().name = "--";
200200
}
201-
section = line.substr(1, len - 2);
201+
currentSection = line.substr(1, len - 2);
202202
// deal with double brackets for TOML
203-
if(section.size() > 1 && section.front() == '[' && section.back() == ']') {
204-
section = section.substr(1, section.size() - 2);
203+
if(currentSection.size() > 1 && currentSection.front() == '[' && currentSection.back() == ']') {
204+
currentSection = currentSection.substr(1, currentSection.size() - 2);
205205
}
206-
if(detail::to_lower(section) == "default") {
207-
section = "default";
206+
if(detail::to_lower(currentSection) == "default") {
207+
currentSection = "default";
208208
} else {
209-
detail::checkParentSegments(output, section, parentSeparatorChar);
209+
detail::checkParentSegments(output, currentSection, parentSeparatorChar);
210210
}
211211
inSection = false;
212-
if(section == previousSection) {
212+
if(currentSection == previousSection) {
213213
++currentSectionIndex;
214214
} else {
215215
currentSectionIndex = 0;
216-
previousSection = section;
216+
previousSection = currentSection;
217217
}
218218
continue;
219219
}
@@ -264,7 +264,7 @@ inline std::vector<ConfigItem> ConfigBase::from_config(std::istream &input) cons
264264
detail::remove_quotes(it);
265265
}
266266

267-
std::vector<std::string> parents = detail::generate_parents(section, name, parentSeparatorChar);
267+
std::vector<std::string> parents = detail::generate_parents(currentSection, name, parentSeparatorChar);
268268
if(parents.size() > maximumLayers) {
269269
continue;
270270
}
@@ -287,11 +287,11 @@ inline std::vector<ConfigItem> ConfigBase::from_config(std::istream &input) cons
287287
output.back().inputs = std::move(items_buffer);
288288
}
289289
}
290-
if(section != "default") {
290+
if(currentSection != "default") {
291291
// insert a section end which is just an empty items_buffer
292292
std::string ename;
293293
output.emplace_back();
294-
output.back().parents = detail::generate_parents(section, ename, parentSeparatorChar);
294+
output.back().parents = detail::generate_parents(currentSection, ename, parentSeparatorChar);
295295
output.back().name = "--";
296296
while(output.back().parents.size() > 1) {
297297
output.push_back(output.back());

tests/ConfigFileTest.cpp

Lines changed: 18 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,6 @@ TEST_CASE("StringBased: Spaces", "[config]") {
208208
CHECK(output.at(1).inputs.at(0) == "four");
209209
}
210210

211-
212211
TEST_CASE("StringBased: Sections", "[config]") {
213212
std::stringstream ofile;
214213

@@ -799,7 +798,6 @@ TEST_CASE_METHOD(TApp, "IniRequired", "[config]") {
799798
CHECK_THROWS_AS(run(), CLI::RequiredError);
800799
}
801800

802-
803801
TEST_CASE_METHOD(TApp, "IniInlineComment", "[config]") {
804802

805803
TempFile tmpini{"TestIniTmp.ini"};
@@ -885,29 +883,27 @@ TEST_CASE_METHOD(TApp, "TomlInlineComment", "[config]") {
885883
CHECK_THROWS_AS(run(), CLI::RequiredError);
886884
}
887885

888-
889886
TEST_CASE_METHOD(TApp, "ConfigModifiers", "[config]") {
890887

891-
892888
app.set_config("--config", "test.ini", "", true);
893889

894-
auto cfgptr = app.get_config_formatter_base();
890+
auto cfgptr = app.get_config_formatter_base();
895891

896-
cfgptr->section("test");
897-
CHECK(cfgptr->section()=="test");
892+
cfgptr->section("test");
893+
CHECK(cfgptr->section() == "test");
898894

899-
CHECK(cfgptr->sectionRef() == "test");
900-
auto &sref = cfgptr->sectionRef();
901-
sref= "this";
902-
CHECK(cfgptr->section() == "this");
895+
CHECK(cfgptr->sectionRef() == "test");
896+
auto &sref = cfgptr->sectionRef();
897+
sref = "this";
898+
CHECK(cfgptr->section() == "this");
903899

904900
cfgptr->index(5);
905-
CHECK(cfgptr->index() == 5);
901+
CHECK(cfgptr->index() == 5);
906902

907-
CHECK(cfgptr->indexRef() == 5);
908-
auto &iref = cfgptr->indexRef();
909-
iref = 7;
910-
CHECK(cfgptr->index() == 7);
903+
CHECK(cfgptr->indexRef() == 5);
904+
auto &iref = cfgptr->indexRef();
905+
iref = 7;
906+
CHECK(cfgptr->index() == 7);
911907
}
912908

913909
TEST_CASE_METHOD(TApp, "IniVector", "[config]") {
@@ -1152,7 +1148,6 @@ TEST_CASE_METHOD(TApp, "IniLayeredDotSection", "[config]") {
11521148
app.get_config_formatter_base()->maxLayers(1);
11531149
run();
11541150
CHECK(three == 0);
1155-
11561151
}
11571152

11581153
TEST_CASE_METHOD(TApp, "IniLayeredCustomSectionSeparator", "[config]") {
@@ -1265,27 +1260,26 @@ TEST_CASE_METHOD(TApp, "IniSubcommandConfigurablePreParse", "[config]") {
12651260

12661261
TEST_CASE_METHOD(TApp, "IniSection", "[config]") {
12671262

1268-
TempFile tmpini{ "TestIniTmp.ini" };
1263+
TempFile tmpini{"TestIniTmp.ini"};
12691264

12701265
app.set_config("--config", tmpini);
12711266
app.get_config_formatter_base()->section("config");
12721267

12731268
{
1274-
std::ofstream out{ tmpini };
1269+
std::ofstream out{tmpini};
12751270
out << "[config]" << std::endl;
12761271
out << "val=2" << std::endl;
12771272
out << "subsubcom.val=3" << std::endl;
12781273
out << "[default]" << std::endl;
12791274
out << "val=1" << std::endl;
12801275
}
12811276

1282-
int val{ 0 };
1277+
int val{0};
12831278
app.add_option("--val", val);
12841279

12851280
run();
12861281

1287-
CHECK(2==val);
1288-
1282+
CHECK(2 == val);
12891283
}
12901284

12911285
TEST_CASE_METHOD(TApp, "IniSection2", "[config]") {
@@ -1372,23 +1366,22 @@ TEST_CASE_METHOD(TApp, "TomlSectionNumber", "[config]") {
13721366

13731367
CHECK(2 == val);
13741368

1375-
auto &index=app.get_config_formatter_base()->indexRef();
1369+
auto &index = app.get_config_formatter_base()->indexRef();
13761370
index = 1;
13771371
run();
13781372

13791373
CHECK(4 == val);
13801374

13811375
index = -1;
13821376
run();
1383-
//Take the first section in this case
1377+
// Take the first section in this case
13841378
CHECK(2 == val);
13851379
index = 2;
13861380
run();
13871381

13881382
CHECK(6 == val);
13891383
}
13901384

1391-
13921385
TEST_CASE_METHOD(TApp, "IniSubcommandConfigurableParseComplete", "[config]") {
13931386

13941387
TempFile tmpini{"TestIniTmp.ini"};

0 commit comments

Comments
 (0)