From 00a505d01664e5592ceb68601074bedeb76dff48 Mon Sep 17 00:00:00 2001 From: Ben Noordhuis Date: Mon, 25 Jul 2011 15:23:30 +0200 Subject: [PATCH] cli: don't print result of --eval unless -p or --print is given Fixes #572. --- src/node.cc | 11 ++++++++++- src/node.js | 5 ++++- test/simple/test-cli-eval.js | 24 ++++++++++++++++++++---- 3 files changed, 34 insertions(+), 6 deletions(-) diff --git a/src/node.cc b/src/node.cc index 8ce02183be3..116085a877f 100644 --- a/src/node.cc +++ b/src/node.cc @@ -111,6 +111,7 @@ static Persistent uncaught_exception_symbol; static Persistent emit_symbol; +static int eval_print = 0; static char *eval_string = NULL; static int option_end_index = 0; static bool use_debug_agent = false; @@ -2120,6 +2121,7 @@ Handle SetupProcessObject(int argc, char *argv[]) { // -e, --eval if (eval_string) { process->Set(String::NewSymbol("_eval"), String::New(eval_string)); + process->Set(String::NewSymbol("_print"), Boolean::New(eval_print)); } size_t size = 2*PATH_MAX; @@ -2252,6 +2254,7 @@ static void PrintHelp() { "Options:\n" " -v, --version print node's version\n" " -e, --eval script evaluate script\n" + " -p, --print print result of --eval\n" " --v8-options print v8 command line options\n" " --vars print various compiled-in variables\n" " --max-stack-size=val set max v8 stack size (bytes)\n" @@ -2295,13 +2298,19 @@ static void ParseArgs(int argc, char **argv) { } else if (strcmp(arg, "--help") == 0 || strcmp(arg, "-h") == 0) { PrintHelp(); exit(0); - } else if (strcmp(arg, "--eval") == 0 || strcmp(arg, "-e") == 0) { + } else if (strcmp(arg, "--eval") == 0 || + strcmp(arg, "-e") == 0 || + strcmp(arg, "-pe") == 0) { if (argc <= i + 1) { fprintf(stderr, "Error: --eval requires an argument\n"); exit(1); } + eval_print |= ('p' == argv[i][1]); // node -pe 42 # prints 42 argv[i] = const_cast(""); eval_string = argv[++i]; + } else if (strcmp(arg, "--print") == 0 || strcmp(arg, "-p") == 0) { + argv[i] = const_cast(""); + eval_print = 1; } else if (strcmp(arg, "--v8-options") == 0) { argv[i] = const_cast("--help"); } else if (argv[i][0] != '-') { diff --git a/src/node.js b/src/node.js index cecc240e426..ee5ba5b0819 100644 --- a/src/node.js +++ b/src/node.js @@ -80,8 +80,11 @@ var module = new Module('eval'); module.filename = path.join(cwd, 'eval'); module.paths = Module._nodeModulePaths(cwd); + var rv = module._compile('return eval(process._eval)', 'eval'); - console.log(rv); + if (process._print) { + console.log(rv); + } } else if (process.argv[1]) { // make process.argv[1] into a full path diff --git a/test/simple/test-cli-eval.js b/test/simple/test-cli-eval.js index 3340d013843..5efc55ac20e 100644 --- a/test/simple/test-cli-eval.js +++ b/test/simple/test-cli-eval.js @@ -31,25 +31,41 @@ if (module.parent) { } // assert that the result of the final expression is written to stdout -child.exec(nodejs + ' --eval "1337; 42"', +child.exec(nodejs + ' --print --eval "1337; 42"', function(err, stdout, stderr) { assert.equal(parseInt(stdout), 42); }); // assert that module loading works -child.exec(nodejs + ' --eval "require(\'' + __filename + '\')"', +child.exec(nodejs + ' --print --eval "require(\'' + __filename + '\')"', function(status, stdout, stderr) { assert.equal(status.code, 42); }); // module path resolve bug, regression test -child.exec(nodejs + ' --eval "require(\'./test/simple/test-cli-eval.js\')"', +child.exec(nodejs + ' --print --eval "require(\'./test/simple/test-cli-eval.js\')"', function(status, stdout, stderr) { assert.equal(status.code, 42); }); // empty program should do nothing -child.exec(nodejs + ' -e ""', function(status, stdout, stderr) { +child.exec(nodejs + ' -p -e ""', function(status, stdout, stderr) { assert.equal(stdout, 'undefined\n'); assert.equal(stderr, ''); }); + +// this shouldn't print anything... +'-e|--eval'.split('|').forEach(function(args) { + child.exec(nodejs + ' ' + args + ' 42', function(status, stdout, stderr) { + assert.equal(stdout, ''); + assert.equal(stderr, ''); + }); +}); + +// ...while this should +'-pe|-p -e|--print --eval'.split('|').forEach(function(args) { + child.exec(nodejs + ' ' + args + ' 42', function(status, stdout, stderr) { + assert.equal(stdout, '42\n'); + assert.equal(stderr, ''); + }); +});