Skip to content

Commit

Permalink
fix!: do not allow additional positionals in strict mode
Browse files Browse the repository at this point in the history
  • Loading branch information
mleguen authored and bcoe committed Jun 16, 2019
1 parent 59739e6 commit 35d777c
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/validation.js
Expand Up @@ -102,7 +102,7 @@ module.exports = function validation (yargs, usage, y18n) {
}
})

if (commandKeys.length > 0) {
if ((currentContext.commands.length > 0) || (commandKeys.length > 0)) {
argv._.slice(currentContext.commands.length).forEach((key) => {
if (commandKeys.indexOf(key) === -1) {
unknown.push(key)
Expand Down
12 changes: 12 additions & 0 deletions test/validation.js
Expand Up @@ -335,6 +335,18 @@ describe('validation tests', () => {
.parse()
})

it('fails in strict mode with extra positionals', (done) => {
yargs(['kangaroo', 'jumping', 'fast'])
.command('kangaroo <status>', 'kangaroo handlers')
.strict()
.fail((msg) => {
msg.should.equal('Unknown argument: fast')
return done()
})
.parse()
expect.fail('no parsing failure')
})

it('does not fail in strict mode when no commands configured', () => {
const argv = yargs('koala')
.demand(1)
Expand Down

0 comments on commit 35d777c

Please sign in to comment.