Skip to content

Commit

Permalink
Support option aliases
Browse files Browse the repository at this point in the history
  • Loading branch information
ajfranzoia committed Jan 25, 2016
1 parent 1452f6f commit 0ab0eef
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 3 deletions.
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();

0 comments on commit 0ab0eef

Please sign in to comment.