visionmedia/commander.js — pull request #262 by tonylukasavage (2014-09-19 20:58:06+00:00)
Title: add opts() for getting key-value pairs

A common usage pattern I have when using commander is to process the CLI in a bin file and defer all actual program-specific execution to a separate module. I often pass all options gathered from commander straight my module, like this:

```js
#!/usr/bin/env node

var program = require('commander'),
mymodule = require('mymodule');

// setup the program object

mymodule(program);
```

While this works, it would be nice if the object passed through included _only_ the options I specified when setting up the program object.

This PR add the `opts()` function to the program object (via `Command.prototype`) so as to return the options, with special handling for the version, as a simple object of key-value pairs. No parsing Option objects, no additional properties associated with the program obejcts, I can then do the following in my above example to get the desired behavior.

```js
#!/usr/bin/env node

var program = require('commander'),
mymodule = require('mymodule');

// setup the program object

mymodule(program.opts());
```

The included test case also demonstrates this fairly simply.

**NOTE**: I am entirely open to changing the name of the API. `opts()` was the first thing to occur to me that wasn't already claimed on the `Command` object. If someone has a better alternative I'd be happy to update the code and testing.