Permalink
Browse files

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

Fixes #572.
  • Loading branch information...
1 parent 216829e commit 00a505d01664e5592ceb68601074bedeb76dff48 @bnoordhuis committed Jul 25, 2011
Showing with 34 additions and 6 deletions.
  1. +10 −1 src/node.cc
  2. +4 −1 src/node.js
  3. +20 −4 test/simple/test-cli-eval.js
View
@@ -111,6 +111,7 @@ static Persistent<String> uncaught_exception_symbol;
static Persistent<String> 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<Object> 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<char*>("");
eval_string = argv[++i];
+ } else if (strcmp(arg, "--print") == 0 || strcmp(arg, "-p") == 0) {
+ argv[i] = const_cast<char*>("");
+ eval_print = 1;
} else if (strcmp(arg, "--v8-options") == 0) {
argv[i] = const_cast<char*>("--help");
} else if (argv[i][0] != '-') {
View
@@ -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
@@ -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, '');
+ });
+});

0 comments on commit 00a505d

Please sign in to comment.