Skip to content

Commit

Permalink
feat(command): builder function no longer needs to return the yargs i…
Browse files Browse the repository at this point in the history
…nstance (#549)

BREAKING CHANG:

changes the behavior of a builder function, so that the updated yargs object no longer needs to be returned.
  • Loading branch information
nexdrew authored and bcoe committed Aug 7, 2016
1 parent 7645019 commit eaa2873
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 16 deletions.
14 changes: 9 additions & 5 deletions lib/command.js
Expand Up @@ -130,17 +130,21 @@ module.exports = function (yargs, usage, validation) {
var parentCommands = currentContext.commands.slice()
currentContext.commands.push(command)
if (commandHandler.builder && typeof commandHandler.builder === 'function') {
// a function can be provided, which interacts which builds
// up a yargs chain and returns it.
// a function can be provided, which builds
// up a yargs chain and possibly returns it.
innerArgv = commandHandler.builder(yargs.reset(parsed.aliases))
// if the builder function did not yet parse argv with reset yargs
// and did not explicitly set a usage() string, then apply the
// original command string as usage() for consistent behavior with
// options object below
if (yargs.parsed === false && typeof yargs.getUsageInstance().getUsage() === 'undefined') {
yargs.usage('$0 ' + (parentCommands.length ? parentCommands.join(' ') + ' ' : '') + commandHandler.original)
if (yargs.parsed === false) {
if (typeof yargs.getUsageInstance().getUsage() === 'undefined') {
yargs.usage('$0 ' + (parentCommands.length ? parentCommands.join(' ') + ' ' : '') + commandHandler.original)
}
innerArgv = innerArgv ? innerArgv.argv : yargs.argv
} else {
innerArgv = yargs.parsed.argv
}
innerArgv = innerArgv ? innerArgv.argv : argv
} else if (commandHandler.builder && typeof commandHandler.builder === 'object') {
// as a short hand, an object can instead be provided, specifying
// the options that a command takes.
Expand Down
34 changes: 34 additions & 0 deletions test/command.js
Expand Up @@ -570,4 +570,38 @@ describe('Command', function () {
var argv = y.parse(['foo', 'bar'])
argv.second.should.equal('bar')
})

// addresses https://github.com/yargs/yargs/issues/522
it('does not require builder function to return', function () {
var argv = yargs('yo')
.command('yo [someone]', 'Send someone a yo', function (yargs) {
yargs.default('someone', 'Pat')
}, function (argv) {
argv.should.have.property('someone').and.equal('Pat')
})
.argv
argv.should.have.property('someone').and.equal('Pat')
})

it('allows builder function to parse argv without returning', function () {
var argv = yargs('yo Jude')
.command('yo <someone>', 'Send someone a yo', function (yargs) {
yargs.argv
}, function (argv) {
argv.should.have.property('someone').and.equal('Jude')
})
.argv
argv.should.have.property('someone').and.equal('Jude')
})

it('allows builder function to return parsed argv', function () {
var argv = yargs('yo Leslie')
.command('yo <someone>', 'Send someone a yo', function (yargs) {
return yargs.argv
}, function (argv) {
argv.should.have.property('someone').and.equal('Leslie')
})
.argv
argv.should.have.property('someone').and.equal('Leslie')
})
})
11 changes: 0 additions & 11 deletions test/yargs.js
Expand Up @@ -40,17 +40,6 @@ describe('yargs dsl tests', function () {
Object.keys(argv).should.include('cool')
})

it('populates argv with placeholder keys when passed into command handler', function (done) {
yargs(['blerg'])
.option('cool', {})
.command('blerg', 'handle blerg things', function () {}, function (argv) {
Object.keys(argv).should.include('cool')
return done()
})
.exitProcess(false) // defaults to true.
.argv
})

it('accepts an object for implies', function () {
var r = checkOutput(function () {
return yargs(['--x=33'])
Expand Down

0 comments on commit eaa2873

Please sign in to comment.