Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add coerce api #586

Merged
merged 3 commits into from Aug 13, 2016
Merged

feat: add coerce api #586

merged 3 commits into from Aug 13, 2016

Conversation

nexdrew
Copy link
Member

@nexdrew nexdrew commented Aug 13, 2016

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 to yargs-parser.

The new method accepts the following:

  • a single key and a function
  • an array of keys and a function
  • an object mapping keys to functions

The .option() method was also augmented to support a coerce 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 in argv - those are populated in lib/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 to yargs-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 in yargs 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.

  • Add .coerce() to API
  • Support for coercion of positional and variadic args for commands
  • Tests
  • Add docs to README

@nexdrew nexdrew added the 5.x label Aug 13, 2016
@maxrimue maxrimue mentioned this pull request Aug 13, 2016
10 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants