Skip to content

feat: add coerce api - #586

Merged
bcoe merged 3 commits into
masterfrom
coerce-api
Aug 13, 2016
Merged

bcoe merged 3 commits into
masterfrom
coerce-api

Conversation

@nexdrew

@nexdrew nexdrew commented Aug 13, 2016 •

Copy link
Copy Markdown
Member

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 done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants