visionmedia/commander.js — pull request #75 by karthikv (2012-07-23 04:13:19+00:00)
Title: Incorrectly guessed parameters to boolean options should be arguments to a command.

To illustrate the issue, consider the simple command below that prints the string it is given. Call this file cmd.js:

```js
#!/usr/bin/env node
var program = require('commander');

program.command('print ')
.option('-e, --example', 'Example boolean option')
.action(function(str, env) {
console.log(str);
});

program.parse(process.argv);
```

When run without any options, it works fine. The problem arises when the `-e` or `--example` boolean option is provided before the input ``, like so:

```bash
$ ./cmd.js print hello
# => hello

$ ./cmd.js print -e hello
# => error: missing required argument `str'
```

Even though `-e` is a boolean option, "hello" is guessed to be its argument in the `lib/commander.js` on [line 504](https://github.com/visionmedia/commander.js/blob/72344894f09cadaa7cc4bf8ab3fa51a790a72878/lib/commander.js#L504):

```js
// looks like an option
if (arg.length > 1 && '-' == arg[0]) {
unknownOptions.push(arg);

// If the next argument looks like it might be
// an argument for this option, we pass it on.
// If it isn't, then it'll simply be ignored
if (argv[i+1] && '-' != argv[i+1][0]) {
unknownOptions.push(argv[++i]);
}
continue;
}
```

The "hello" argument is then ignored, which makes the command fail.

I have fixed this issue in the latest commit below by reinserting incorrectly guessed option parameters back into the arguments array for a command. Let me know if you have any questions about the code and if there any changes I need to make.

I also noticed that the date test in `test.prompt.js` was failing. It seems to be that this line:

```js
actual.should.be.an.instanceof(Date);
```

was acting up even though `actual` was indeed an instance of `Date`. In fact, I changed it to:

```js
(new Date()).should.be.an.instanceof(Date);
```

and it still failed. As a result, I modified the test to use `should.equal(true)` instead, like so:

```js
(actual instanceof Date).should.equal(true);
```

and it passed.

I've been using commander.js for one of my personal projects and have been pleased with its robustness. I'd love to fix these issues and give back. Don't hesitate if you have any questions or need me to modify something.