Skip to content

Commit 67f5e0e

Browse files
helmesjohsutter
andauthored
feat: Add command -cwd (change working directory) (#1024)
* cli: Add command '-cwd' (change working directory). * Tweak to strip leading path always, so that the `.cpp2` source file path is stripped In addition to enabling `-cwd` behavior, this will change the default to put files in the current directory even if a pathname is used to refer to a file in another directory (this default behavior will be overridden by `-o` or `-cwd` if specified) * Remove stray `;;` for clean builds --------- Co-authored-by: Herb Sutter <[email protected]>
1 parent d0836be commit 67f5e0e

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

source/cppfront.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,10 @@ auto main(
5353
return EXIT_FAILURE;
5454
}
5555

56+
if (std::filesystem::exists(flag_cwd)) {
57+
std::filesystem::current_path(flag_cwd);
58+
}
59+
5660
// For each Cpp2 source file
5761
int exit_status = EXIT_SUCCESS;
5862
for (auto const& arg : cmdline.arguments())

source/to_cpp1.h

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,15 @@ static cmdline_processor::register_flag cmd_cpp1_filename(
164164
[](std::string const& name) { flag_cpp1_filename = name; }
165165
);
166166

167+
static auto flag_cwd = std::filesystem::path{};
168+
static cpp2::cmdline_processor::register_flag cmd_cwd(
169+
9,
170+
"cwd path",
171+
"Change current working directory to path",
172+
nullptr,
173+
[](std::string const& path) { flag_cwd = { path }; }
174+
);
175+
167176
static auto flag_no_exceptions = false;
168177
static cmdline_processor::register_flag cmd_no_exceptions(
169178
4,
@@ -1245,8 +1254,14 @@ class cppfront
12451254

12461255
// Now we'll open the Cpp1 file
12471256
auto cpp1_filename = sourcefile.substr(0, std::ssize(sourcefile) - 1);
1257+
1258+
// Use explicit filename override if present,
1259+
// otherwise strip leading path
12481260
if (!flag_cpp1_filename.empty()) {
1249-
cpp1_filename = flag_cpp1_filename; // use override if present
1261+
cpp1_filename = flag_cpp1_filename;
1262+
}
1263+
else {
1264+
cpp1_filename = std::filesystem::path(cpp1_filename).filename().string();
12501265
}
12511266

12521267
printer.open(

0 commit comments

Comments
 (0)