Skip to content

Commit

Permalink
Merge pull request #728 from sorohan/master
Browse files Browse the repository at this point in the history
Fixes issue #727: Passing empty string for option on command is set to undefined
  • Loading branch information
abetomo committed Jan 30, 2018
2 parents d5ac793 + 06451e3 commit f5abcdb
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
6 changes: 3 additions & 3 deletions index.js
Expand Up @@ -747,7 +747,7 @@ Command.prototype.parseOptions = function(argv) {
// 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] !== '-') {
if ((i + 1) < argv.length && argv[i + 1][0] !== '-') {
unknownOptions.push(argv[++i]);
}
continue;
Expand Down Expand Up @@ -855,8 +855,8 @@ Command.prototype.version = function(str, flags) {
if (arguments.length === 0) return this._version;
this._version = str;
flags = flags || '-V, --version';
var longOptIndex = flags.indexOf('--')
this._versionOptionName = ~longOptIndex ? flags.substr(longOptIndex + 2) : 'version'
var longOptIndex = flags.indexOf('--');
this._versionOptionName = ~longOptIndex ? flags.substr(longOptIndex + 2) : 'version';
this.option(flags, 'output the version number');
this.on('option:' + this._versionOptionName, function() {
process.stdout.write(str + '\n');
Expand Down
20 changes: 20 additions & 0 deletions test/test.command.action.emptyOption.js
@@ -0,0 +1,20 @@
/**
* Module dependencies.
*/

var program = require('../')
, should = require('should');

var val = "some cheese"
program
.name('test')
.command('mycommand')
.option('-c, --cheese [type]', 'optionally specify the type of cheese')
.action(function(cmd) {
val = cmd.cheese;
});

program.parse(['node', 'test', 'mycommand', '--cheese', '']);

val.should.equal('');

0 comments on commit f5abcdb

Please sign in to comment.