GeezerVison is showing github.com — cli: normalize `_` → `-` when parsing options · nodejs/node@c29e5ac
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: doc/api/cli.md +12-5Lines changed: 12 additions & 5 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
@@ -21,6 +21,18 @@ Execute without arguments to start the [REPL][]. | 21 | 21 | _For more info about `node inspect`, please see the [debugger][] documentation._ | 22 | 22 |
| 23 | 23 | ## Options | | 24 | +<!-- YAML | | 25 | +changes: | | 26 | + - version: REPLACEME | | 27 | + pr-url: https://github.com/nodejs/node/pull/23020 | | 28 | + description: Underscores instead of dashes are now allowed for | | 29 | + Node.js options as well, in addition to V8 options. | | 30 | +--> | | 31 | + | | 32 | +All options, including V8 options, allow words to be separated by both | | 33 | +dashes (`-`) or underscores (`_`). | | 34 | + | | 35 | +For example, `--pending-deprecation` is equivalent to `--pending_deprecation`. | 24 | 36 |
| 25 | 37 | ### `-` | 26 | 38 | <!-- YAML | @@ -390,11 +402,6 @@ added: v0.1.3 | 390 | 402 |
| 391 | 403 | Print V8 command line options. | 392 | 404 |
| 393 | | -V8 options allow words to be separated by both dashes (`-`) or | 394 | | -underscores (`_`). | 395 | | - | 396 | | -For example, `--stack-trace-limit` is equivalent to `--stack_trace_limit`. | 397 | | - | 398 | 405 | ### `--v8-pool-size=num` | 399 | 406 | <!-- YAML | 400 | 407 | added: v5.10.0 | |
Collapse file Copy file name to clipboardExpand all lines: lib/internal/bootstrap/node.js +12-31Lines changed: 12 additions & 31 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 616 | 616 | // This builds process.allowedNodeEnvironmentFlags | 617 | 617 | // from data in the config binding | 618 | 618 |
| 619 | | - const replaceDashesRegex = /-/g; | | 619 | + const replaceUnderscoresRegex = /_/g; | 620 | 620 | const leadingDashesRegex = /^--?/; | 621 | 621 | const trailingValuesRegex = /=.*$/; | 622 | 622 |
| | 628 | 628 | const get = () => { | 629 | 629 | const { | 630 | 630 | getOptions, | 631 | | - types: { kV8Option }, | 632 | 631 | envSettings: { kAllowedInEnvironment } | 633 | 632 | } = internalBinding('options'); | 634 | 633 | const { options, aliases } = getOptions(); | 635 | 634 |
| 636 | | - const allowedV8EnvironmentFlags = []; | 637 | 635 | const allowedNodeEnvironmentFlags = []; | 638 | 636 | for (const [name, info] of options) { | 639 | 637 | if (info.envVarSettings === kAllowedInEnvironment) { | 640 | | - if (info.type === kV8Option) { | 641 | | - allowedV8EnvironmentFlags.push(name); | 642 | | - } else { | 643 | | - allowedNodeEnvironmentFlags.push(name); | 644 | | - } | | 638 | + allowedNodeEnvironmentFlags.push(name); | 645 | 639 | } | 646 | 640 | } | 647 | 641 |
| | 675 | 669 | // process.allowedNodeEnvironmentFlags.has() which lack leading dashes. | 676 | 670 | // Avoid interference w/ user code by flattening `Set.prototype` into | 677 | 671 | // each object. | 678 | | - const [nodeFlags, v8Flags] = [ | 679 | | - allowedNodeEnvironmentFlags, allowedV8EnvironmentFlags | 680 | | - ].map((flags) => Object.defineProperties( | 681 | | - new Set(flags.map(trimLeadingDashes)), | 682 | | - Object.getOwnPropertyDescriptors(Set.prototype)) | | 672 | + const nodeFlags = Object.defineProperties( | | 673 | + new Set(allowedNodeEnvironmentFlags.map(trimLeadingDashes)), | | 674 | + Object.getOwnPropertyDescriptors(Set.prototype) | 683 | 675 | ); | 684 | 676 |
| 685 | 677 | class NodeEnvironmentFlagsSet extends Set { | | 703 | 695 | has(key) { | 704 | 696 | // This will return `true` based on various possible | 705 | 697 | // permutations of a flag, including present/missing leading | 706 | | - // dash(es) and/or underscores-for-dashes in the case of V8-specific | 707 | | - // flags. Strips any values after `=`, inclusive. | | 698 | + // dash(es) and/or underscores-for-dashes. | | 699 | + // Strips any values after `=`, inclusive. | 708 | 700 | // TODO(addaleax): It might be more flexible to run the option parser | 709 | 701 | // on a dummy option set and see whether it rejects the argument or | 710 | 702 | // not. | 711 | 703 | if (typeof key === 'string') { | 712 | | - key = replace(key, trailingValuesRegex, ''); | | 704 | + key = replace(key, replaceUnderscoresRegex, '-'); | 713 | 705 | if (test(leadingDashesRegex, key)) { | 714 | | - return has(this, key) || | 715 | | - has(v8Flags, | 716 | | - replace( | 717 | | - replace( | 718 | | - key, | 719 | | - leadingDashesRegex, | 720 | | - '' | 721 | | - ), | 722 | | - replaceDashesRegex, | 723 | | - '_' | 724 | | - ) | 725 | | - ); | | 706 | + key = replace(key, trailingValuesRegex, ''); | | 707 | + return has(this, key); | 726 | 708 | } | 727 | | - return has(nodeFlags, key) || | 728 | | - has(v8Flags, replace(key, replaceDashesRegex, '_')); | | 709 | + return has(nodeFlags, key); | 729 | 710 | } | 730 | 711 | return false; | 731 | 712 | } | | 736 | 717 |
| 737 | 718 | return process.allowedNodeEnvironmentFlags = Object.freeze( | 738 | 719 | new NodeEnvironmentFlagsSet( | 739 | | - allowedNodeEnvironmentFlags.concat(allowedV8EnvironmentFlags) | | 720 | + allowedNodeEnvironmentFlags | 740 | 721 | )); | 741 | 722 | }; | 742 | 723 |
| |
Collapse file Copy file name to clipboardExpand all lines: src/node_options-inl.h +6-13Lines changed: 6 additions & 13 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -307,6 +307,12 @@ void OptionsParser<Options>::Parse( | 307 | 307 | if (equals_index != std::string::npos) | 308 | 308 | original_name += '='; | 309 | 309 |
| | 310 | + // Normalize by replacing `_` with `-` in options. | | 311 | + for (std::string::size_type i = 2; i < name.size(); ++i) { | | 312 | + if (name[i] == '_') | | 313 | + name[i] = '-'; | | 314 | + } | | 315 | + | 310 | 316 | { | 311 | 317 | auto it = aliases_.end(); | 312 | 318 | // Expand aliases: | @@ -341,19 +347,6 @@ void OptionsParser<Options>::Parse( | 341 | 347 |
| 342 | 348 | auto it = options_.find(name); | 343 | 349 |
| 344 | | - if (it == options_.end()) { | 345 | | - // We would assume that this is a V8 option if neither we nor any child | 346 | | - // parser knows about it, so we convert - to _ for | 347 | | - // canonicalization (since V8 accepts both) and look up again in order | 348 | | - // to find a match. | 349 | | - // TODO(addaleax): Make the canonicalization unconditional, i.e. allow | 350 | | - // both - and _ in Node's own options as well. | 351 | | - std::string::size_type index = 2; // Start after initial '--'. | 352 | | - while ((index = name.find('-', index + 1)) != std::string::npos) | 353 | | - name[index] = '_'; | 354 | | - it = options_.find(name); | 355 | | - } | 356 | | - | 357 | 350 | if ((it == options_.end() || | 358 | 351 | it->second.env_setting == kDisallowedInEnvironment) && | 359 | 352 | required_env_settings == kAllowedInEnvironment) { | |
Collapse file Copy file name to clipboardExpand all lines: src/node_options.cc +5-7Lines changed: 5 additions & 7 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -102,8 +102,6 @@ EnvironmentOptionsParser::EnvironmentOptionsParser() { | 102 | 102 | &EnvironmentOptions::experimental_worker, | 103 | 103 | kAllowedInEnvironment); | 104 | 104 | AddOption("--expose-internals", "", &EnvironmentOptions::expose_internals); | 105 | | - // TODO(addaleax): Remove this when adding -/_ canonicalization to the parser. | 106 | | - AddAlias("--expose_internals", "--expose-internals"); | 107 | 105 | AddOption("--loader", | 108 | 106 | "(with --experimental-modules) use the specified file as a " | 109 | 107 | "custom loader", | @@ -206,15 +204,15 @@ PerIsolateOptionsParser::PerIsolateOptionsParser() { | 206 | 204 | kAllowedInEnvironment); | 207 | 205 |
| 208 | 206 | // Explicitly add some V8 flags to mark them as allowed in NODE_OPTIONS. | 209 | | - AddOption("--abort_on_uncaught_exception", | | 207 | + AddOption("--abort-on-uncaught-exception", | 210 | 208 | "aborting instead of exiting causes a core file to be generated " | 211 | 209 | "for analysis", | 212 | 210 | V8Option{}, | 213 | 211 | kAllowedInEnvironment); | 214 | | - AddOption("--max_old_space_size", "", V8Option{}, kAllowedInEnvironment); | 215 | | - AddOption("--perf_basic_prof", "", V8Option{}, kAllowedInEnvironment); | 216 | | - AddOption("--perf_prof", "", V8Option{}, kAllowedInEnvironment); | 217 | | - AddOption("--stack_trace_limit", "", V8Option{}, kAllowedInEnvironment); | | 212 | + AddOption("--max-old-space-size", "", V8Option{}, kAllowedInEnvironment); | | 213 | + AddOption("--perf-basic-prof", "", V8Option{}, kAllowedInEnvironment); | | 214 | + AddOption("--perf-prof", "", V8Option{}, kAllowedInEnvironment); | | 215 | + AddOption("--stack-trace-limit", "", V8Option{}, kAllowedInEnvironment); | 218 | 216 |
| 219 | 217 | Insert(&EnvironmentOptionsParser::instance, | 220 | 218 | &PerIsolateOptions::get_per_env_options); | |
Collapse file Copy file name to clipboardExpand all lines: test/parallel/test-cli-node-options-disallowed.js -1Lines changed: 0 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -29,7 +29,6 @@ disallow('--interactive'); | 29 | 29 | disallow('-i'); | 30 | 30 | disallow('--v8-options'); | 31 | 31 | disallow('--'); | 32 | | -disallow('--no_warnings'); // Node options don't allow '_' instead of '-'. | 33 | 32 |
| 34 | 33 | function disallow(opt) { | 35 | 34 | const env = Object.assign({}, process.env, { NODE_OPTIONS: opt }); | |
Collapse file Copy file name to clipboardExpand all lines: test/parallel/test-cli-node-options.js +1Lines changed: 1 addition & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -19,6 +19,7 @@ expect(`-r ${printA}`, 'A\nB\n'); | 19 | 19 | expect(`-r ${printA} -r ${printA}`, 'A\nB\n'); | 20 | 20 | expect('--no-deprecation', 'B\n'); | 21 | 21 | expect('--no-warnings', 'B\n'); | | 22 | +expect('--no_warnings', 'B\n'); | 22 | 23 | expect('--trace-warnings', 'B\n'); | 23 | 24 | expect('--redirect-warnings=_', 'B\n'); | 24 | 25 | expect('--trace-deprecation', 'B\n'); | |
Collapse file Copy file name to clipboardExpand all lines: test/parallel/test-pending-deprecation.js +8Lines changed: 8 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -36,6 +36,14 @@ switch (process.argv[2]) { | 36 | 36 | assert.strictEqual(code, 0, message('--pending-deprecation')); | 37 | 37 | })); | 38 | 38 |
| | 39 | + // Test the --pending_deprecation command line switch. | | 40 | + fork(__filename, ['switch'], { | | 41 | + execArgv: ['--pending_deprecation'], | | 42 | + silent: true | | 43 | + }).on('exit', common.mustCall((code) => { | | 44 | + assert.strictEqual(code, 0, message('--pending_deprecation')); | | 45 | + })); | | 46 | + | 39 | 47 | // Test the NODE_PENDING_DEPRECATION environment var. | 40 | 48 | fork(__filename, ['env'], { | 41 | 49 | env: Object.assign({}, process.env, { NODE_PENDING_DEPRECATION: 1 }), | |
Collapse file Copy file name to clipboardExpand all lines: test/parallel/test-process-env-allowed-flags.js +1-1Lines changed: 1 addition & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -19,10 +19,10 @@ require('../common'); | 19 | 19 | ].concat(process.config.variables.v8_enable_inspector ? [ | 20 | 20 | '--inspect-brk', | 21 | 21 | 'inspect-brk', | | 22 | + '--inspect_brk', | 22 | 23 | ] : []); | 23 | 24 |
| 24 | 25 | const badFlags = [ | 25 | | - '--inspect_brk', | 26 | 26 | 'INSPECT-BRK', | 27 | 27 | '--INSPECT-BRK', | 28 | 28 | '--r', | |
|