Commit 71755e4f679d26f6e7ef0af562eafbc7161e96de
Committed by
GitHub
1 parent
ca9a1287
tweak the parsing of files for flags with disable_flag_override (#800)
* tweak the parsing of files for flags with disable_flag_override, basically allow true as a valid value to be interpreted as the default in that case * style: pre-commit.ci fixes Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Showing
3 changed files
with
67 additions
and
1 deletions
include/CLI/impl/App_inl.hpp
| @@ -1396,7 +1396,22 @@ CLI11_INLINE bool App::_parse_single_config(const ConfigItem &item, std::size_t | @@ -1396,7 +1396,22 @@ CLI11_INLINE bool App::_parse_single_config(const ConfigItem &item, std::size_t | ||
| 1396 | if(item.inputs.size() <= 1) { | 1396 | if(item.inputs.size() <= 1) { |
| 1397 | // Flag parsing | 1397 | // Flag parsing |
| 1398 | auto res = config_formatter_->to_flag(item); | 1398 | auto res = config_formatter_->to_flag(item); |
| 1399 | - res = op->get_flag_value(item.name, res); | 1399 | + bool converted{false}; |
| 1400 | + if(op->get_disable_flag_override()) { | ||
| 1401 | + | ||
| 1402 | + try { | ||
| 1403 | + auto val = detail::to_flag_value(res); | ||
| 1404 | + if(val == 1) { | ||
| 1405 | + res = op->get_flag_value(item.name, "{}"); | ||
| 1406 | + converted = true; | ||
| 1407 | + } | ||
| 1408 | + } catch(...) { | ||
| 1409 | + } | ||
| 1410 | + } | ||
| 1411 | + | ||
| 1412 | + if(!converted) { | ||
| 1413 | + res = op->get_flag_value(item.name, res); | ||
| 1414 | + } | ||
| 1400 | 1415 | ||
| 1401 | op->add_result(res); | 1416 | op->add_result(res); |
| 1402 | return true; | 1417 | return true; |
tests/ConfigFileTest.cpp
| @@ -435,6 +435,8 @@ TEST_CASE("StringBased: file_error", "[config]") { | @@ -435,6 +435,8 @@ TEST_CASE("StringBased: file_error", "[config]") { | ||
| 435 | CHECK_THROWS_AS(CLI::ConfigINI().from_file("nonexist_file"), CLI::FileError); | 435 | CHECK_THROWS_AS(CLI::ConfigINI().from_file("nonexist_file"), CLI::FileError); |
| 436 | } | 436 | } |
| 437 | 437 | ||
| 438 | +static const int fclear1 = fileClear("TestIniTmp.ini"); | ||
| 439 | + | ||
| 438 | TEST_CASE_METHOD(TApp, "IniNotRequired", "[config]") { | 440 | TEST_CASE_METHOD(TApp, "IniNotRequired", "[config]") { |
| 439 | 441 | ||
| 440 | TempFile tmpini{"TestIniTmp.ini"}; | 442 | TempFile tmpini{"TestIniTmp.ini"}; |
| @@ -595,6 +597,8 @@ TEST_CASE_METHOD(TApp, "IniNotRequiredbadConfigurator", "[config]") { | @@ -595,6 +597,8 @@ TEST_CASE_METHOD(TApp, "IniNotRequiredbadConfigurator", "[config]") { | ||
| 595 | REQUIRE_NOTHROW(run()); | 597 | REQUIRE_NOTHROW(run()); |
| 596 | } | 598 | } |
| 597 | 599 | ||
| 600 | +static const int fclear2 = fileClear("TestIniTmp2.ini"); | ||
| 601 | + | ||
| 598 | TEST_CASE_METHOD(TApp, "IniNotRequiredNotDefault", "[config]") { | 602 | TEST_CASE_METHOD(TApp, "IniNotRequiredNotDefault", "[config]") { |
| 599 | 603 | ||
| 600 | TempFile tmpini{"TestIniTmp.ini"}; | 604 | TempFile tmpini{"TestIniTmp.ini"}; |
| @@ -2020,6 +2024,51 @@ TEST_CASE_METHOD(TApp, "IniFalseFlagsDefDisableOverrideSuccess", "[config]") { | @@ -2020,6 +2024,51 @@ TEST_CASE_METHOD(TApp, "IniFalseFlagsDefDisableOverrideSuccess", "[config]") { | ||
| 2020 | CHECK(val == 15); | 2024 | CHECK(val == 15); |
| 2021 | } | 2025 | } |
| 2022 | 2026 | ||
| 2027 | +static const int fclear3 = fileClear("TestIniTmp3.ini"); | ||
| 2028 | + | ||
| 2029 | +TEST_CASE_METHOD(TApp, "IniDisableFlagOverride", "[config]") { | ||
| 2030 | + | ||
| 2031 | + TempFile tmpini{"TestIniTmp.ini"}; | ||
| 2032 | + TempFile tmpini2{"TestIniTmp2.ini"}; | ||
| 2033 | + TempFile tmpini3{"TestIniTmp3.ini"}; | ||
| 2034 | + | ||
| 2035 | + app.set_config("--config", tmpini); | ||
| 2036 | + | ||
| 2037 | + { | ||
| 2038 | + std::ofstream out{tmpini}; | ||
| 2039 | + out << "[default]" << std::endl; | ||
| 2040 | + out << "two=2" << std::endl; | ||
| 2041 | + } | ||
| 2042 | + | ||
| 2043 | + { | ||
| 2044 | + std::ofstream out{tmpini2}; | ||
| 2045 | + out << "[default]" << std::endl; | ||
| 2046 | + out << "two=7" << std::endl; | ||
| 2047 | + } | ||
| 2048 | + | ||
| 2049 | + { | ||
| 2050 | + std::ofstream out{tmpini3}; | ||
| 2051 | + out << "[default]" << std::endl; | ||
| 2052 | + out << "three=true" << std::endl; | ||
| 2053 | + } | ||
| 2054 | + | ||
| 2055 | + int val{0}; | ||
| 2056 | + app.add_flag("--one{1},--two{2},--three{3}", val)->disable_flag_override(); | ||
| 2057 | + | ||
| 2058 | + run(); | ||
| 2059 | + CHECK(tmpini.c_str() == app["--config"]->as<std::string>()); | ||
| 2060 | + CHECK(val == 2); | ||
| 2061 | + | ||
| 2062 | + args = {"--config", tmpini2}; | ||
| 2063 | + CHECK_THROWS_AS(run(), CLI::ArgumentMismatch); | ||
| 2064 | + | ||
| 2065 | + args = {"--config", tmpini3}; | ||
| 2066 | + run(); | ||
| 2067 | + | ||
| 2068 | + CHECK(val == 3); | ||
| 2069 | + CHECK(tmpini3.c_str() == app.get_config_ptr()->as<std::string>()); | ||
| 2070 | +} | ||
| 2071 | + | ||
| 2023 | TEST_CASE_METHOD(TApp, "TomlOutputSimple", "[config]") { | 2072 | TEST_CASE_METHOD(TApp, "TomlOutputSimple", "[config]") { |
| 2024 | 2073 | ||
| 2025 | int v{0}; | 2074 | int v{0}; |
tests/app_helper.hpp