Skip to content

Commit cb4b2d3

Browse files
committed
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. Refs: #43397 Continues the work from #61962
1 parent e42dbef commit cb4b2d3

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

doc/api/cli.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -997,6 +997,9 @@ changes:
997997
Evaluate the following argument as JavaScript. The modules which are
998998
predefined in the REPL can also be used in `script`.
999999

1000+
If `script` starts with `-`, pass it using `=` (for example,
1001+
`node --print --eval=-42`) so it is parsed as the value of `--eval`.
1002+
10001003
On Windows, using `cmd.exe` a single quote will not work correctly because it
10011004
only recognizes double `"` for quoting. In Powershell or Git bash, both `'`
10021005
and `"` are usable.

test/parallel/test-cli-eval.js

Lines 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)