Skip to content

Commit cb0ee1a

Browse files
authored
Merge pull request #21 from gemini-testing/INFRADUTY-20011
fix(locator): fix argv parsing
2 parents b716e08 + e06ce4d commit cb0ee1a

2 files changed

Lines changed: 23 additions & 8 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-

test/locator.js

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ describe('locator', () => {
9999
assert.propertyVal(childPointer, 'cliOption', 'cli value');
100100
});
101101

102-
it('should return cli option set with --option=value syntax', () => {
102+
it('should return cli option set with --option="cli value"', () => {
103103
const pointer = locatorWithArgv([
104104
'--option=cli value'
105105
]);
@@ -108,7 +108,7 @@ describe('locator', () => {
108108
assert.propertyVal(childPointer, 'cliOption', 'cli value');
109109
});
110110

111-
it('should allow to have = sign inside option set with --option=value syntax', () => {
111+
it('should allow to have = sign inside option set with --option="cli=value"', () => {
112112
const pointer = locatorWithArgv([
113113
'--option=cli=value'
114114
]);
@@ -117,6 +117,16 @@ describe('locator', () => {
117117
assert.propertyVal(childPointer, 'cliOption', 'cli=value');
118118
});
119119

120+
it('should allow to have = sign inside option set with --option "cli=value"', () => {
121+
const pointer = locatorWithArgv([
122+
'--option',
123+
'cli=value'
124+
]);
125+
const childPointer = pointer.nested('option');
126+
127+
assert.propertyVal(childPointer, 'cliOption', 'cli=value');
128+
});
129+
120130
it('should use last value of an option', () => {
121131
const pointer = locatorWithArgv([
122132
'--option=first',

0 commit comments

Comments
 (0)