Skip to content

Commit c408a3b

Browse files
jBarzMylesBorins
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 Backport-PR-URL: #11013 Reviewed-By: Sam Roberts <vieuxtech@gmail.com> Reviewed-By: Roman Reiss <me@silverwind.io> Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Jeremiah Senkpiel <fishrock123@rocketmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
1 parent b7ca748 commit c408a3b

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

@@ -251,6 +251,15 @@ added: v0.11.15
251251

252252
Specify ICU data load path. (overrides `NODE_ICU_DATA`)
253253

254+
### `--`
255+
<!-- YAML
256+
added: REPLACEME
257+
-->
258+
259+
Indicate the end of node options. Pass the rest of the arguments to the script.
260+
If no script filename or eval/print script is supplied prior to this, then
261+
the next argument will be used as a script filename.
262+
254263
## Environment Variables
255264

256265
### `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
@@ -175,6 +176,13 @@ used to enable FIPS-compliant crypto if Node.js is built with
175176
.BR \-\-icu\-data\-dir =\fIfile\fR
176177
Specify ICU data load path. (overrides \fBNODE_ICU_DATA\fR)
177178

179+
.TP
180+
.BR \-\-\fR
181+
Indicate the end of node options. Pass the rest of the arguments to the script.
182+
183+
If no script filename or eval/print script is supplied prior to this, then
184+
the next argument will be used as a script filename.
185+
178186
.SH ENVIRONMENT VARIABLES
179187

180188
.TP

src/node.cc

+3
Original file line numberDiff line numberDiff line change
@@ -3860,6 +3860,9 @@ static void ParseArgs(int* argc,
38603860
} else if (strcmp(arg, "--expose-internals") == 0 ||
38613861
strcmp(arg, "--expose_internals") == 0) {
38623862
// consumed in js
3863+
} else if (strcmp(arg, "--") == 0) {
3864+
index += 1;
3865+
break;
38633866
} else {
38643867
// V8 option. Pass through as-is.
38653868
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);
@@ -163,3 +168,38 @@ child.exec(`${nodejs} --use-strict -p process.execArgv`,
163168
});
164169
proc.send('ping');
165170
}
171+
172+
[ '-arg1',
173+
'-arg1 arg2 --arg3',
174+
'--',
175+
'arg1 -- arg2',
176+
].forEach(function(args) {
177+
178+
// Ensure that arguments are successfully passed to eval.
179+
const opt = ' --eval "console.log(process.argv.slice(1).join(\' \'))"';
180+
const cmd = `${nodejs}${opt} -- ${args}`;
181+
child.exec(cmd, common.mustCall(function(err, stdout, stderr) {
182+
assert.strictEqual(stdout, args + '\n');
183+
assert.strictEqual(stderr, '');
184+
assert.strictEqual(err, null);
185+
}));
186+
187+
// Ensure that arguments are successfully passed to print.
188+
const popt = ' --print "process.argv.slice(1).join(\' \')"';
189+
const pcmd = `${nodejs}${popt} -- ${args}`;
190+
child.exec(pcmd, common.mustCall(function(err, stdout, stderr) {
191+
assert.strictEqual(stdout, args + '\n');
192+
assert.strictEqual(stderr, '');
193+
assert.strictEqual(err, null);
194+
}));
195+
196+
// Ensure that arguments are successfully passed to a script.
197+
// The first argument after '--' should be interpreted as a script
198+
// filename.
199+
const filecmd = `${nodejs} -- ${__filename} ${args}`;
200+
child.exec(filecmd, common.mustCall(function(err, stdout, stderr) {
201+
assert.strictEqual(stdout, args + '\n');
202+
assert.strictEqual(stderr, '');
203+
assert.strictEqual(err, null);
204+
}));
205+
});

0 commit comments

Comments
 (0)