Skip to content

Commit

Permalink
fix: positional arguments now work if no handler is provided to inner…
Browse files Browse the repository at this point in the history
… command (#864)

BREAKING CHANGE: this pull requests introduces language features that require Node 4+.
  • Loading branch information
bcoe committed Apr 30, 2017
1 parent 73b5733 commit e28ded3
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 4 deletions.
3 changes: 1 addition & 2 deletions .travis.yml
Expand Up @@ -7,7 +7,6 @@ os:
after_success: npm run coverage

node_js:
- "0.10"
- "0.12"
- "4"
- "6"
- "node"
2 changes: 1 addition & 1 deletion appveyor.yml
Expand Up @@ -3,9 +3,9 @@
# Test against these versions of Node.js.
environment:
matrix:
- nodejs_version: "0.10"
- nodejs_version: "4"
- nodejs_version: "6"
- nodejs_version: "7"

# Install scripts. (runs after repo cloning)
install:
Expand Down
2 changes: 2 additions & 0 deletions lib/command.js
Expand Up @@ -15,6 +15,8 @@ module.exports = function (yargs, usage, validation) {
var defaultCommand
self.addHandler = function (cmd, description, builder, handler) {
var aliases = []
handler = handler || function () {}

if (Array.isArray(cmd)) {
aliases = cmd.slice(1)
cmd = cmd[0]
Expand Down
16 changes: 15 additions & 1 deletion test/command.js
Expand Up @@ -409,7 +409,7 @@ describe('Command', function () {
var handlers = y.getCommandInstance().getCommandHandlers()
handlers.foo.original.should.equal(module.command)
handlers.foo.builder.should.equal(module.builder)
expect(handlers.foo.handler).to.equal(undefined)
expect(typeof handlers.foo.handler).to.equal('function')
var commands = y.getUsageInstance().getCommands()
commands[0].should.deep.equal([module.command, module.describe, isDefault, aliases])
})
Expand Down Expand Up @@ -1339,4 +1339,18 @@ describe('Command', function () {
argv._.should.eql(['bar', 'foo'])
})
})

// see: https://github.com/yargs/yargs/issues/861 phew! that's an edge-case.
it('should allow positional arguments for inner commands in strict mode, when no handler is provided', () => {
yargs()
.command('foo', 'outer command', (yargs) => {
yargs.command('bar [optional]', 'inner command')
})
.strict()
.parse('foo bar 33', function (err, argv) {
expect(err).to.equal(null)
argv.optional.should.equal(33)
argv._.should.eql(['foo', 'bar'])
})
})
})

0 comments on commit e28ded3

Please sign in to comment.