Browse Source

cmd: support dash as stdin alias

PR-URL: https://github.com/nodejs/node/pull/13012
Reviewed-By: Jeremiah Senkpiel <fishrock123@rocketmail.com>
Reviewed-By: Refael Ackermann <refack@gmail.com>
Reviewed-By: Gibson Fahnestock <gibfahn@gmail.com>
v6
Ebrahim Byagowi 8 years ago
committed by Anna Henningsen
parent
commit
594b5d7b89
No known key found for this signature in database GPG Key ID: D8B9F5AEAE84E4CF
  1. 13
      doc/api/cli.md
  2. 2
      doc/api/synopsis.md
  3. 11
      doc/node.1
  4. 2
      lib/internal/bootstrap_node.js
  5. 6
      src/node.cc
  6. 17
      test/parallel/test-stdin-script-child-option.js
  7. 49
      test/parallel/test-stdin-script-child.js

13
doc/api/cli.md

@ -10,7 +10,7 @@ To view this documentation as a manual page in a terminal, run `man node`.
## Synopsis ## 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>] …` `node debug [script.js | -e "script" | <host>:<port>] …`
@ -345,6 +345,17 @@ added: v0.11.15
Specify ICU data load path. (overrides `NODE_ICU_DATA`) Specify ICU data load path. (overrides `NODE_ICU_DATA`)
### `-`
<!-- YAML
added: REPLACEME
-->
Alias for stdin, analogous to the use of - in other command line utilities,
meaning that the script will be read from stdin, and the rest of the options
are passed to that script.
### `--` ### `--`
<!-- YAML <!-- YAML
added: v7.5.0 added: v7.5.0

2
doc/api/synopsis.md

@ -2,7 +2,7 @@
<!--type=misc--> <!--type=misc-->
`node [options] [v8 options] [script.js | -e "script"] [arguments]` `node [options] [v8 options] [script.js | -e "script" | - ] [arguments]`
Please see the [Command Line Options][] document for information about Please see the [Command Line Options][] document for information about
different options and ways to run scripts with Node.js. different options and ways to run scripts with Node.js.

11
doc/node.1

@ -36,7 +36,10 @@ node \- Server-side JavaScript runtime
.RI [ v8\ options ] .RI [ v8\ options ]
.RI [ script.js \ | .RI [ script.js \ |
.B -e .B -e
.RI \&" script \&"] .RI \&" script \&"
.R |
.B -
.R ]
.B [--] .B [--]
.RI [ arguments ] .RI [ arguments ]
.br .br
@ -225,6 +228,12 @@ See \fBSSL_CERT_DIR\fR and \fBSSL_CERT_FILE\fR.
.BR \-\-icu\-data\-dir =\fIfile\fR .BR \-\-icu\-data\-dir =\fIfile\fR
Specify ICU data load path. (overrides \fBNODE_ICU_DATA\fR) Specify ICU data load path. (overrides \fBNODE_ICU_DATA\fR)
.TP
.BR \-\fR
Alias for stdin, analogous to the use of - in other command line utilities,
meaning that the script will be read from stdin, and the rest of the options
are passed to that script.
.TP .TP
.BR \-\-\fR .BR \-\-\fR
Indicate the end of node options. Pass the rest of the arguments to the script. Indicate the end of node options. Pass the rest of the arguments to the script.

2
lib/internal/bootstrap_node.js

