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 2d0f868
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 7 deletions.
12 changes: 6 additions & 6 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
[![NPM Downloads](https://img.shields.io/npm/dm/commander.svg?style=flat)](https://www.npmjs.org/package/commander)
[![Join the chat at https://gitter.im/tj/commander.js](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/tj/commander.js?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)

The complete solution for [node.js](http://nodejs.org) command-line interfaces, inspired by Ruby's [commander](https://github.com/tj/commander).
The complete solution for [node.js](http://nodejs.org) command-line interfaces, inspired by Ruby's [commander](https://github.com/tj/commander).
[API documentation](http://tj.github.com/commander.js/)


Expand All @@ -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 Expand Up @@ -95,7 +95,7 @@ program
.option('-s --size <size>', 'Pizza size', /^(large|medium|small)$/i, 'medium')
.option('-d --drink [drink]', 'Drink', /^(coke|pepsi|izze)$/i)
.parse(process.argv);

console.log(' size: %j', program.size);
console.log(' drink: %j', program.drink);
```
Expand Down Expand Up @@ -172,7 +172,7 @@ program
.parse(process.argv);
```

When `.command()` is invoked with a description argument, no `.action(callback)` should be called to handle sub-commands, otherwise there will be an error. This tells commander that you're going to use separate executables for sub-commands, much like `git(1)` and other popular tools.
When `.command()` is invoked with a description argument, no `.action(callback)` should be called to handle sub-commands, otherwise there will be an error. This tells commander that you're going to use separate executables for sub-commands, much like `git(1)` and other popular tools.
The commander will try to search the executables in the directory of the entry script (like `./examples/pm`) with the name `program-command`, like `pm-install`, `pm-search`.

Options can be passed with the call to `.command()`. Specifying `true` for `opts.noHelp` will remove the option from the generated help output. Specifying `true` for `opts.isDefault` will run the subcommand if no other subcommand is specified.
Expand All @@ -189,7 +189,7 @@ You can enable `--harmony` option in two ways:

The help information is auto-generated based on the information commander already knows about your program, so the following `--help` info is for free:

```
```
$ ./examples/pizza --help
Usage: pizza [options]
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 2d0f868

Please sign in to comment.