diff --git a/include/CLI/impl/App_inl.hpp b/include/CLI/impl/App_inl.hpp index 4d8b9445..101f20cd 100644 --- a/include/CLI/impl/App_inl.hpp +++ b/include/CLI/impl/App_inl.hpp @@ -1396,7 +1396,22 @@ CLI11_INLINE bool App::_parse_single_config(const ConfigItem &item, std::size_t if(item.inputs.size() <= 1) { // Flag parsing auto res = config_formatter_->to_flag(item); - res = op->get_flag_value(item.name, res); + bool converted{false}; + if(op->get_disable_flag_override()) { + + try { + auto val = detail::to_flag_value(res); + if(val == 1) { + res = op->get_flag_value(item.name, "{}"); + converted = true; + } + } catch(...) { + } + } + + if(!converted) { + res = op->get_flag_value(item.name, res); + } op->add_result(res); return true; diff --git a/tests/ConfigFileTest.cpp b/tests/ConfigFileTest.cpp index cde0a7aa..3aa92f99 100644 --- a/tests/ConfigFileTest.cpp +++ b/tests/ConfigFileTest.cpp @@ -435,6 +435,8 @@ TEST_CASE("StringBased: file_error", "[config]") { CHECK_THROWS_AS(CLI::ConfigINI().from_file("nonexist_file"), CLI::FileError); } +static const int fclear1 = fileClear("TestIniTmp.ini"); + TEST_CASE_METHOD(TApp, "IniNotRequired", "[config]") { TempFile tmpini{"TestIniTmp.ini"}; @@ -595,6 +597,8 @@ TEST_CASE_METHOD(TApp, "IniNotRequiredbadConfigurator", "[config]") { REQUIRE_NOTHROW(run()); } +static const int fclear2 = fileClear("TestIniTmp2.ini"); + TEST_CASE_METHOD(TApp, "IniNotRequiredNotDefault", "[config]") { TempFile tmpini{"TestIniTmp.ini"}; @@ -2020,6 +2024,51 @@ TEST_CASE_METHOD(TApp, "IniFalseFlagsDefDisableOverrideSuccess", "[config]") { CHECK(val == 15); } +static const int fclear3 = fileClear("TestIniTmp3.ini"); + +TEST_CASE_METHOD(TApp, "IniDisableFlagOverride", "[config]") { + + TempFile tmpini{"TestIniTmp.ini"}; + TempFile tmpini2{"TestIniTmp2.ini"}; + TempFile tmpini3{"TestIniTmp3.ini"}; + + app.set_config("--config", tmpini); + + { + std::ofstream out{tmpini}; + out << "[default]" << std::endl; + out << "two=2" << std::endl; + } + + { + std::ofstream out{tmpini2}; + out << "[default]" << std::endl; + out << "two=7" << std::endl; + } + + { + std::ofstream out{tmpini3}; + out << "[default]" << std::endl; + out << "three=true" << std::endl; + } + + int val{0}; + app.add_flag("--one{1},--two{2},--three{3}", val)->disable_flag_override(); + + run(); + CHECK(tmpini.c_str() == app["--config"]->as()); + CHECK(val == 2); + + args = {"--config", tmpini2}; + CHECK_THROWS_AS(run(), CLI::ArgumentMismatch); + + args = {"--config", tmpini3}; + run(); + + CHECK(val == 3); + CHECK(tmpini3.c_str() == app.get_config_ptr()->as()); +} + TEST_CASE_METHOD(TApp, "TomlOutputSimple", "[config]") { int v{0}; diff --git a/tests/app_helper.hpp b/tests/app_helper.hpp index f8d7e722..db98b29a 100644 --- a/tests/app_helper.hpp +++ b/tests/app_helper.hpp @@ -33,6 +33,8 @@ class TApp { } }; +CLI11_INLINE int fileClear(const std::string &name) { return std::remove(name.c_str()); } + class TempFile { std::string _name{};