Skip to content

Commit

Permalink
feat: builder is now optional for a command module (#545)
Browse files Browse the repository at this point in the history
  • Loading branch information
bcoe committed Jul 9, 2016
1 parent 2609b2e commit 8d6ad6e
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 6 deletions.
9 changes: 3 additions & 6 deletions lib/command.js
Expand Up @@ -11,14 +11,9 @@ module.exports = function (yargs, usage, validation) {

var handlers = {}
self.addHandler = function (cmd, description, builder, handler) {
// allow modules that define (a) all properties or (b) only command and description
if (typeof cmd === 'object') {
const commandString = typeof cmd.command === 'string' ? cmd.command : moduleName(cmd)
if (cmd.builder && typeof cmd.handler === 'function') {
self.addHandler(commandString, extractDesc(cmd), cmd.builder, cmd.handler)
} else {
self.addHandler(commandString, extractDesc(cmd))
}
self.addHandler(commandString, extractDesc(cmd), cmd.builder, cmd.handler)
return
}

Expand All @@ -39,6 +34,8 @@ module.exports = function (yargs, usage, validation) {
handlers[parsedCommand.cmd] = {
original: cmd,
handler: handler,
// TODO: default to a noop builder in
// yargs@5.x
builder: builder,
demanded: parsedCommand.demanded,
optional: parsedCommand.optional
Expand Down
20 changes: 20 additions & 0 deletions test/command.js
Expand Up @@ -318,6 +318,26 @@ describe('Command', function () {
var commands = y.getUsageInstance().getCommands()
commands[0].should.deep.equal([module.command, module.describe])
})

it('accepts module (missing handler function) as 1st argument', function () {
var module = {
command: 'foo',
describe: 'i\'m not feeling very creative at the moment',
builder: {
hello: {
default: 'world'
}
}
}

var y = yargs([]).command(module)
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)
var commands = y.getUsageInstance().getCommands()
commands[0].should.deep.equal([module.command, module.describe])
})
})

describe('commandDir', function () {
Expand Down

0 comments on commit 8d6ad6e

Please sign in to comment.