Skip to content

Commit 978acd1

Browse files
jBarzitaloacasas
authored andcommitted
src: support "--" after "-e" as 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. PR-URL: #10651 Reviewed-By: Sam Roberts <[email protected]> Reviewed-By: Roman Reiss <[email protected]> Reviewed-By: Ben Noordhuis <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Jeremiah Senkpiel <[email protected]> Reviewed-By: Colin Ihrig <[email protected]>
1 parent 6687b95 commit 978acd1

File tree

4 files changed

+61
-1
lines changed

4 files changed

+61
-1
lines changed

doc/api/cli.md

+10-1
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

@@ -269,6 +269,15 @@ added: v0.11.15
269269

270270
Specify ICU data load path. (overrides `NODE_ICU_DATA`)
271271

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

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

doc/node.1

+8
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
@@ -191,6 +192,13 @@ See \fBSSL_CERT_DIR\fR and \fBSSL_CERT_FILE\fR.
191192
.BR \-\-icu\-data\-dir =\fIfile\fR
192193
Specify ICU data load path. (overrides \fBNODE_ICU_DATA\fR)
193194

195+
.TP
196+
.BR \-\-\fR
197+
Indicate the end of node options. Pass the rest of the arguments to the script.
198+
199+
If no script filename or eval/print script is supplied prior to this, then
200+
the next argument will be used as a script filename.
201+
194202
.SH ENVIRONMENT VARIABLES
195203

196204
.TP

src/node.cc

+3
Original file line numberDiff line numberDiff line change
@@ -3688,6 +3688,9 @@ static void ParseArgs(int* argc,
36883688
} else if (strcmp(arg, "--expose-internals") == 0 ||
36893689
strcmp(arg, "--expose_internals") == 0) {
36903690
// consumed in js
3691+
} else if (strcmp(arg, "--") == 0) {
3692+
index += 1;
3693+
break;
36913694
} else {
36923695
// V8 option. Pass through as-is.
36933696
new_v8_argv[new_v8_argc] = arg;

test/parallel/test-cli-eval.js

+40
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)