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 2, 2019
1 parent f5ca468 commit bea695b
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 16 deletions.
21 changes: 16 additions & 5 deletions lib/cli/options.js
Original file line number Diff line number Diff line change
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
* - 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)}),
{}
)
);

/**
Expand Down
7 changes: 0 additions & 7 deletions lib/cli/run.js
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ describe('options', function() {
config: false,
opts: false,
package: false,
retries: 3
retries: '3'
})
);
});
Expand Down Expand Up @@ -199,7 +199,7 @@ describe('options', function() {
config: false,
opts: false,
package: false,
retries: 3
retries: '3'
}
)
);
Expand Down Expand Up @@ -424,7 +424,7 @@ describe('options', function() {
config: false,
opts: false,
package: false,
retries: 3
retries: '3'
})
);
});
Expand Down Expand Up @@ -473,7 +473,7 @@ describe('options', function() {
config: false,
opts: false,
package: false,
retries: 3
retries: '3'
})
);
});
Expand Down

0 comments on commit bea695b

Please sign in to comment.