Skip to content

Commit

Permalink
fix(command): Run default cmd even if the only cmd (#950)
Browse files Browse the repository at this point in the history
This ensures that the default command is executed even if the default command
is the only command that is defined.
  • Loading branch information
danez authored and Morishiri committed Sep 25, 2017
1 parent 74a38b2 commit 7b22203
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
20 changes: 20 additions & 0 deletions test/command.js
Expand Up @@ -1198,6 +1198,26 @@ describe('Command', () => {
.argv
})

it('executes default command if undefined positional arguments and only command', (done) => {
yargs('baz --foo bar')
.command('*', 'default command', noop, (argv) => {
argv.foo.should.equal('bar')
argv._.should.contain('baz')
return done()
})
.argv
})

it('executes default command if defined positional arguments and only command', (done) => {
yargs('baz --foo bar')
.command('* <target>', 'default command', noop, (argv) => {
argv.foo.should.equal('bar')
argv.target.should.equal('baz')
return done()
})
.argv
})

it('does not execute default command if another command is provided', (done) => {
yargs('run bcoe --foo bar')
.command('*', 'default command', noop, (argv) => {})
Expand Down
3 changes: 3 additions & 0 deletions yargs.js
Expand Up @@ -985,6 +985,9 @@ function Yargs (processArgs, cwd, parentRequire) {
if (recommendCommands && firstUnknownCommand && !argv[helpOpt]) {
validation.recommendCommands(firstUnknownCommand, handlerKeys)
}
} else if (command.hasDefaultCommand() && !argv[helpOpt]) {
setPlaceholderKeys(argv)
return command.runCommand(null, self, parsed)
}

// generate a completion script for adding to ~/.bashrc.
Expand Down

0 comments on commit 7b22203

Please sign in to comment.