diff --git a/source/cppfront.cpp b/source/cppfront.cpp index 9e9a7821fe..64c53689d2 100644 --- a/source/cppfront.cpp +++ b/source/cppfront.cpp @@ -45,6 +45,10 @@ auto main( return EXIT_FAILURE; } + if (std::filesystem::exists(flag_cwd)) { + std::filesystem::current_path(flag_cwd); + } + // For each Cpp2 source file int exit_status = EXIT_SUCCESS; for (auto const& arg : cmdline.arguments()) diff --git a/source/to_cpp1.h b/source/to_cpp1.h index 80e0ebad5b..d83806df0c 100644 --- a/source/to_cpp1.h +++ b/source/to_cpp1.h @@ -176,6 +176,15 @@ static cmdline_processor::register_flag cmd_cpp1_filename( [](std::string const& name) { flag_cpp1_filename = name; } ); +static auto flag_cwd = std::filesystem::path{};; +static cpp2::cmdline_processor::register_flag cmd_cwd( + 9, + "cwd directory", + "Change current working directory", + nullptr, + [](std::string const& path) { flag_cwd = { path }; } +); + static auto flag_print_colon_errors = false; static cmdline_processor::register_flag cmd_print_colon_errors( 9, @@ -1297,8 +1306,11 @@ class cppfront // Now we'll open the Cpp1 file auto cpp1_filename = sourcefile.substr(0, std::ssize(sourcefile) - 1); - if (!flag_cpp1_filename.empty()) { - cpp1_filename = flag_cpp1_filename; // use override if present + if (!flag_cpp1_filename.empty()) { // use override if present + cpp1_filename = flag_cpp1_filename; + } + else if (!flag_cwd.empty()) { // strip leading path if cwd was explicitly set + cpp1_filename = std::filesystem::path(cpp1_filename).filename().string(); } printer.open(