Skip to content

Commit

Permalink
coerce function for boolean/string/number types
Browse files Browse the repository at this point in the history
  • Loading branch information
juergba committed May 17, 2019
1 parent 8f2a35d commit ffe1967
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 17 deletions.
21 changes: 16 additions & 5 deletions lib/cli/options.js
Expand Up @@ -54,15 +54,26 @@ const configuration = Object.assign({}, YARGS_PARSER_CONFIG, {
});

/**
* This is a really fancy way to ensure unique values for `array`-type
* options.
* This is a really fancy way to:
* - ensure unique values for `array`-type options
* - use its array's last element for `boolean`/`number`/`string`- options given multiple times
* This is passed as the `coerce` option to `yargs-parser`
* @private
* @ignore
*/
const coerceOpts = types.array.reduce(
(acc, arg) => Object.assign(acc, {[arg]: v => Array.from(new Set(list(v)))}),
{}
const coerceOpts = Object.assign(
types.array.reduce(
(acc, arg) =>
Object.assign(acc, {[arg]: v => Array.from(new Set(list(v)))}),
{}
),
types.boolean
.concat(types.string, types.number)
.reduce(
(acc, arg) =>
Object.assign(acc, {[arg]: v => (Array.isArray(v) ? v.pop() : v)}),
{}
)
);

/**
Expand Down
12 changes: 11 additions & 1 deletion lib/cli/run-option-metadata.js
Expand Up @@ -45,7 +45,17 @@ exports.types = {
'watch'
],
number: ['retries'],
string: ['fgrep', 'grep', 'package', 'reporter', 'ui', 'slow', 'timeout']
string: [
'config',
'fgrep',
'grep',
'opts',
'package',
'reporter',
'ui',
'slow',
'timeout'
]
};

/**
Expand Down
7 changes: 0 additions & 7 deletions lib/cli/run.js
Expand Up @@ -258,13 +258,6 @@ exports.builder = yargs =>
}
});

types.boolean
.concat(types.string, types.number)
.filter(opt => Array.isArray(argv[opt]))
.forEach(opt => {
argv[opt] = argv[opt].pop();
});

// yargs.implies() isn't flexible enough to handle this
if (argv.invert && !('fgrep' in argv || 'grep' in argv)) {
throw createMissingArgumentError(
Expand Down
8 changes: 4 additions & 4 deletions test/node-unit/cli/options.spec.js
Expand Up @@ -86,7 +86,7 @@ describe('options', function() {
config: false,
opts: false,
package: false,
retries: 3
retries: '3'
})
);
});
Expand Down Expand Up @@ -202,7 +202,7 @@ describe('options', function() {
config: false,
opts: false,
package: false,
retries: 3
retries: '3'
}
)
);
Expand Down Expand Up @@ -427,7 +427,7 @@ describe('options', function() {
config: false,
opts: false,
package: false,
retries: 3
retries: '3'
})
);
});
Expand Down Expand Up @@ -476,7 +476,7 @@ describe('options', function() {
config: false,
opts: false,
package: false,
retries: 3
retries: '3'
})
);
});
Expand Down

0 comments on commit ffe1967

Please sign in to comment.