Skip to content

Commit ab0f9ec

Browse files
committed
cli: Add command '-cwd' (change working directory).
1 parent dbf0bbf commit ab0f9ec

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

source/cppfront.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,10 @@ auto main(
4545
return EXIT_FAILURE;
4646
}
4747

48+
if (std::filesystem::exists(flag_cwd)) {
49+
std::filesystem::current_path(flag_cwd);
50+
}
51+
4852
// For each Cpp2 source file
4953
int exit_status = EXIT_SUCCESS;
5054
for (auto const& arg : cmdline.arguments())

source/to_cpp1.h

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,15 @@ static cmdline_processor::register_flag cmd_cpp1_filename(
176176
[](std::string const& name) { flag_cpp1_filename = name; }
177177
);
178178

179+
static auto flag_cwd = std::filesystem::path{};;
180+
static cpp2::cmdline_processor::register_flag cmd_cwd(
181+
9,
182+
"cwd directory",
183+
"Change current working directory",
184+
nullptr,
185+
[](std::string const& path) { flag_cwd = { path }; }
186+
);
187+
179188
static auto flag_print_colon_errors = false;
180189
static cmdline_processor::register_flag cmd_print_colon_errors(
181190
9,
@@ -1297,8 +1306,11 @@ class cppfront
12971306

12981307
// Now we'll open the Cpp1 file
12991308
auto cpp1_filename = sourcefile.substr(0, std::ssize(sourcefile) - 1);
1300-
if (!flag_cpp1_filename.empty()) {
1301-
cpp1_filename = flag_cpp1_filename; // use override if present
1309+
if (!flag_cpp1_filename.empty()) { // use override if present
1310+
cpp1_filename = flag_cpp1_filename;
1311+
}
1312+
else if (!flag_cwd.empty()) { // strip leading path if cwd was explicitly set
1313+
cpp1_filename = std::filesystem::path(cpp1_filename).filename().string();
13021314
}
13031315

13041316
printer.open(

0 commit comments

Comments
 (0)