add opts() for getting key-value pairs #262
Merged
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.
A common usage pattern I have when using commander is to process the CLI in a bin file and defer all actual program-specific execution to a separate module. I often pass all options gathered from commander straight my module, like this:
While this works, it would be nice if the object passed through included only the options I specified when setting up the program object.
This PR add the
opts()
function to the program object (viaCommand.prototype
) so as to return the options, with special handling for the version, as a simple object of key-value pairs. No parsing Option objects, no additional properties associated with the program obejcts, I can then do the following in my above example to get the desired behavior.The included test case also demonstrates this fairly simply.
NOTE: I am entirely open to changing the name of the API.
opts()
was the first thing to occur to me that wasn't already claimed on theCommand
object. If someone has a better alternative I'd be happy to update the code and testing.