Skip to content

Commit

Permalink
fix for yargs.argv having the same keys added multiple times see #63
Browse files Browse the repository at this point in the history
  • Loading branch information
bcoe committed Nov 11, 2014
1 parent b8d3472 commit 31043de
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 8 deletions.
9 changes: 6 additions & 3 deletions lib/minimist.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,12 @@ module.exports = function (args, opts) {
aliases[key].concat(key).forEach(function (x) {
if (/-/.test(x)) {
var c = toCamelCase(x);
aliases[key].push(c);
newAliases[c] = true;
// ensure we don't add the same alias multiple
// times while camel-casing.
if (opts.alias[x] || !opts.default[c]) {
aliases[key].push(c);
newAliases[c] = true;
}
}
});
aliases[key].forEach(function (x) {
Expand Down Expand Up @@ -71,7 +75,6 @@ module.exports = function (args, opts) {
});

var notFlags = [];

if (args.indexOf('--') !== -1) {
notFlags = args.slice(args.indexOf('--')+1);
args = args.slice(0, args.indexOf('--'));
Expand Down
30 changes: 25 additions & 5 deletions test/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ describe('parse', function () {
parse.should.have.property('bool', true);
parse.should.have.property('_').with.length(0);
});

it('should place bare options in the _ array', function () {
var parse = yargs.parse(['foo', 'bar', 'baz']);
parse.should.have.property('_').and.deep.equal(['foo','bar','baz']);
Expand All @@ -38,7 +38,7 @@ describe('parse', function () {
parse.should.have.property('s', 'meow');
parse.should.have.property('_').with.length(0);
});

it('should set the value of a single short option to the next supplied value', function () {
var parse = yargs.parse(['-h', 'localhost']);
parse.should.have.property('h', 'localhost');
Expand Down Expand Up @@ -85,7 +85,7 @@ describe('parse', function () {
parse.should.have.property('h', 'localhost');
parse.should.have.property('_').and.deep.equal(['script.js']);
});

it('should still set values appropriately if a mix of short and long options are specified', function () {
var parse = yargs.parse(['-h', 'localhost', '--port', '555']);
parse.should.have.property('h', 'localhost');
Expand All @@ -98,13 +98,13 @@ describe('parse', function () {
parse.should.have.property('moo', false);
parse.should.have.property('_').with.length(0);
});

it('should group values into an array if the same option is specified multiple times', function () {
var parse = yargs.parse(['-v', 'a', '-v', 'b', '-v', 'c' ]);
parse.should.have.property('v').and.deep.equal(['a','b','c']);
parse.should.have.property('_').with.length(0);
});

it('should still set values appropriately if we supply a comprehensive list of various types of options', function () {
var parse = yargs.parse([
'--name=meowmers', 'bare', '-cats', 'woo',
Expand Down Expand Up @@ -352,4 +352,24 @@ describe('parse', function () {
parsed.should.have.property('other', 'false');
});

// regression, see https://github.com/chevex/yargs/issues/63
it('should not add alias multiple times to argv, if argv is called multiple times', function() {
var yargs = require('../')(['--health-check=/banana', '--second-key', '/apple'])
.options('h', {
alias: 'health-check',
description: 'health check',
default: '/apple'
})
.options('second-key', {
alias: 's',
description: 'second key',
default: '/banana'
});

yargs.argv;

yargs.argv.secondKey.should.eql('/apple');
yargs.argv.healthCheck.should.eql('/banana');
});

});

0 comments on commit 31043de

Please sign in to comment.