Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

cli: don't print result of --eval unless -p or --print is given

Fixes #572.
  • Loading branch information...
commit 00a505d01664e5592ceb68601074bedeb76dff48 1 parent 216829e
Ben Noordhuis authored

Showing 3 changed files with 34 additions and 6 deletions. Show diff stats Hide diff stats

  1. +10 1 src/node.cc
  2. +4 1 src/node.js
  3. +20 4 test/simple/test-cli-eval.js
11 src/node.cc
@@ -111,6 +111,7 @@ static Persistent<String> uncaught_exception_symbol;
111 111 static Persistent<String> emit_symbol;
112 112
113 113
  114 +static int eval_print = 0;
114 115 static char *eval_string = NULL;
115 116 static int option_end_index = 0;
116 117 static bool use_debug_agent = false;
@@ -2120,6 +2121,7 @@ Handle<Object> SetupProcessObject(int argc, char *argv[]) {
2120 2121 // -e, --eval
2121 2122 if (eval_string) {
2122 2123 process->Set(String::NewSymbol("_eval"), String::New(eval_string));
  2124 + process->Set(String::NewSymbol("_print"), Boolean::New(eval_print));
2123 2125 }
2124 2126
2125 2127 size_t size = 2*PATH_MAX;
@@ -2252,6 +2254,7 @@ static void PrintHelp() {
2252 2254 "Options:\n"
2253 2255 " -v, --version print node's version\n"
2254 2256 " -e, --eval script evaluate script\n"
  2257 + " -p, --print print result of --eval\n"
2255 2258 " --v8-options print v8 command line options\n"
2256 2259 " --vars print various compiled-in variables\n"
2257 2260 " --max-stack-size=val set max v8 stack size (bytes)\n"
@@ -2295,13 +2298,19 @@ static void ParseArgs(int argc, char **argv) {
2295 2298 } else if (strcmp(arg, "--help") == 0 || strcmp(arg, "-h") == 0) {
2296 2299 PrintHelp();
2297 2300 exit(0);
2298   - } else if (strcmp(arg, "--eval") == 0 || strcmp(arg, "-e") == 0) {
  2301 + } else if (strcmp(arg, "--eval") == 0 ||
  2302 + strcmp(arg, "-e") == 0 ||
  2303 + strcmp(arg, "-pe") == 0) {
2299 2304 if (argc <= i + 1) {
2300 2305 fprintf(stderr, "Error: --eval requires an argument\n");
2301 2306 exit(1);
2302 2307 }
  2308 + eval_print |= ('p' == argv[i][1]); // node -pe 42 # prints 42
2303 2309 argv[i] = const_cast<char*>("");
2304 2310 eval_string = argv[++i];
  2311 + } else if (strcmp(arg, "--print") == 0 || strcmp(arg, "-p") == 0) {
  2312 + argv[i] = const_cast<char*>("");
  2313 + eval_print = 1;
2305 2314 } else if (strcmp(arg, "--v8-options") == 0) {
2306 2315 argv[i] = const_cast<char*>("--help");
2307 2316 } else if (argv[i][0] != '-') {
5 src/node.js
@@ -80,8 +80,11 @@
80 80 var module = new Module('eval');
81 81 module.filename = path.join(cwd, 'eval');
82 82 module.paths = Module._nodeModulePaths(cwd);
  83 +
83 84 var rv = module._compile('return eval(process._eval)', 'eval');
84   - console.log(rv);
  85 + if (process._print) {
  86 + console.log(rv);
  87 + }
85 88
86 89 } else if (process.argv[1]) {
87 90 // make process.argv[1] into a full path
24 test/simple/test-cli-eval.js
@@ -31,25 +31,41 @@ if (module.parent) {
31 31 }
32 32
33 33 // assert that the result of the final expression is written to stdout
34   -child.exec(nodejs + ' --eval "1337; 42"',
  34 +child.exec(nodejs + ' --print --eval "1337; 42"',
35 35 function(err, stdout, stderr) {
36 36 assert.equal(parseInt(stdout), 42);
37 37 });
38 38
39 39 // assert that module loading works
40   -child.exec(nodejs + ' --eval "require(\'' + __filename + '\')"',
  40 +child.exec(nodejs + ' --print --eval "require(\'' + __filename + '\')"',
41 41 function(status, stdout, stderr) {
42 42 assert.equal(status.code, 42);
43 43 });
44 44
45 45 // module path resolve bug, regression test
46   -child.exec(nodejs + ' --eval "require(\'./test/simple/test-cli-eval.js\')"',
  46 +child.exec(nodejs + ' --print --eval "require(\'./test/simple/test-cli-eval.js\')"',
47 47 function(status, stdout, stderr) {
48 48 assert.equal(status.code, 42);
49 49 });
50 50
51 51 // empty program should do nothing
52   -child.exec(nodejs + ' -e ""', function(status, stdout, stderr) {
  52 +child.exec(nodejs + ' -p -e ""', function(status, stdout, stderr) {
53 53 assert.equal(stdout, 'undefined\n');
54 54 assert.equal(stderr, '');
55 55 });
  56 +
  57 +// this shouldn't print anything...
  58 +'-e|--eval'.split('|').forEach(function(args) {
  59 + child.exec(nodejs + ' ' + args + ' 42', function(status, stdout, stderr) {
  60 + assert.equal(stdout, '');
  61 + assert.equal(stderr, '');
  62 + });
  63 +});
  64 +
  65 +// ...while this should
  66 +'-pe|-p -e|--print --eval'.split('|').forEach(function(args) {
  67 + child.exec(nodejs + ' ' + args + ' 42', function(status, stdout, stderr) {
  68 + assert.equal(stdout, '42\n');
  69 + assert.equal(stderr, '');
  70 + });
  71 +});

0 comments on commit 00a505d

Please sign in to comment.
Something went wrong with that request. Please try again.