visionmedia/commander.js — pull request #140 by akoenig (2013-03-22 10:24:22+00:00)
Title: Chaining in a loop

In a current project I needed to be able to chain a "Commander.js command" in a loop.

So, this is my approach, which might be helpful for somebody who is facing the same kind of problem.

```javascript
var cli = require("commander");

;(function () {
"use strict";

var commands = require("./commands");

//
// Command structure:
//
// {
// pattern: "add [url]",
// description: "Add command foo.",
// options: [
// {
// pattern: '-t, --tags "',
// description: "The addable tags."
// },
// {
// pattern: '-ver, --verbose',
// description: "Verbose output"
// }
// ]
// }
//

commands.forEach(function (command) {
cli
.command(command.pattern)
.description(command.description)
.and(function (cli) {
if (command.options) {
command.options.forEach(function (option) {
cli.option(option.pattern, option.description);
});
}
})
.action(command.exec);
});
})();
```