mirror of
https://github.com/CLIUtils/CLI11.git
synced 2025-05-01 13:13:53 +00:00
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>
This commit is contained in:
parent
ca9a128720
commit
71755e4f67
@ -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);
|
||||
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;
|
||||
|
@ -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<std::string>());
|
||||
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<std::string>());
|
||||
}
|
||||
|
||||
TEST_CASE_METHOD(TApp, "TomlOutputSimple", "[config]") {
|
||||
|
||||
int v{0};
|
||||
|
@ -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{};
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user