Skip to content

Commit

Permalink
fix: commands are now applied in order, from left to right (#857)
Browse files Browse the repository at this point in the history
  • Loading branch information
bcoe committed Apr 29, 2017
1 parent 6f78c05 commit baba863
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 6 deletions.
6 changes: 3 additions & 3 deletions lib/command.js
Expand Up @@ -174,7 +174,7 @@ module.exports = function (yargs, usage, validation) {
return !!defaultCommand
}

self.runCommand = function (command, yargs, parsed) {
self.runCommand = function (command, yargs, parsed, commandIndex) {
var aliases = parsed.aliases
var commandHandler = handlers[command] || handlers[aliasMap[command]] || defaultCommand
var currentContext = yargs.getContext()
Expand All @@ -199,7 +199,7 @@ module.exports = function (yargs, usage, validation) {
if (typeof yargs.getUsageInstance().getUsage() === 'undefined') {
yargs.usage('$0 ' + (parentCommands.length ? parentCommands.join(' ') + ' ' : '') + commandHandler.original)
}
innerArgv = innerYargs ? innerYargs._parseArgs(null, null, true) : yargs._parseArgs(null, null, true)
innerArgv = innerYargs ? innerYargs._parseArgs(null, null, true, commandIndex) : yargs._parseArgs(null, null, true, commandIndex)
} else {
innerArgv = yargs.parsed.argv
}
Expand All @@ -214,7 +214,7 @@ module.exports = function (yargs, usage, validation) {
Object.keys(commandHandler.builder).forEach(function (key) {
innerYargs.option(key, commandHandler.builder[key])
})
innerArgv = innerYargs._parseArgs(null, null, true)
innerArgv = innerYargs._parseArgs(null, null, true, commandIndex)
aliases = innerYargs.parsed.aliases
}

Expand Down
12 changes: 12 additions & 0 deletions test/command.js
Expand Up @@ -1327,4 +1327,16 @@ describe('Command', function () {
.argv
output.should.include('1')
})

// see: https://github.com/yargs/yargs/issues/853
it('should not execute command if it is proceeded by another positional argument', function () {
var commandCalled = false
yargs()
.command('foo', 'foo command', function () {}, function () { commandCalled = true })
.parse('bar foo', function (err, argv) {
expect(err).to.equal(null)
commandCalled.should.equal(false)
argv._.should.eql(['bar', 'foo'])
})
})
})
10 changes: 7 additions & 3 deletions yargs.js
Expand Up @@ -929,7 +929,7 @@ function Yargs (processArgs, cwd, parentRequire) {
enumerable: true
})

self._parseArgs = function (args, shortCircuit, _skipValidation) {
self._parseArgs = function (args, shortCircuit, _skipValidation, commandIndex) {
var skipValidation = !!_skipValidation
args = args || processArgs

Expand Down Expand Up @@ -980,13 +980,17 @@ function Yargs (processArgs, cwd, parentRequire) {
var handlerKeys = command.getCommands()
if (handlerKeys.length) {
var firstUnknownCommand
for (var i = 0, cmd; argv._[i] !== undefined; i++) {
for (var i = (commandIndex || 0), cmd; argv._[i] !== undefined; i++) {
cmd = String(argv._[i])
if (~handlerKeys.indexOf(cmd) && cmd !== completionCommand) {
setPlaceholderKeys(argv)
return command.runCommand(cmd, self, parsed)
// commands are executed using a recursive algorithm that executes
// the deepest command first; we keep track of the position in the
// argv._ array that is currently being executed.
return command.runCommand(cmd, self, parsed, i + 1)
} else if (!firstUnknownCommand && cmd !== completionCommand) {
firstUnknownCommand = cmd
break
}
}

Expand Down

0 comments on commit baba863

Please sign in to comment.