Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit a035bd5

Browse filesBrowse files
kovanjorgitin02
authored andcommitted
doc,test: clarify --eval syntax for leading '-' scripts
If the script passed to --eval starts with a hyphen, the CLI parser treats it as another option flag. Document the --eval=<script> form as the workaround and add tests for --eval=-42, --eval=-0, and the -e -p missing-argument error. Co-authored-by: jorge guerrero <grrr.jrg@gmail.com> Refs: #43397 Refs: #61962 PR-URL: #62244 Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com> Reviewed-By: Anna Henningsen <anna@addaleax.net>
1 parent 7b28fb9 commit a035bd5
Copy full SHA for a035bd5

2 files changed

+23Lines changed: 23 additions & 0 deletions

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎doc/api/cli.md‎

Copy file name to clipboardExpand all lines: doc/api/cli.md
+3Lines changed: 3 additions & 0 deletions
  • Display the source diff
  • Display the rich diff
Original file line numberDiff line numberDiff line change
@@ -943,6 +943,9 @@ changes:
943943
Evaluate the following argument as JavaScript. The modules which are
944944
predefined in the REPL can also be used in `script`.
945945

946+
If `script` starts with `-`, pass it using `=` (for example,
947+
`node --print --eval=-42`) so it is parsed as the value of `--eval`.
948+
946949
On Windows, using `cmd.exe` a single quote will not work correctly because it
947950
only recognizes double `"` for quoting. In Powershell or Git bash, both `'`
948951
and `"` are usable.
Collapse file

‎test/parallel/test-cli-eval.js‎

Copy file name to clipboardExpand all lines: test/parallel/test-cli-eval.js
+20Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,26 @@ child.exec(...common.escapePOSIXShell`"${process.execPath}" -p "\\-42"`, common.
115115
assert.strictEqual(stderr, '');
116116
}));
117117

118+
// Eval expressions that start with '-' can be passed with '='.
119+
child.exec(...common.escapePOSIXShell`"${process.execPath}" --print --eval=-42`, common.mustSucceed((stdout, stderr) => {
120+
assert.strictEqual(stdout, '-42\n');
121+
assert.strictEqual(stderr, '');
122+
}));
123+
124+
// Edge case: negative zero should preserve its sign when printed.
125+
child.exec(...common.escapePOSIXShell`"${process.execPath}" --print --eval=-0`, common.mustSucceed((stdout, stderr) => {
126+
assert.strictEqual(stdout, '-0\n');
127+
assert.strictEqual(stderr, '');
128+
}));
129+
130+
// Nearby-path safety: option-looking values should still be rejected with -e.
131+
child.exec(...common.escapePOSIXShell`"${process.execPath}" -e -p`, common.mustCall((err, stdout, stderr) => {
132+
assert.strictEqual(err.code, 9);
133+
assert.strictEqual(stdout, '');
134+
assert.strictEqual(stderr.trim(),
135+
`${process.execPath}: -e requires an argument`);
136+
}));
137+
118138
// Long output should not be truncated.
119139
child.exec(...common.escapePOSIXShell`"${process.execPath}" -p "'1'.repeat(1e5)"`, common.mustSucceed((stdout, stderr) => {
120140
assert.strictEqual(stdout, `${'1'.repeat(1e5)}\n`);

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.