Skip to content

Commit

Permalink
cli: Add command '-cwd' (change working directory).
Browse files Browse the repository at this point in the history
  • Loading branch information
helmesjo committed Mar 17, 2024
1 parent 7041994 commit e6500f4
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
4 changes: 4 additions & 0 deletions source/cppfront.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
16 changes: 14 additions & 2 deletions source/to_cpp1.h
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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(
Expand Down

0 comments on commit e6500f4

Please sign in to comment.