Skip to content

Commit 5239229

Browse files
authored
preset: handle negated arg, reverse the meaning if needed (#18041)
1 parent 5c8a717 commit 5239229

File tree

1 file changed

+22
-2
lines changed

1 file changed

+22
-2
lines changed

common/preset.cpp

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,21 @@ static std::map<std::string, common_arg> get_map_key_opt(common_params_context &
157157
return mapping;
158158
}
159159

160+
static bool is_bool_arg(const common_arg & arg) {
161+
return !arg.args_neg.empty();
162+
}
163+
164+
static std::string parse_bool_arg(const common_arg & arg, const std::string & key, const std::string & value) {
165+
// if this is a negated arg, we need to reverse the value
166+
for (const auto & neg_arg : arg.args_neg) {
167+
if (rm_leading_dashes(neg_arg) == key) {
168+
return common_arg_utils::is_truthy(value) ? "false" : "true";
169+
}
170+
}
171+
// otherwise, not negated
172+
return value;
173+
}
174+
160175
common_presets common_presets_load(const std::string & path, common_params_context & ctx_params) {
161176
common_presets out;
162177
auto key_to_opt = get_map_key_opt(ctx_params);
@@ -173,8 +188,13 @@ common_presets common_presets_load(const std::string & path, common_params_conte
173188
for (const auto & [key, value] : section.second) {
174189
LOG_DBG("option: %s = %s\n", key.c_str(), value.c_str());
175190
if (key_to_opt.find(key) != key_to_opt.end()) {
176-
preset.options[key_to_opt[key]] = value;
177-
LOG_DBG("accepted option: %s = %s\n", key.c_str(), value.c_str());
191+
auto & opt = key_to_opt[key];
192+
if (is_bool_arg(opt)) {
193+
preset.options[opt] = parse_bool_arg(opt, key, value);
194+
} else {
195+
preset.options[opt] = value;
196+
}
197+
LOG_DBG("accepted option: %s = %s\n", key.c_str(), preset.options[opt].c_str());
178198
} else {
179199
// TODO: maybe warn about unknown key?
180200
}

0 commit comments

Comments
 (0)