Skip to content

Commit

Permalink
src: support "--" after "-e" as end-of-options
Browse files Browse the repository at this point in the history
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 <[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]>
  • Loading branch information
jBarz authored and MylesBorins committed May 16, 2017
1 parent 689e678 commit 0ff6394
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 1 deletion.
11 changes: 10 additions & 1 deletion doc/api/cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ To view this documentation as a manual page in your terminal, run `man node`.

## Synopsis

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

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

Expand Down Expand Up @@ -251,6 +251,15 @@ added: v0.11.15

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

### `--`
<!-- YAML
added: REPLACEME
-->

Indicate the end of node options. Pass the rest of the arguments to the script.
If no script filename or eval/print script is supplied prior to this, then
the next argument will be used as a script filename.

## Environment Variables

### `NODE_DEBUG=module[,…]`
Expand Down
8 changes: 8 additions & 0 deletions doc/node.1
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ node \- Server-side JavaScript runtime
.RI [ script.js \ |
.B -e
.RI \&" script \&"]
.B [--]
.RI [ arguments ]
.br
.B node debug
Expand Down Expand Up @@ -175,6 +176,13 @@ used to enable FIPS-compliant crypto if Node.js is built with
.BR \-\-icu\-data\-dir =\fIfile\fR
Specify ICU data load path. (overrides \fBNODE_ICU_DATA\fR)

.TP
.BR \-\-\fR
Indicate the end of node options. Pass the rest of the arguments to the script.

If no script filename or eval/print script is supplied prior to this, then
the next argument will be used as a script filename.

.SH ENVIRONMENT VARIABLES

.TP
Expand Down
3 changes: 3 additions & 0 deletions src/node.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3860,6 +3860,9 @@ static void ParseArgs(int* argc,
} else if (strcmp(arg, "--expose-internals") == 0 ||
strcmp(arg, "--expose_internals") == 0) {
// consumed in js
} else if (strcmp(arg, "--") == 0) {
index += 1;
break;
} else {
// V8 option. Pass through as-is.
new_v8_argv[new_v8_argc] = arg;
Expand Down
40 changes: 40 additions & 0 deletions test/parallel/test-cli-eval.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ const child = require('child_process');
const path = require('path');
const nodejs = `"${process.execPath}"`;

if (process.argv.length > 2) {
console.log(process.argv.slice(2).join(' '));
process.exit(0);
}

// Assert that nothing is written to stdout.
child.exec(`${nodejs} --eval 42`, common.mustCall((err, stdout, stderr) => {
assert.ifError(err);
Expand Down Expand Up @@ -163,3 +168,38 @@ child.exec(`${nodejs} --use-strict -p process.execArgv`,
});
proc.send('ping');
}

[ '-arg1',
'-arg1 arg2 --arg3',
'--',
'arg1 -- arg2',
].forEach(function(args) {

// Ensure that arguments are successfully passed to eval.
const opt = ' --eval "console.log(process.argv.slice(1).join(\' \'))"';
const cmd = `${nodejs}${opt} -- ${args}`;
child.exec(cmd, common.mustCall(function(err, stdout, stderr) {
assert.strictEqual(stdout, args + '\n');
assert.strictEqual(stderr, '');
assert.strictEqual(err, null);
}));

// Ensure that arguments are successfully passed to print.
const popt = ' --print "process.argv.slice(1).join(\' \')"';
const pcmd = `${nodejs}${popt} -- ${args}`;
child.exec(pcmd, common.mustCall(function(err, stdout, stderr) {
assert.strictEqual(stdout, args + '\n');
assert.strictEqual(stderr, '');
assert.strictEqual(err, null);
}));

// Ensure that arguments are successfully passed to a script.
// The first argument after '--' should be interpreted as a script
// filename.
const filecmd = `${nodejs} -- ${__filename} ${args}`;
child.exec(filecmd, common.mustCall(function(err, stdout, stderr) {
assert.strictEqual(stdout, args + '\n');
assert.strictEqual(stderr, '');
assert.strictEqual(err, null);
}));
});

0 comments on commit 0ff6394

Please sign in to comment.