Skip to content

Commit

Permalink
src: throw when -c and -e are used simultaneously
Browse files Browse the repository at this point in the history
The -c flag ("check script syntax") and -e flag ("evaluate given code")
have contradictory meanings. Make them mutually exclusive by throwing
when both of them are provided.

Fixes: #11680
PR-URL: #11689
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Richard Lau <riclau@uk.ibm.com>
  • Loading branch information
not-an-aardvark committed Apr 4, 2017
1 parent 3209a8e commit a5f91ab
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
6 changes: 6 additions & 0 deletions src/node.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3816,6 +3816,12 @@ static void ParseArgs(int* argc,
}
#endif

if (eval_string != nullptr && syntax_check_only) {
fprintf(stderr,
"%s: either --check or --eval can be used, not both\n", argv[0]);
exit(9);
}

// Copy remaining arguments.
const unsigned int args_left = nargs - index;
memcpy(new_argv + new_argc, argv + index, args_left * sizeof(*argv));
Expand Down
17 changes: 16 additions & 1 deletion test/parallel/test-cli-syntax.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ syntaxArgs.forEach(function(args) {
assert.strictEqual(c.status, 0, 'code === ' + c.status);
});

// should should throw if code piped from stdin with --check has bad syntax
// should throw if code piped from stdin with --check has bad syntax
// loop each possible option, `-c` or `--check`
syntaxArgs.forEach(function(args) {
const stdin = 'var foo bar;';
Expand All @@ -117,3 +117,18 @@ syntaxArgs.forEach(function(args) {

assert.strictEqual(c.status, 1, 'code === ' + c.status);
});

// should throw if -c and -e flags are both passed
['-c', '--check'].forEach(function(checkFlag) {
['-e', '--eval'].forEach(function(evalFlag) {
const args = [checkFlag, evalFlag, 'foo'];
const c = spawnSync(node, args, {encoding: 'utf8'});

assert.strictEqual(
c.stderr,
`${node}: either --check or --eval can be used, not both\n`
);

assert.strictEqual(c.status, 9, 'code === ' + c.status);
});
});

0 comments on commit a5f91ab

Please sign in to comment.