@ -123,7 +123,7 @@
const internalModule = NativeModule.require('internal/module'); const internalModule = NativeModule.require('internal/module');
internalModule.addBuiltinLibsToObject(global); internalModule.addBuiltinLibsToObject(global);
evalScript('[eval]'); evalScript('[eval]');
} else if (process.argv[1]) { } else if (process.argv[1] && process.argv[1] !== '-') {
// make process.argv[1] into a full path // make process.argv[1] into a full path
const path = NativeModule.require('path'); const path = NativeModule.require('path');
process.argv[1] = path.resolve(process.argv[1]); process.argv[1] = path.resolve(process.argv[1]);

6
src/node.cc

@ -3592,7 +3592,7 @@ void LoadEnvironment(Environment* env) {
static void PrintHelp() { static void PrintHelp() {
// XXX: If you add an option here, please also add it to doc/node.1 and // XXX: If you add an option here, please also add it to doc/node.1 and
// doc/api/cli.md // doc/api/cli.md
printf("Usage: node [options] [ -e script | script.js ] [arguments]\n" printf("Usage: node [options] [ -e script | script.js | - ] [arguments]\n"
" node inspect script.js [arguments]\n" " node inspect script.js [arguments]\n"
"\n" "\n"
"Options:\n" "Options:\n"
@ -3604,6 +3604,8 @@ static void PrintHelp() {
" does not appear to be a terminal\n" " does not appear to be a terminal\n"
" -r, --require module to preload (option can be " " -r, --require module to preload (option can be "
"repeated)\n" "repeated)\n"
" - script read from stdin (default; "
"interactive mode if a tty)"
#if HAVE_INSPECTOR #if HAVE_INSPECTOR
" --inspect[=[host:]port] activate inspector on host:port\n" " --inspect[=[host:]port] activate inspector on host:port\n"
" (default: 127.0.0.1:9229)\n" " (default: 127.0.0.1:9229)\n"
@ -3913,6 +3915,8 @@ static void ParseArgs(int* argc,
} else if (strcmp(arg, "--expose-internals") == 0 || } else if (strcmp(arg, "--expose-internals") == 0 ||
strcmp(arg, "--expose_internals") == 0) { strcmp(arg, "--expose_internals") == 0) {
config_expose_internals = true; config_expose_internals = true;
} else if (strcmp(arg, "-") == 0) {
break;
} else if (strcmp(arg, "--") == 0) { } else if (strcmp(arg, "--") == 0) {
index += 1; index += 1;
break; break;

17
test/parallel/test-stdin-script-child-option.js

@ -0,0 +1,17 @@
'use strict';
const common = require('../common');
const assert = require('assert');
const expected = '--option-to-be-seen-on-child';
const { spawn } = require('child_process');
const child = spawn(process.execPath, ['-', expected], { stdio: 'pipe' });
child.stdin.end('console.log(process.argv[2])');
let actual = '';
child.stdout.setEncoding('utf8');
child.stdout.on('data', (chunk) => actual += chunk);
child.stdout.on('end', common.mustCall(() => {
assert.strictEqual(actual.trim(), expected);
}));

49
test/parallel/test-stdin-script-child.js

@ -2,31 +2,32 @@
const common = require('../common'); const common = require('../common');
const assert = require('assert'); const assert = require('assert');
const spawn = require('child_process').spawn; const { spawn } = require('child_process');
const child = spawn(process.execPath, [], { for (const args of [[], ['-']]) {
env: Object.assign(process.env, { const child = spawn(process.execPath, args, {
NODE_DEBUG: process.argv[2] env: Object.assign(process.env, {
}) NODE_DEBUG: process.argv[2]
}); })
const wanted = `${child.pid}\n`; });
let found = ''; const wanted = `${child.pid}\n`;
let found = '';
child.stdout.setEncoding('utf8'); child.stdout.setEncoding('utf8');
child.stdout.on('data', function(c) { child.stdout.on('data', function(c) {
found += c; found += c;
}); });
child.stderr.setEncoding('utf8'); child.stderr.setEncoding('utf8');
child.stderr.on('data', function(c) { child.stderr.on('data', function(c) {
console.error(`> ${c.trim().split(/\n/).join('\n> ')}`); console.error(`> ${c.trim().split(/\n/).join('\n> ')}`);
}); });
child.on('close', common.mustCall(function(c) { child.on('close', common.mustCall(function(c) {
assert.strictEqual(c, 0); assert.strictEqual(c, 0);
assert.strictEqual(found, wanted); assert.strictEqual(found, wanted);
console.log('ok'); }));
}));
setTimeout(function() { setTimeout(function() {
child.stdin.end('console.log(process.pid)'); child.stdin.end('console.log(process.pid)');
}, 1); }, 1);
}

Loading…
Cancel
Save