Skip to content

Commit

Permalink
Fixes based on #30 by @infinity0n3
Browse files Browse the repository at this point in the history
  • Loading branch information
henryiii committed Sep 10, 2017
1 parent 726a970 commit 2f97837
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 12 deletions.
4 changes: 2 additions & 2 deletions .ci/check_style.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ git ls-files -- '*.cpp' '*.hpp' | xargs clang-format -i -style=file

git diff --exit-code --color

mkdir build || true
cd build
mkdir build-tidy || true
cd build-tidy
CXX_FLAGS="-Werror -Wall -Wextra -pedantic -std=c++11" cmake .. -DCLANG_TIDY_FIX=ON
cmake --build .

Expand Down
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
## Version 1.2 (in progress)


* Fixed Config file search if passed on command line [#30](https://github.com/CLIUtils/CLI11/issues/30)
* Added `CLI11_PARSE(app, argc, argv)` macro for simple parse commands (does not support returning arg)
* The name string can now contain spaces around commas [#29](https://github.com/CLIUtils/CLI11/pull/29)
* `set_default_str` now only sets string, and `set_default_val` will evaluate the default string given [#26](https://github.com/CLIUtils/CLI11/issues/26)
Expand Down
24 changes: 15 additions & 9 deletions include/CLI/App.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -899,17 +899,23 @@ class App {
}

// Process an INI file
if(config_ptr_ != nullptr && config_name_ != "") {
try {
std::vector<detail::ini_ret_t> values = detail::parse_ini(config_name_);
while(!values.empty()) {
if(!_parse_ini(values)) {
throw ExtrasINIError(values.back().fullname);
if(config_ptr_ != nullptr) {
if(*config_ptr_) {
config_ptr_->run_callback();
config_required_ = true;
}
if(config_name_ != "") {
try {
std::vector<detail::ini_ret_t> values = detail::parse_ini(config_name_);
while(!values.empty()) {
if(!_parse_ini(values)) {
throw ExtrasINIError(values.back().fullname);
}
}
} catch(const FileError &) {
if(config_required_)
throw;
}
} catch(const FileError &) {
if(config_required_)
throw;
}
}

Expand Down
48 changes: 48 additions & 0 deletions tests/IniTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,45 @@ TEST_F(TApp, IniNotRequired) {
EXPECT_EQ(3, three);
}

TEST_F(TApp, IniNotRequiredNotDefault) {

TempFile tmpini{"TestIniTmp.ini"};
TempFile tmpini2{"TestIniTmp2.ini"};

app.add_config("--config", tmpini);

{
std::ofstream out{tmpini};
out << "[default]" << std::endl;
out << "two=99" << std::endl;
out << "three=3" << std::endl;
}

{
std::ofstream out{tmpini2};
out << "[default]" << std::endl;
out << "two=98" << std::endl;
out << "three=4" << std::endl;
}

int one = 0, two = 0, three = 0;
app.add_option("--one", one);
app.add_option("--two", two);
app.add_option("--three", three);

run();

EXPECT_EQ(99, two);
EXPECT_EQ(3, three);

app.reset();
args = {"--config", tmpini2};
run();

EXPECT_EQ(98, two);
EXPECT_EQ(4, three);
}

TEST_F(TApp, IniRequiredNotFound) {

std::string noini = "TestIniNotExist.ini";
Expand All @@ -203,6 +242,15 @@ TEST_F(TApp, IniRequiredNotFound) {
EXPECT_THROW(run(), CLI::FileError);
}

TEST_F(TApp, IniNotRequiredPassedNotFound) {

std::string noini = "TestIniNotExist.ini";
app.add_config("--config", "", "", false);

args = {"--config", noini};
EXPECT_THROW(run(), CLI::FileError);
}

TEST_F(TApp, IniOverwrite) {

TempFile tmpini{"TestIniTmp.ini"};
Expand Down

0 comments on commit 2f97837

Please sign in to comment.