Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Closes #535.
Builds on top of yargs/yargs-parser#42 and yargs/yargs-parser#47.
Adds a
.coerce()
method that allows users to map a coercion (or transform) function to specific option keys. Coercion is then delegated toyargs-parser
.The new method accepts the following:
The
.option()
method was also augmented to support acoerce
property.Note that, in commit 46eaa9e, a special case of coercion logic was needed to support positional and variadic args for commands. This is because
yargs-parser
does not populate positional args inargv
- those are populated inlib/command.js
after parsing is complete - and so it was necessary to duplicate the execution of coercion functions after population of positional args. I would eventually like to replace this by moving the positional arg population logic down toyargs-parser
, but that's a bigger change.Also note that, when mixing default values with coercion for positional args, coercion is actually executed twice - first in
yargs-parser
against the default value and second inyargs
against the "parsed" value. Similar to yargs/yargs-parser#51, variadic args are given to the coercion function as an array (function called once) instead of as individual element values..coerce()
to API