GeezerVison is showing github.com — src: make CLI options programatically accesible · nodejs/node@b829958
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: src/env.h +4Lines changed: 4 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -118,6 +118,7 @@ struct PackageConfig { | 118 | 118 | // for the sake of convenience. Strings should be ASCII-only. | 119 | 119 | #define PER_ISOLATE_STRING_PROPERTIES(V) \ | 120 | 120 | V(address_string, "address") \ | | 121 | + V(aliases_string, "aliases") \ | 121 | 122 | V(args_string, "args") \ | 122 | 123 | V(async, "async") \ | 123 | 124 | V(async_ids_stack_string, "async_ids_stack") \ | @@ -156,6 +157,7 @@ struct PackageConfig { | 156 | 157 | V(entries_string, "entries") \ | 157 | 158 | V(entry_type_string, "entryType") \ | 158 | 159 | V(env_pairs_string, "envPairs") \ | | 160 | + V(env_var_settings_string, "envVarSettings") \ | 159 | 161 | V(errno_string, "errno") \ | 160 | 162 | V(error_string, "error") \ | 161 | 163 | V(exit_code_string, "exitCode") \ | @@ -176,6 +178,7 @@ struct PackageConfig { | 176 | 178 | V(get_shared_array_buffer_id_string, "_getSharedArrayBufferId") \ | 177 | 179 | V(gid_string, "gid") \ | 178 | 180 | V(handle_string, "handle") \ | | 181 | + V(help_text_string, "helpText") \ | 179 | 182 | V(homedir_string, "homedir") \ | 180 | 183 | V(host_string, "host") \ | 181 | 184 | V(hostmaster_string, "hostmaster") \ | @@ -233,6 +236,7 @@ struct PackageConfig { | 233 | 236 | V(onunpipe_string, "onunpipe") \ | 234 | 237 | V(onwrite_string, "onwrite") \ | 235 | 238 | V(openssl_error_stack, "opensslErrorStack") \ | | 239 | + V(options_string, "options") \ | 236 | 240 | V(output_string, "output") \ | 237 | 241 | V(order_string, "order") \ | 238 | 242 | V(parse_error_string, "Parse Error") \ | |
Collapse file Copy file name to clipboardExpand all lines: src/node.cc +17-9Lines changed: 17 additions & 9 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -126,6 +126,8 @@ typedef int mode_t; | 126 | 126 |
| 127 | 127 | namespace node { | 128 | 128 |
| | 129 | +using options_parser::kAllowedInEnvironment; | | 130 | +using options_parser::kDisallowedInEnvironment; | 129 | 131 | using v8::Array; | 130 | 132 | using v8::ArrayBuffer; | 131 | 133 | using v8::Boolean; | @@ -183,6 +185,7 @@ bool linux_at_secure = false; | 183 | 185 | // process-relative uptime base, initialized at start-up | 184 | 186 | double prog_start_time; | 185 | 187 |
| | 188 | +Mutex per_process_opts_mutex; | 186 | 189 | std::shared_ptr<PerProcessOptions> per_process_opts { | 187 | 190 | new PerProcessOptions() }; | 188 | 191 |
| @@ -2346,8 +2349,6 @@ void LoadEnvironment(Environment* env) { | 2346 | 2349 | } | 2347 | 2350 |
| 2348 | 2351 | static void PrintHelp() { | 2349 | | - // XXX: If you add an option here, please also add it to doc/node.1 and | 2350 | | - // doc/api/cli.md | 2351 | 2352 | printf("Usage: node [options] [ -e script | script.js | - ] [arguments]\n" | 2352 | 2353 | " node inspect script.js [arguments]\n" | 2353 | 2354 | "\n" | @@ -2747,13 +2748,20 @@ void ProcessArgv(std::vector<std::string>* args, | 2747 | 2748 | // Parse a few arguments which are specific to Node. | 2748 | 2749 | std::vector<std::string> v8_args; | 2749 | 2750 | std::string error; | 2750 | | - PerProcessOptionsParser::instance.Parse( | 2751 | | - args, | 2752 | | - exec_args, | 2753 | | - &v8_args, | 2754 | | - per_process_opts.get(), | 2755 | | - is_env ? kAllowedInEnvironment : kDisallowedInEnvironment, | 2756 | | - &error); | | 2751 | + | | 2752 | + { | | 2753 | + // TODO(addaleax): The mutex here should ideally be held during the | | 2754 | + // entire function, but that doesn't play well with the exit() calls below. | | 2755 | + Mutex::ScopedLock lock(per_process_opts_mutex); | | 2756 | + options_parser::PerProcessOptionsParser::instance.Parse( | | 2757 | + args, | | 2758 | + exec_args, | | 2759 | + &v8_args, | | 2760 | + per_process_opts.get(), | | 2761 | + is_env ? kAllowedInEnvironment : kDisallowedInEnvironment, | | 2762 | + &error); | | 2763 | + } | | 2764 | + | 2757 | 2765 | if (!error.empty()) { | 2758 | 2766 | fprintf(stderr, "%s: %s\n", args->at(0).c_str(), error.c_str()); | 2759 | 2767 | exit(9); | |
Collapse file Copy file name to clipboardExpand all lines: src/node_internals.h +2Lines changed: 2 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -119,6 +119,7 @@ struct sockaddr; | 119 | 119 | V(js_stream) \ | 120 | 120 | V(messaging) \ | 121 | 121 | V(module_wrap) \ | | 122 | + V(options) \ | 122 | 123 | V(os) \ | 123 | 124 | V(performance) \ | 124 | 125 | V(pipe_wrap) \ | @@ -176,6 +177,7 @@ extern Mutex environ_mutex; | 176 | 177 | // Tells whether it is safe to call v8::Isolate::GetCurrent(). | 177 | 178 | extern bool v8_initialized; | 178 | 179 |
| | 180 | +extern Mutex per_process_opts_mutex; | 179 | 181 | extern std::shared_ptr<PerProcessOptions> per_process_opts; | 180 | 182 |
| 181 | 183 | extern const char* const environment_flags[]; | |
Collapse file Copy file name to clipboardExpand all lines: src/node_options-inl.h +51-39Lines changed: 51 additions & 39 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -21,73 +21,88 @@ EnvironmentOptions* PerIsolateOptions::get_per_env_options() { | 21 | 21 | return per_env.get(); | 22 | 22 | } | 23 | 23 |
| | 24 | +namespace options_parser { | | 25 | + | 24 | 26 | template <typename Options> | 25 | 27 | void OptionsParser<Options>::AddOption(const std::string& name, | | 28 | + const std::string& help_text, | 26 | 29 | bool Options::* field, | 27 | 30 | OptionEnvvarSettings env_setting) { | 28 | | - options_.emplace(name, OptionInfo { | 29 | | - kBoolean, | 30 | | - std::make_shared<SimpleOptionField<bool>>(field), | 31 | | - env_setting | 32 | | - }); | | 31 | + options_.emplace(name, | | 32 | + OptionInfo{kBoolean, | | 33 | + std::make_shared<SimpleOptionField<bool>>(field), | | 34 | + env_setting, | | 35 | + help_text}); | 33 | 36 | } | 34 | 37 |
| 35 | 38 | template <typename Options> | 36 | 39 | void OptionsParser<Options>::AddOption(const std::string& name, | | 40 | + const std::string& help_text, | 37 | 41 | int64_t Options::* field, | 38 | 42 | OptionEnvvarSettings env_setting) { | 39 | | - options_.emplace(name, OptionInfo { | 40 | | - kInteger, | 41 | | - std::make_shared<SimpleOptionField<int64_t>>(field), | 42 | | - env_setting | 43 | | - }); | | 43 | + options_.emplace( | | 44 | + name, | | 45 | + OptionInfo{kInteger, | | 46 | + std::make_shared<SimpleOptionField<int64_t>>(field), | | 47 | + env_setting, | | 48 | + help_text}); | 44 | 49 | } | 45 | 50 |
| 46 | 51 | template <typename Options> | 47 | 52 | void OptionsParser<Options>::AddOption(const std::string& name, | | 53 | + const std::string& help_text, | 48 | 54 | std::string Options::* field, | 49 | 55 | OptionEnvvarSettings env_setting) { | 50 | | - options_.emplace(name, OptionInfo { | 51 | | - kString, | 52 | | - std::make_shared<SimpleOptionField<std::string>>(field), | 53 | | - env_setting | 54 | | - }); | | 56 | + options_.emplace( | | 57 | + name, | | 58 | + OptionInfo{kString, | | 59 | + std::make_shared<SimpleOptionField<std::string>>(field), | | 60 | + env_setting, | | 61 | + help_text}); | 55 | 62 | } | 56 | 63 |
| 57 | 64 | template <typename Options> | 58 | 65 | void OptionsParser<Options>::AddOption( | 59 | 66 | const std::string& name, | | 67 | + const std::string& help_text, | 60 | 68 | std::vector<std::string> Options::* field, | 61 | 69 | OptionEnvvarSettings env_setting) { | 62 | 70 | options_.emplace(name, OptionInfo { | 63 | 71 | kStringList, | 64 | 72 | std::make_shared<SimpleOptionField<std::vector<std::string>>>(field), | 65 | | - env_setting | | 73 | + env_setting, | | 74 | + help_text | 66 | 75 | }); | 67 | 76 | } | 68 | 77 |
| 69 | 78 | template <typename Options> | 70 | 79 | void OptionsParser<Options>::AddOption(const std::string& name, | | 80 | + const std::string& help_text, | 71 | 81 | HostPort Options::* field, | 72 | 82 | OptionEnvvarSettings env_setting) { | 73 | | - options_.emplace(name, OptionInfo { | 74 | | - kHostPort, | 75 | | - std::make_shared<SimpleOptionField<HostPort>>(field), | 76 | | - env_setting | 77 | | - }); | | 83 | + options_.emplace( | | 84 | + name, | | 85 | + OptionInfo{kHostPort, | | 86 | + std::make_shared<SimpleOptionField<HostPort>>(field), | | 87 | + env_setting, | | 88 | + help_text}); | 78 | 89 | } | 79 | 90 |
| 80 | 91 | template <typename Options> | 81 | | -void OptionsParser<Options>::AddOption(const std::string& name, NoOp no_op_tag, | | 92 | +void OptionsParser<Options>::AddOption(const std::string& name, | | 93 | + const std::string& help_text, | | 94 | + NoOp no_op_tag, | 82 | 95 | OptionEnvvarSettings env_setting) { | 83 | | - options_.emplace(name, OptionInfo { kNoOp, nullptr, env_setting }); | | 96 | + options_.emplace(name, OptionInfo{kNoOp, nullptr, env_setting, help_text}); | 84 | 97 | } | 85 | 98 |
| 86 | 99 | template <typename Options> | 87 | 100 | void OptionsParser<Options>::AddOption(const std::string& name, | | 101 | + const std::string& help_text, | 88 | 102 | V8Option v8_option_tag, | 89 | 103 | OptionEnvvarSettings env_setting) { | 90 | | - options_.emplace(name, OptionInfo { kV8Option, nullptr, env_setting }); | | 104 | + options_.emplace(name, | | 105 | + OptionInfo{kV8Option, nullptr, env_setting, help_text}); | 91 | 106 | } | 92 | 107 |
| 93 | 108 | template <typename Options> | @@ -161,11 +176,10 @@ template <typename ChildOptions> | 161 | 176 | auto OptionsParser<Options>::Convert( | 162 | 177 | typename OptionsParser<ChildOptions>::OptionInfo original, | 163 | 178 | ChildOptions* (Options::* get_child)()) { | 164 | | - return OptionInfo { | 165 | | - original.type, | 166 | | - Convert(original.field, get_child), | 167 | | - original.env_setting | 168 | | - }; | | 179 | + return OptionInfo{original.type, | | 180 | + Convert(original.field, get_child), | | 181 | + original.env_setting, | | 182 | + original.help_text}; | 169 | 183 | } | 170 | 184 |
| 171 | 185 | template <typename Options> | @@ -385,24 +399,21 @@ void OptionsParser<Options>::Parse( | 385 | 399 |
| 386 | 400 | switch (info.type) { | 387 | 401 | case kBoolean: | 388 | | - *std::static_pointer_cast<OptionField<bool>>(info.field) | 389 | | - ->Lookup(options) = true; | | 402 | + *Lookup<bool>(info.field, options) = true; | 390 | 403 | break; | 391 | 404 | case kInteger: | 392 | | - *std::static_pointer_cast<OptionField<int64_t>>(info.field) | 393 | | - ->Lookup(options) = std::atoll(value.c_str()); | | 405 | + *Lookup<int64_t>(info.field, options) = std::atoll(value.c_str()); | 394 | 406 | break; | 395 | 407 | case kString: | 396 | | - *std::static_pointer_cast<OptionField<std::string>>(info.field) | 397 | | - ->Lookup(options) = value; | | 408 | + *Lookup<std::string>(info.field, options) = value; | 398 | 409 | break; | 399 | 410 | case kStringList: | 400 | | - std::static_pointer_cast<OptionField<std::vector<std::string>>>( | 401 | | - info.field)->Lookup(options)->emplace_back(std::move(value)); | | 411 | + Lookup<std::vector<std::string>>(info.field, options) | | 412 | + ->emplace_back(std::move(value)); | 402 | 413 | break; | 403 | 414 | case kHostPort: | 404 | | - std::static_pointer_cast<OptionField<HostPort>>(info.field) | 405 | | - ->Lookup(options)->Update(SplitHostPort(value, error)); | | 415 | + Lookup<HostPort>(info.field, options) | | 416 | + ->Update(SplitHostPort(value, error)); | 406 | 417 | break; | 407 | 418 | case kNoOp: | 408 | 419 | break; | @@ -415,6 +426,7 @@ void OptionsParser<Options>::Parse( | 415 | 426 | } | 416 | 427 | } | 417 | 428 |
| | 429 | +} // namespace options_parser | 418 | 430 | } // namespace node | 419 | 431 |
| 420 | 432 | #endif // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS | |
|