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

add opts() for getting key-value pairs #262

Merged
merged 1 commit into from
Sep 21, 2014

Conversation

tonylukasavage
Copy link
Contributor

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:

#!/usr/bin/env node

var program = require('commander'),
  mymodule = require('mymodule');

// setup the program object

mymodule(program);

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 (via Command.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.

#!/usr/bin/env node

var program = require('commander'),
  mymodule = require('mymodule');

// setup the program object

mymodule(program.opts());

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 the Command object. If someone has a better alternative I'd be happy to update the code and testing.

@SomeKittens
Copy link
Collaborator

opts sounds good to me.

@thethomaseffect Look good?

@thethomaseffect
Copy link
Collaborator

Looks great, will also unblock anyone who wanted to use the names of JS internals like apply for their options without clobbering the main object, LGTM 👍

thethomaseffect added a commit that referenced this pull request Sep 21, 2014
add opts() for getting key-value pairs of options
@thethomaseffect thethomaseffect merged commit 6089ed4 into tj:master Sep 21, 2014
@tonylukasavage
Copy link
Contributor Author

@thethomaseffect any plans for a 2.4.0 or 2.3.1 release? I didn't see any obvious documentation or milestones set up and I was wondering when this would be in an official version.

@thethomaseffect
Copy link
Collaborator

Hopefully in the next few days, want to update the documentation first and do some manual testing of a few new features and then should push out the new version!

@FokkeZB
Copy link

FokkeZB commented Jan 7, 2015

@tonylukasavage was it a deliberate decision for opts() not to apply the defaults for options?

program
  .version(pkg.version, '-v, --version')
  .description(pkg.description)
  .option('-d, --project-dir [path]', 'the directory containing the project [.]', process.cwd());

console.log(program.projectDir);
console.log(program.opts());

The first log has the default, the second one not.

@tonylukasavage
Copy link
Contributor Author

You didn't parse()
On Jan 7, 2015 3:25 AM, "Fokke Zandbergen" notifications@github.com wrote:

@tonylukasavage https://github.com/tonylukasavage was it a deliberate
decision for opts() not to apply the defaults for options?

program
.version(pkg.version, '-v, --version')
.description(pkg.description)
.option('-d, --project-dir [path]', 'the directory containing the project [.]', process.cwd());

console.log(program.projectDir);
console.log(program.opts());

The first log has the default, the second one not.


Reply to this email directly or view it on GitHub
#262 (comment).

@FokkeZB
Copy link

FokkeZB commented Jan 7, 2015

Same result (probably copy pasted it wrong):

program
  .version(pkg.version, '-v, --version')
  .description(pkg.description)
  .option('-d, --project-dir [path]', 'the directory containing the project [.]', process.cwd());

program.parse(process.argv);

console.log(program.projectDir); 
console.log(program.opts()); 

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.

4 participants