Skip to content

Commit a03981d

Browse files
committed
"--" after "-e <script>" means end-of-options
When the double dash "--" appears after "-e <script>" on the command line, it indicates the end of options and the beginning of positional parameters for the script.
1 parent 3e9c8ef commit a03981d

File tree

4 files changed

+61
-1
lines changed

4 files changed

+61
-1
lines changed

doc/api/cli.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ To view this documentation as a manual page in your terminal, run `man node`.
1010

1111
## Synopsis
1212

13-
`node [options] [v8 options] [script.js | -e "script"] [arguments]`
13+
`node [options] [v8 options] [script.js | -e "script"] [--] [arguments]`
1414

1515
`node debug [script.js | -e "script" | <host>:<port>] …`
1616

@@ -265,6 +265,15 @@ added: v0.11.15
265265

266266
Specify ICU data load path. (overrides `NODE_ICU_DATA`)
267267

268+
### `--`
269+
<!-- YAML
270+
added: REPLACEME
271+
-->
272+
273+
Indicate the end of node options. Pass the rest of the arguments to the script.
274+
If no script filename or eval/print script is supplied prior to this, then
275+
the next argument will be used as a script filename.
276+
268277
## Environment Variables
269278

270279
### `NODE_DEBUG=module[,…]`

doc/node.1

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ node \- Server-side JavaScript runtime
3737
.RI [ script.js \ |
3838
.B -e
3939
.RI \&" script \&"]
40+
.B [--]
4041
.RI [ arguments ]
4142
.br
4243
.B node debug
@@ -184,6 +185,13 @@ used to enable FIPS-compliant crypto if Node.js is built with
184185
.BR \-\-icu\-data\-dir =\fIfile\fR
185186
Specify ICU data load path. (overrides \fBNODE_ICU_DATA\fR)
186187

188+
.TP
189+
.BR \-\-\fR
190+
Indicate the end of node options. Pass the rest of the arguments to the script.
191+
192+
If no script filename or eval/print script is supplied prior to this, then
193+
the next argument will be used as a script filename.
194+
187195
.SH ENVIRONMENT VARIABLES
188196

189197
.TP

src/node.cc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3697,6 +3697,9 @@ static void ParseArgs(int* argc,
36973697
} else if (strcmp(arg, "--expose-internals") == 0 ||
36983698
strcmp(arg, "--expose_internals") == 0) {
36993699
// consumed in js
3700+
} else if (strcmp(arg, "--") == 0) {
3701+
index += 1;
3702+
break;
37003703
} else {
37013704
// V8 option. Pass through as-is.
37023705
new_v8_argv[new_v8_argc] = arg;

test/parallel/test-cli-eval.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@ const child = require('child_process');
1212
const path = require('path');
1313
const nodejs = `"${process.execPath}"`;
1414

15+
if (process.argv.length > 2) {
16+
console.log(process.argv.slice(2).join(' '));
17+
process.exit(0);
18+
}
19+
1520
// Assert that nothing is written to stdout.
1621
child.exec(`${nodejs} --eval 42`, common.mustCall((err, stdout, stderr) => {
1722
assert.ifError(err);
@@ -133,3 +138,38 @@ child.exec(`${nodejs} --use-strict -p process.execArgv`,
133138
assert.strictEqual(proc.stderr, '');
134139
assert.strictEqual(proc.stdout, 'start\nbeforeExit\nexit\n');
135140
}
141+
142+
[ '-arg1',
143+
'-arg1 arg2 --arg3',
144+
'--',
145+
'arg1 -- arg2',
146+
].forEach(function(args) {
147+
148+
// Ensure that arguments are successfully passed to eval.
149+
const opt = ' --eval "console.log(process.argv.slice(1).join(\' \'))"';
150+
const cmd = `${nodejs}${opt} -- ${args}`;
151+
child.exec(cmd, common.mustCall(function(err, stdout, stderr) {
152+
assert.strictEqual(stdout, args + '\n');
153+
assert.strictEqual(stderr, '');
154+
assert.strictEqual(err, null);
155+
}));
156+
157+
// Ensure that arguments are successfully passed to print.
158+
const popt = ' --print "process.argv.slice(1).join(\' \')"';
159+
const pcmd = `${nodejs}${popt} -- ${args}`;
160+
child.exec(pcmd, common.mustCall(function(err, stdout, stderr) {
161+
assert.strictEqual(stdout, args + '\n');
162+
assert.strictEqual(stderr, '');
163+
assert.strictEqual(err, null);
164+
}));
165+
166+
// Ensure that arguments are successfully passed to a script.
167+
// The first argument after '--' should be interpreted as a script
168+
// filename.
169+
const filecmd = `${nodejs} -- ${__filename} ${args}`;
170+
child.exec(filecmd, common.mustCall(function(err, stdout, stderr) {
171+
assert.strictEqual(stdout, args + '\n');
172+
assert.strictEqual(stderr, '');
173+
assert.strictEqual(err, null);
174+
}));
175+
});

0 commit comments

Comments
 (0)