Skip to content

Commit 0e7c2f9

Browse files
author
oldskytree
committed
fix(locator): fix argv parsing
1 parent b716e08 commit 0e7c2f9

1 file changed

Lines changed: 11 additions & 6 deletions

File tree

lib/locator.js

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,28 @@
11
const _ = require('lodash');
22

3-
module.exports = function({options, env, argv, envPrefix = '', cliPrefix = '--'}) {
4-
argv = argv.reduce(function(argv, arg) {
5-
if (!_.includes(arg, '=')) {
3+
function parseArgv(argv, cliPrefix) {
4+
return argv.reduce(function(argv, arg) {
5+
if (!arg.startsWith(cliPrefix) || !_.includes(arg, '=')) {
66
return argv.concat(arg);
77
}
8+
89
const parts = arg.split('=');
910
const option = parts[0];
1011
const value = parts.slice(1).join('=');
12+
1113
return argv.concat(option, value);
1214
}, []);
15+
}
16+
17+
module.exports = function({options, env, argv, envPrefix = '', cliPrefix = '--'}) {
18+
const parsedArgv = parseArgv(argv, cliPrefix);
1319

1420
function getNested(option, {namePrefix, envPrefix, cliPrefix}) {
1521
return (subKey) => {
1622
const envName = envPrefix + _.snakeCase(subKey);
1723
const cliFlag = cliPrefix + _.kebabCase(subKey);
1824

19-
const argIndex = argv.lastIndexOf(cliFlag);
25+
const argIndex = parsedArgv.lastIndexOf(cliFlag);
2026
const subOption = _.get(option, subKey);
2127
const newName = namePrefix ? `${namePrefix}.${subKey}` : subKey;
2228

@@ -26,7 +32,7 @@ module.exports = function({options, env, argv, envPrefix = '', cliPrefix = '--'}
2632
parent: namePrefix,
2733
option: subOption,
2834
envVar: env[envName],
29-
cliOption: argIndex > -1 ? argv[argIndex + 1] : undefined
35+
cliOption: argIndex > -1 ? parsedArgv[argIndex + 1] : undefined
3036
},
3137
{
3238
namePrefix: newName,
@@ -64,4 +70,3 @@ module.exports = function({options, env, argv, envPrefix = '', cliPrefix = '--'}
6470
}
6571
);
6672
};
67-

0 commit comments

Comments
 (0)