Skip to content

Commit f956ce6

Browse files
Vaishnavi220506sarlinpeahojnnes
authored
Fix command-line overrides for project files (colmap#4902)
## Summary Command-line options passed together with `--project_path` were silently ignored when the same option was present in the project file. This PR gives explicit command-line values precedence and adds a regression test. ## Problem Given a project file containing: ```ini database_path=/tmp/config.db image_path=/tmp/images ``` running: ```text colmap ... --project_path project.ini --database_path /tmp/command-line.db ``` should use `/tmp/command-line.db`, but COLMAP used `/tmp/config.db` instead. ## Cause `BaseOptionManager::Parse` parsed the command line, then loaded the project file through `Read`, which created and notified a separate variables map. The project-file values therefore replaced the already parsed command-line state. ## Changes - Parse project-file options into the existing command-line variables map, preserving explicit command-line values while still filling options supplied only by the project file. - Share project-file parsing and unregistered-option diagnostics between `Parse` and `Read`. - Add a regression test covering command-line database-path overrides and project-file-only image paths. ## Testing Focused: `/home/vaishnavi/colmap-flagship-20261026-clean-build/src/colmap/controllers/base_option_manager_test --gtest_filter=BaseOptionManager.CommandLineOptionsOverrideProjectFile` — 1 passed on HEAD `0976409702e778bb89db531dd8ab7b8fa10bba06`. Adjacent: `ctest --test-dir /home/vaishnavi/colmap-flagship-20261026-clean-build -R "(base_option_manager_test|option_manager_test)" --output-on-failure` — 2 tests passed: 20 `BaseOptionManager` tests and 14 `OptionManager` tests. Format: `clang-format --dry-run --Werror` with clang-format 23.1.3 — passed. `git diff --check` — passed. Full suite: not run. Not covered: changing project-file validation or unsupported-option handling; this PR only changes precedence when both sources specify an option. --------- Co-authored-by: Paul-Edouard Sarlin <[email protected]> Co-authored-by: Johannes Schönberger <[email protected]>
1 parent ce2b5ab commit f956ce6

3 files changed

Lines changed: 132 additions & 41 deletions

File tree

‎doc/cli.rst‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,9 @@ the available options, e.g.::
330330

331331

332332
The available options can either be provided directly from the command-line or
333-
through a ``.ini`` file provided to ``--project_path``.
333+
through a ``.ini`` file provided to ``--project_path``. When an option is
334+
specified in both places, the command-line value takes precedence, and options
335+
may be split across the two sources.
334336

335337

336338
Commands

‎src/colmap/controllers/base_option_manager.cc‎

Lines changed: 31 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,30 @@
1313
namespace config = boost::program_options;
1414

1515
namespace colmap {
16+
namespace {
17+
18+
void ParseConfigFile(const std::filesystem::path& path,
19+
const config::options_description& desc,
20+
bool allow_unregistered,
21+
config::variables_map* vmap) {
22+
THROW_CHECK_FILE_EXISTS(path);
23+
std::ifstream file(path);
24+
THROW_CHECK_FILE_OPEN(file, path);
25+
26+
const config::parsed_options parsed_options =
27+
config::parse_config_file(file, desc, allow_unregistered);
28+
config::store(parsed_options, *vmap);
29+
30+
if (allow_unregistered) {
31+
for (const auto& option : parsed_options.options) {
32+
if (option.unregistered) {
33+
LOG(WARNING) << "Unrecognized option key: " << option.string_key;
34+
}
35+
}
36+
}
37+
}
38+
39+
} // namespace
1640

1741
BaseOptionManager::BaseOptionManager(bool add_project_options) {
1842
project_path = std::make_shared<std::filesystem::path>();
@@ -225,18 +249,15 @@ bool BaseOptionManager::Parse(const int argc, char** argv) {
225249

226250
if (vmap.count("project_path")) {
227251
*project_path = vmap["project_path"].as<std::string>();
228-
if (!Read(*project_path)) {
229-
return false;
230-
}
231-
} else {
232-
vmap.notify();
252+
ParseConfigFile(
253+
*project_path, *desc_, /*allow_unregistered=*/true, &vmap);
233254
}
234255

256+
vmap.notify();
235257
ApplyEnumConversions();
236258
ApplyLogFlags();
237259
PostParse();
238-
239-
} catch (std::exception& exc) {
260+
} catch (const std::exception& exc) {
240261
LOG(ERROR) << "Failed to parse options - " << exc.what() << ".";
241262
return false;
242263
} catch (...) {
@@ -256,31 +277,12 @@ bool BaseOptionManager::Read(const std::filesystem::path& path,
256277
bool allow_unregistered) {
257278
config::variables_map vmap;
258279

259-
if (!ExistsFile(path)) {
260-
LOG(ERROR) << "Configuration file does not exist.";
261-
return false;
262-
}
263-
264280
try {
265-
std::ifstream file(path);
266-
THROW_CHECK_FILE_OPEN(file, path);
267-
268-
const config::parsed_options parsed_options =
269-
config::parse_config_file(file, *desc_, allow_unregistered);
270-
config::store(parsed_options, vmap);
271-
272-
if (allow_unregistered) {
273-
for (const auto& option : parsed_options.options) {
274-
if (option.unregistered) {
275-
LOG(WARNING) << "Unrecognized option key: " << option.string_key;
276-
}
277-
}
278-
}
279-
281+
ParseConfigFile(path, *desc_, allow_unregistered, &vmap);
280282
vmap.notify();
281283
ApplyEnumConversions();
282-
} catch (std::exception& e) {
283-
LOG(ERROR) << "Failed to parse options " << e.what() << ".";
284+
} catch (const std::exception& e) {
285+
LOG(ERROR) << "Failed to parse options - " << e.what() << ".";
284286
return false;
285287
} catch (...) {
286288
LOG(ERROR) << "Failed to parse options for unknown reason.";

‎src/colmap/controllers/base_option_manager_test.cc‎

Lines changed: 98 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -263,37 +263,124 @@ TEST(BaseOptionManager, ParseWithProjectPath) {
263263
const auto config_path = test_dir / "config.ini";
264264
CreateDirIfNotExists(test_dir / "images");
265265

266-
// Create and write a config file
267-
BaseOptionManager options_write;
268-
options_write.AddDatabaseOptions();
269-
options_write.AddImageOptions();
266+
auto make_options = [](int* int_opt, TestEnumType* enum_opt) {
267+
BaseOptionManager opts;
268+
opts.AddDatabaseOptions();
269+
opts.AddImageOptions();
270+
opts.AddDefaultOption("int_option", int_opt);
271+
opts.AddDefaultEnumOption(
272+
"enum_option", enum_opt, TestEnumTypeToString, TestEnumTypeFromString);
273+
return opts;
274+
};
275+
auto parse_args = [](BaseOptionManager& opts, std::vector<std::string> args) {
276+
std::vector<char*> argv;
277+
argv.reserve(args.size());
278+
for (auto& arg : args) {
279+
argv.push_back(arg.data());
280+
}
281+
return opts.Parse(argv.size(), argv.data());
282+
};
270283

284+
int int_write = 42;
285+
TestEnumType enum_write = TestEnumType::VALUE_B;
286+
BaseOptionManager options_write = make_options(&int_write, &enum_write);
271287
*options_write.database_path = test_dir / "database.db";
272288
*options_write.image_path = test_dir / "images";
273289
options_write.Write(config_path);
274290

275-
// Parse using project_path
291+
// Parse using project_path alone.
292+
int int_read = 0;
293+
TestEnumType enum_read = TestEnumType::VALUE_A;
294+
BaseOptionManager options = make_options(&int_read, &enum_read);
295+
ASSERT_TRUE(
296+
parse_args(options, {"colmap", "--project_path", config_path.string()}));
297+
EXPECT_EQ(*options.database_path, *options_write.database_path);
298+
EXPECT_EQ(*options.image_path, *options_write.image_path);
299+
EXPECT_EQ(int_read, 42);
300+
EXPECT_EQ(enum_read, TestEnumType::VALUE_B);
301+
302+
// Command-line flags override project-file values (including overriding an
303+
// invalid project-file path and resetting an option to its C++ default).
304+
*options_write.database_path = test_dir / "missing_dir" / "config.db";
305+
options_write.Write(config_path);
306+
307+
int int_override = 0;
308+
TestEnumType enum_override = TestEnumType::VALUE_A;
309+
BaseOptionManager options_override =
310+
make_options(&int_override, &enum_override);
311+
const auto cli_database_path = test_dir / "command_line.db";
312+
ASSERT_TRUE(parse_args(options_override,
313+
{"colmap",
314+
"--project_path",
315+
config_path.string(),
316+
"--database_path",
317+
cli_database_path.string(),
318+
"--enum_option",
319+
"VALUE_A"}));
320+
EXPECT_EQ(*options_override.database_path, cli_database_path);
321+
EXPECT_EQ(*options_override.image_path, *options_write.image_path);
322+
EXPECT_EQ(int_override, 42);
323+
EXPECT_EQ(enum_override, TestEnumType::VALUE_A);
324+
}
325+
326+
TEST(BaseOptionManager, ParseWithProjectPathSplitOptions) {
327+
const auto test_dir = CreateTestDir();
328+
const auto config_path = test_dir / "config.ini";
329+
CreateDirIfNotExists(test_dir / "images");
330+
331+
// The project file provides only image_path. The required database_path
332+
// is supplied on the command line instead, and int_option appears only
333+
// on the command line. Both must be applied by the single merged parse.
334+
std::ofstream file(config_path);
335+
file << "image_path=" << (test_dir / "images").string() << "\n";
336+
file.close();
337+
338+
int int_option = 0;
276339
BaseOptionManager options;
277340
options.AddDatabaseOptions();
278341
options.AddImageOptions();
342+
options.AddDefaultOption("int_option", &int_option);
279343

280-
const std::vector<std::string> args = {
344+
const auto cli_database_path = test_dir / "command_line.db";
345+
std::vector<std::string> args = {
281346
"colmap",
282347
"--project_path",
283348
config_path.string(),
349+
"--database_path",
350+
cli_database_path.string(),
351+
"--int_option",
352+
"7",
284353
};
285354

286355
std::vector<char*> argv;
287356
argv.reserve(args.size());
288357
for (auto& arg : args) {
289-
argv.push_back(const_cast<char*>(arg.c_str()));
358+
argv.push_back(arg.data());
290359
}
291360

292-
EXPECT_TRUE(options.Parse(argv.size(), argv.data()));
361+
ASSERT_TRUE(options.Parse(argv.size(), argv.data()));
362+
EXPECT_EQ(*options.database_path, cli_database_path);
363+
EXPECT_EQ(*options.image_path, test_dir / "images");
364+
EXPECT_EQ(int_option, 7);
365+
}
293366

294-
// Verify values were loaded from config file
295-
EXPECT_EQ(*options.database_path, *options_write.database_path);
296-
EXPECT_EQ(*options.image_path, *options_write.image_path);
367+
TEST(BaseOptionManager, ParseWithMissingProjectPathFails) {
368+
const auto test_dir = CreateTestDir();
369+
370+
BaseOptionManager options;
371+
options.AddDatabaseOptions();
372+
options.AddImageOptions();
373+
374+
const std::vector<std::string> args = {
375+
"colmap", "--project_path", (test_dir / "missing.ini").string()};
376+
377+
std::vector<char*> argv;
378+
argv.reserve(args.size());
379+
for (auto& arg : args) {
380+
argv.push_back(const_cast<char*>(arg.c_str()));
381+
}
382+
383+
EXPECT_FALSE(options.Parse(argv.size(), argv.data()));
297384
}
298385

299386
TEST(BaseOptionManager, ParseEmptyArguments) {

0 commit comments

Comments
 (0)