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

Move parsed options into a data object #514

Closed
ericuldall opened this issue Mar 25, 2016 · 3 comments
Closed

Move parsed options into a data object #514

ericuldall opened this issue Mar 25, 2016 · 3 comments

Comments

@ericuldall
Copy link

First off, I really like this plugin. I've written argv parsing snippets more times than I like and they always come out different. This is a much needed piece of code.

One thing I think would be a good addition is moving all the parsed options into a data object. This would allow us to abstract away all the private data from the public data a user is trying to access.

Instead of

if( program.myoption ){ //do something with myoption}

We can do

if( program.has('myoption') ){ program.get('myoption') }
//also would allow us to easily get all options
console.log(program.get_options()); //returns an object of all parsed options

I haven't dug too deep into this packages code, so perhaps there's an implementation detail that makes storing data this way less than simple. If not, then I'd like to see it work this way.

@ericuldall
Copy link
Author

I've implemented the proposed changes with some helper methods in a forked feature branch. Still need to write tests for the new commands.

https://github.com/ericuldall/commander.js/tree/data_overload

New features allow the following:

#!/usr/bin/env node
var program = require('commander');

program
  .version('0.0.1')
  .description('Test')
  .usage('[options] string')
  .option('-t, --type [value]', 'Type')
  .option('-f, --format', 'Format')
  .parse(process.argv);

var type = program.get('type'); //if exists return type, else returns false
var options = program.get(); //return all options, or empty object

var has = program.has('type', function(type){
    console.log(type); //will return if type exists
});
console.log(has); //boolean

var has_all = program.has_all(['type', 'format'], function(type, format){
    console.log(type, format); //will return if both exist
});
console.log(has_all); //boolean

}

@ericuldall
Copy link
Author

Pull request sent and all tests passing.
Pull Request 515

@shadowspawn
Copy link
Collaborator

Closing this as covered by #183 and #933

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

No branches or pull requests

2 participants