From 3d22dbf27bfb3cb681879784d2a80ac7246fa568 Mon Sep 17 00:00:00 2001 From: Ben Noordhuis Date: Thu, 1 Dec 2011 17:21:00 +0100 Subject: [PATCH] cli: add -p switch, print result of --eval --- src/node.cc | 12 +++++++++++- src/node.js | 4 ++-- test/simple/test-cli-eval.js | 15 +++++++++++++++ 3 files changed, 28 insertions(+), 3 deletions(-) diff --git a/src/node.cc b/src/node.cc index b67f810197..fbe1ad0e40 100644 --- a/src/node.cc +++ b/src/node.cc @@ -115,6 +115,7 @@ static Persistent uncaught_exception_symbol; static Persistent emit_symbol; +static bool print_eval = false; static char *eval_string = NULL; static int option_end_index = 0; static bool use_debug_agent = false; @@ -1987,6 +1988,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_eval"), Boolean::New(print_eval)); } size_t size = 2*PATH_MAX; @@ -2127,6 +2129,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" @@ -2170,13 +2173,20 @@ 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); } + if (arg[1] == 'p') { + print_eval = true; + } argv[i] = const_cast(""); eval_string = argv[++i]; + } else if (strcmp(arg, "--print") == 0 || strcmp(arg, "-p") == 0) { + print_eval = true; + argv[i] = const_cast(""); } 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 2d409eb534..53e0c489c6 100644 --- a/src/node.js +++ b/src/node.js @@ -77,8 +77,8 @@ var module = new Module('eval'); module.filename = path.join(cwd, 'eval'); module.paths = Module._nodeModulePaths(cwd); - module._compile('eval(process._eval)', 'eval'); - + var result = module._compile('return eval(process._eval)', 'eval'); + if (process._print_eval) console.log(result); } else if (process.argv[1]) { // make process.argv[1] into a full path var path = NativeModule.require('path'); diff --git a/test/simple/test-cli-eval.js b/test/simple/test-cli-eval.js index 98e8dc90c2..4c6b518b23 100644 --- a/test/simple/test-cli-eval.js +++ b/test/simple/test-cli-eval.js @@ -46,6 +46,21 @@ child.exec(nodejs + ' --eval "console.error(42)"', assert.equal(stderr, '42\n'); }); +// assert that nothing is written to stdout +['--print --eval', '-p -e', '-pe'].forEach(function(s) { + var cmd = nodejs + ' ' + s + ' '; + + child.exec(cmd + '42', + function(err, stdout, stderr) { + assert.equal(stdout, '42\n'); + }); + + child.exec(cmd + "'[]'", + function(err, stdout, stderr) { + assert.equal(stdout, '[]\n'); + }); +}); + // assert that module loading works child.exec(nodejs + ' --eval "require(\'' + filename + '\')"', function(status, stdout, stderr) {