From bea695bbe6ccfd69c8d533572f2a023138a3ebfb Mon Sep 17 00:00:00 2001 From: juergba Date: Thu, 2 May 2019 19:54:21 +0200 Subject: [PATCH] coerce function for boolean/string/number types --- lib/cli/options.js | 21 ++++++++++++++++----- lib/cli/run.js | 7 ------- test/node-unit/cli/options.spec.js | 8 ++++---- 3 files changed, 20 insertions(+), 16 deletions(-) diff --git a/lib/cli/options.js b/lib/cli/options.js index a033c39ac7..4e10e84d7a 100644 --- a/lib/cli/options.js +++ b/lib/cli/options.js @@ -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 + * - for non-`array`-type options given multiple times, use its array's last element * 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)}), + {} + ) ); /** diff --git a/lib/cli/run.js b/lib/cli/run.js index 0541ec6a83..da5ffd7b6a 100644 --- a/lib/cli/run.js +++ b/lib/cli/run.js @@ -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( diff --git a/test/node-unit/cli/options.spec.js b/test/node-unit/cli/options.spec.js index 1b89e9deb5..cae83041ce 100644 --- a/test/node-unit/cli/options.spec.js +++ b/test/node-unit/cli/options.spec.js @@ -83,7 +83,7 @@ describe('options', function() { config: false, opts: false, package: false, - retries: 3 + retries: '3' }) ); }); @@ -199,7 +199,7 @@ describe('options', function() { config: false, opts: false, package: false, - retries: 3 + retries: '3' } ) ); @@ -424,7 +424,7 @@ describe('options', function() { config: false, opts: false, package: false, - retries: 3 + retries: '3' }) ); }); @@ -473,7 +473,7 @@ describe('options', function() { config: false, opts: false, package: false, - retries: 3 + retries: '3' }) ); });