Skip to content

Commit

Permalink
Fix a couple of bad interactions between aliases and defaults
Browse files Browse the repository at this point in the history
- Alias + boolean + default is broken (#7)
- Default values and options with dashes not working (#20)
  • Loading branch information
nylen committed Jun 6, 2014
1 parent 39360db commit 66f12c8
Show file tree
Hide file tree
Showing 3 changed files with 126 additions and 0 deletions.
9 changes: 9 additions & 0 deletions lib/minimist.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,15 @@ module.exports = function (args, opts) {

var defaults = opts['default'] || {};

Object.keys(defaults || {}).forEach(function (key) {
if (/-/.test(key) && !opts.alias[key]) {
aliases[key] = (aliases[key] || []).concat(toCamelCase(key));
}
(aliases[key] || []).forEach(function (alias) {
defaults[alias] = defaults[key];
});
});

var argv = { _ : [] };
Object.keys(flags.bools).forEach(function (key) {
setArg(key, defaults[key] === undefined ? false : defaults[key]);
Expand Down
35 changes: 35 additions & 0 deletions test/parse_camelCase.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,41 @@ describe('parse', function () {
result.should.have.property('someOption').that.is.a('boolean').and.is.true;
});

it('should provide defaults of options with dashes as camelCase properties', function() {
var result = yargs()
.option('some-option', {
describe : 'some option',
default : 'asdf'
})
.parse([ ]);

result.should.have.property('someOption', 'asdf');
});

it('should provide aliases of options with dashes as camelCase properties', function() {
var result = yargs()
.option('some-option', {
alias : 'o',
describe : 'some option',
default : 'asdf'
})
.parse([ ]);

result.should.have.property('o', 'asdf');
});

it('should provide aliases of options with dashes as camelCase properties', function() {
var result = yargs()
.option('o', {
alias : 'some-option',
describe : 'some option',
default : 'asdf'
})
.parse([ ]);

result.should.have.property('someOption', 'asdf');
});

it('should provide aliases with dashes as camelCase properties', function() {
var result = yargs()
.option('o', {
Expand Down
82 changes: 82 additions & 0 deletions test/parse_defaults.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
var should = require('chai').should(),
yargs = require('../');

describe('parse', function () {

describe('defaults', function () {
function checkNoArgs(argv, hasAlias) {
it('should set defaults if no args', function() {
var result = argv.parse([ ]);
result.should.have.property('flag', true);
if (hasAlias) {
result.should.have.property('f', true);
}
});
}

function checkExtraArg(argv, hasAlias) {
it('should set defaults if one extra arg', function() {
var result = argv.parse([ 'extra' ]);
result.should.have.property('flag', true);
result.should.have.property('_').and.deep.equal(['extra']);
if (hasAlias) {
result.should.have.property('f', true);
}
});
}

function checkStringArg(argv, hasAlias) {
it('should set defaults even if arg looks like a string', function() {
var result = argv.parse([ '--flag', 'extra' ]);
result.should.have.property('flag', true);
result.should.have.property('_').and.deep.equal(['extra']);
if (hasAlias) {
result.should.have.property('f', true);
}
});
}

describe('for options with aliases', function () {
var args = yargs().options({
flag : {
alias : 'f',
default : true
}
});

checkNoArgs(args, true);
checkExtraArg(args, true);
// This test case should fail, because we didn't specify that the
// option is a boolean
// checkStringArg(args, true);
});

describe('for typed options without aliases', function () {
var args = yargs().options({
flag : {
type : 'boolean',
default : true
}
});

checkNoArgs(args);
checkExtraArg(args);
checkStringArg(args);
});

describe('for typed options with aliases', function () {
var args = yargs().options({
flag : {
alias : 'f',
type : 'boolean',
default : true
}
});

checkNoArgs(args, true);
checkExtraArg(args, true);
checkStringArg(args, true);
});
});

});

0 comments on commit 66f12c8

Please sign in to comment.