Skip to content

Commit 8ed13dc

Browse files
committed
util: fix parseArgs skipping positional arg with --eval and --print
1 parent 7fd3688 commit 8ed13dc

File tree

2 files changed

+27
-10
lines changed

2 files changed

+27
-10
lines changed

lib/internal/util/parse_args/parse_args.js

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
const {
44
ArrayPrototypeForEach,
5-
ArrayPrototypeIncludes,
65
ArrayPrototypeMap,
76
ArrayPrototypePush,
87
ArrayPrototypePushApply,
@@ -54,20 +53,16 @@ const {
5453
kEmptyObject,
5554
} = require('internal/util');
5655

56+
const { getOptionValue } = require('internal/options');
5757

5858
function getMainArgs() {
59-
// Work out where to slice process.argv for user supplied arguments.
60-
61-
// Check node options for scenarios where user CLI args follow executable.
62-
const execArgv = process.execArgv;
63-
if (ArrayPrototypeIncludes(execArgv, '-e') ||
64-
ArrayPrototypeIncludes(execArgv, '--eval') ||
65-
ArrayPrototypeIncludes(execArgv, '-p') ||
66-
ArrayPrototypeIncludes(execArgv, '--print')) {
59+
// -p / --print internally sets --eval, so this works for all cases
60+
const evalValue = getOptionValue('--eval');
61+
62+
if (evalValue.length !== 0) {
6763
return ArrayPrototypeSlice(process.argv, 1);
6864
}
6965

70-
// Normally first two arguments are executable and script, then CLI arguments
7166
return ArrayPrototypeSlice(process.argv, 2);
7267
}
7368

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
'use strict';
2+
const common = require('../common');
3+
const assert = require('assert');
4+
const { getMainArgs } = require('../../lib/internal/util/parse_args/parse_args');
5+
6+
const originalArgv = process.argv;
7+
const originalExecArgv = process.execArgv;
8+
9+
common.mustCall(() => {
10+
process.argv = ['node', '--eval=code', 'arg1', 'arg2'];
11+
process.execArgv = ['--eval=code'];
12+
const evalArgs = getMainArgs();
13+
assert.deepStrictEqual(evalArgs, ['--eval=code', 'arg1', 'arg2']);
14+
15+
process.argv = ['node', 'script.js', 'arg1', 'arg2'];
16+
process.execArgv = [];
17+
const normalArgs = getMainArgs();
18+
assert.deepStrictEqual(normalArgs, ['script.js', 'arg1', 'arg2']);
19+
})();
20+
21+
process.argv = originalArgv;
22+
process.execArgv = originalExecArgv;

0 commit comments

Comments
 (0)