Permalink
Browse files

src: throw when -c and -e are used simultaneously

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 Mar 30, 2017
1 parent 3209a8e commit a5f91ab230c574d561780b6867d00f06fcc1e4de
Showing with 22 additions and 1 deletion.
  1. +6 −0 src/node.cc
  2. +16 −1 test/parallel/test-cli-syntax.js
@@ -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));
@@ -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;';
@@ -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.