Skip to content

Commit

Permalink
src: use for loop in Dotenv::GetPathFromArgs
Browse files Browse the repository at this point in the history
  • Loading branch information
avivkeller authored Aug 14, 2024
1 parent 38ad892 commit 0fe513e
Showing 1 changed file with 8 additions and 21 deletions.
29 changes: 8 additions & 21 deletions src/node_dotenv.cc
Original file line number Diff line number Diff line change
Expand Up @@ -13,33 +13,20 @@ using v8::String;

std::vector<std::string> Dotenv::GetPathFromArgs(
const std::vector<std::string>& args) {
const auto find_match = [](const std::string& arg) {
return arg == "--" || arg == "--env-file" || arg.starts_with("--env-file=");
};
std::vector<std::string> paths;
auto path = std::find_if(args.begin(), args.end(), find_match);
for (size_t i = 1; i < args.size(); ++i) {
const auto& arg = args[i];

while (path != args.end()) {
if (*path == "--") {
return paths;
if (arg == "--" || arg[0] != '-') {
break;
}
auto equal_char = path->find('=');

if (equal_char != std::string::npos) {
paths.push_back(path->substr(equal_char + 1));
} else {
auto next_path = std::next(path);

if (next_path == args.end()) {
return paths;
}

paths.push_back(*next_path);
if (arg.starts_with("--env-file=")) {
paths.push_back(arg.substr(11)); // Directly extract the path
} else if (arg == "--env-file" && i + 1 < args.size()) {
paths.push_back(args[++i]); // Advance to the next argument
}

path = std::find_if(++path, args.end(), find_match);
}

return paths;
}

Expand Down

0 comments on commit 0fe513e

Please sign in to comment.