diff --git a/source/cppfront.cpp b/source/cppfront.cpp index 414ec4f316..0c89b6c538 100644 --- a/source/cppfront.cpp +++ b/source/cppfront.cpp @@ -53,6 +53,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 79289105b4..c67a762452 100644 --- a/source/to_cpp1.h +++ b/source/to_cpp1.h @@ -164,6 +164,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 path", + "Change current working directory to path", + nullptr, + [](std::string const& path) { flag_cwd = { path }; } +); + static auto flag_no_exceptions = false; static cmdline_processor::register_flag cmd_no_exceptions( 4, @@ -1245,8 +1254,14 @@ class cppfront // Now we'll open the Cpp1 file auto cpp1_filename = sourcefile.substr(0, std::ssize(sourcefile) - 1); + + // Use explicit filename override if present, + // otherwise strip leading path if (!flag_cpp1_filename.empty()) { - cpp1_filename = flag_cpp1_filename; // use override if present + cpp1_filename = flag_cpp1_filename; + } + else { + cpp1_filename = std::filesystem::path(cpp1_filename).filename().string(); } printer.open(