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

Support option aliases #492

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ program
.version('0.0.1')
.option('-p, --peppers', 'Add peppers')
.option('-P, --pineapple', 'Add pineapple')
.option('-b, --bbq-sauce', 'Add bbq sauce')
.option('-b, --bbq-sauce, --addBbq', 'Add bbq sauce')
.option('-c, --cheese [type]', 'Add the specified type of cheese [marble]', 'marble')
.parse(process.argv);

Expand All @@ -42,7 +42,7 @@ if (program.bbqSauce) console.log(' - bbq');
console.log(' - %s cheese', program.cheese);
```

Short flags may be passed as a single arg, for example `-abc` is equivalent to `-a -b -c`. Multi-word options such as "--template-engine" are camel-cased, becoming `program.templateEngine` etc.
Short flags may be passed as a single arg, for example `-abc` is equivalent to `-a -b -c`. Multi-word options such as "--template-engine" are camel-cased, becoming `program.templateEngine` etc. Option aliases are supported.


## Coercion
Expand Down
3 changes: 2 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ function Option(flags, description) {
flags = flags.split(/[ ,|]+/);
if (flags.length > 1 && !/^[[<]/.test(flags[1])) this.short = flags.shift();
this.long = flags.shift();
this.aliases = flags;
this.description = description || '';
}

Expand All @@ -69,7 +70,7 @@ Option.prototype.name = function() {
*/

Option.prototype.is = function(arg) {
return arg == this.short || arg == this.long;
return arg == this.short || arg == this.long || this.aliases.indexOf(arg) !== -1;
};

/**
Expand Down
22 changes: 22 additions & 0 deletions test/test.options.aliases.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/**
* Module dependencies.
*/

var program = require('../')
, should = require('should');

program
.version('0.0.1')
.option('-c, --add-cheese, --addCheese, --iWantCheese', 'add cheese');

program.parse(['node', 'test', '-c']);
program.addCheese.should.be.true();

program.parse(['node', 'test', '--add-cheese']);
program.addCheese.should.be.true();

program.parse(['node', 'test', '--addCheese']);
program.addCheese.should.be.true();

program.parse(['node', 'test', '--iWantCheese']);
program.addCheese.should.be.true();