Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: allow disabling duplicate-arguments-array #67

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 5 additions & 2 deletions index.js
Expand Up @@ -15,7 +15,8 @@ function parse (args, opts) {
'camel-case-expansion': true,
'dot-notation': true,
'parse-numbers': true,
'boolean-negation': true
'boolean-negation': true,
'duplicate-arguments-array': true
}, opts.configuration)
var defaults = opts.default || {}
var configObjects = opts.configObjects || []
Expand Down Expand Up @@ -544,8 +545,10 @@ function parse (args, opts) {
o[key] = value
} else if (Array.isArray(o[key])) {
o[key].push(value)
} else {
} else if (configuration['duplicate-arguments-array']) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great, that's about as simple as I was hoping it would be 👍

o[key] = [ o[key], value ]
} else {
o[key] = value
}
}

Expand Down
21 changes: 21 additions & 0 deletions test/yargs-parser.js
Expand Up @@ -1933,6 +1933,27 @@ describe('yargs-parser', function () {
expect(parsed.dice).to.equal(undefined)
})
})

describe('duplicate arguments array', function () {
it('adds duplicate argument to array', function () {
var parsed = parser('-x a -x b', {
configuration: {
'duplicate-arguments-array': true
}
})

parsed['x'].should.deep.equal(['a', 'b'])
})
it('keeps only last argument', function () {
var parsed = parser('-x a -x b', {
configuration: {
'duplicate-arguments-array': false
}
})

parsed['x'].should.equal('b')
})
})
})

// addresses: https://github.com/yargs/yargs-parser/issues/41
Expand Down