|
2 | 2 | #include "debug_utils-inl.h" |
3 | 3 | #include "simdjson.h" |
4 | 4 |
|
5 | | -#include <string> |
6 | | - |
7 | 5 | namespace node { |
8 | 6 |
|
| 7 | +constexpr std::string_view kConfigFileFlag = "--experimental-config-file"; |
| 8 | +constexpr std::string_view kDefaultConfigFileFlag = |
| 9 | + "--experimental-default-config-file"; |
| 10 | +constexpr std::string_view kDefaultConfigFileName = "node.config.json"; |
| 11 | + |
| 12 | +// Check if "--experimental-config-file=" has path arg |
| 13 | +inline bool HasEqualsPrefix(std::string_view arg, std::string_view flag) { |
| 14 | + return arg.size() > flag.size() + 1 && arg.starts_with(flag) && |
| 15 | + arg[flag.size()] == '='; |
| 16 | +} |
| 17 | + |
9 | 18 | std::optional<std::string_view> ConfigReader::GetDataFromArgs( |
10 | | - const std::vector<std::string>& args) { |
11 | | - constexpr std::string_view flag_path = "--experimental-config-file"; |
12 | | - constexpr std::string_view default_file = |
13 | | - "--experimental-default-config-file"; |
14 | | - |
15 | | - bool has_default_config_file = false; |
16 | | - |
17 | | - for (auto it = args.begin(); it != args.end(); ++it) { |
18 | | - if (*it == flag_path) { |
19 | | - // Case: "--experimental-config-file foo" |
20 | | - if (auto next = std::next(it); next != args.end()) { |
21 | | - return *next; |
22 | | - } |
23 | | - } else if (it->starts_with(flag_path)) { |
24 | | - // Case: "--experimental-config-file=foo" |
25 | | - if (it->size() > flag_path.size() && (*it)[flag_path.size()] == '=') { |
26 | | - return std::string_view(*it).substr(flag_path.size() + 1); |
| 19 | + std::vector<std::string>* args) { |
| 20 | + std::optional<std::string_view> result; |
| 21 | + |
| 22 | + for (size_t i = 0; i < args->size(); ++i) { |
| 23 | + std::string& arg = (*args)[i]; |
| 24 | + |
| 25 | + if (arg == kConfigFileFlag) { |
| 26 | + // Check if next arg is a path |
| 27 | + const bool has_next_arg = i + 1 < args->size(); |
| 28 | + const bool next_is_path = |
| 29 | + has_next_arg && !(*args)[i + 1].starts_with("-"); |
| 30 | + |
| 31 | + if (next_is_path) { |
| 32 | + // --experimental-config-file path |
| 33 | + arg = std::string(kConfigFileFlag) + "=" + (*args)[i + 1]; |
| 34 | + args->erase(args->begin() + static_cast<ptrdiff_t>(i + 1)); |
| 35 | + result = std::string_view(arg).substr(kConfigFileFlag.size() + 1); |
| 36 | + } else { |
| 37 | + // --experimental-config-file |
| 38 | + arg = std::string(kConfigFileFlag) + "=" + |
| 39 | + std::string(kDefaultConfigFileName); |
| 40 | + result = kDefaultConfigFileName; |
27 | 41 | } |
28 | | - } else if (*it == default_file || it->starts_with(default_file)) { |
29 | | - has_default_config_file = true; |
| 42 | + } else if (HasEqualsPrefix(arg, kConfigFileFlag)) { |
| 43 | + // --experimental-config-file=path |
| 44 | + result = std::string_view(arg).substr(kConfigFileFlag.size() + 1); |
| 45 | + } else if (arg == kDefaultConfigFileFlag) { |
| 46 | + // --experimental-default-config-file |
| 47 | + arg = std::string(kConfigFileFlag) + "=" + |
| 48 | + std::string(kDefaultConfigFileName); |
| 49 | + result = kDefaultConfigFileName; |
30 | 50 | } |
31 | 51 | } |
32 | 52 |
|
33 | | - if (has_default_config_file) { |
34 | | - return "node.config.json"; |
35 | | - } |
36 | | - |
37 | | - return std::nullopt; |
| 53 | + return result; |
38 | 54 | } |
39 | 55 |
|
40 | 56 | ParseResult ConfigReader::ProcessOptionValue( |
|
0 commit comments