Skip to content

Commit

Permalink
fix: we shouldn't output help if we've printed a prior help-like mess…
Browse files Browse the repository at this point in the history
…age (#847)
  • Loading branch information
bcoe committed Apr 6, 2017
1 parent 50c9acd commit 17e89bd
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 15 deletions.
15 changes: 15 additions & 0 deletions test/yargs.js
Expand Up @@ -339,6 +339,21 @@ describe('yargs dsl tests', function () {
r.errors[1].should.match(/Did you mean goat/)
})

// see: https://github.com/yargs/yargs/issues/822
it('does not print help message if recommendation has been made', function (done) {
const parser = yargs()
.command('goat')
.help()
.recommendCommands()

parser.parse('boat help', {}, function (err, _argv, output) {
// it should not have printed the help text twice!
err.message.should.equal('Did you mean goat?')
output.split('Commands:').length.should.equal(2)
return done()
})
})

it("skips executing top-level command if builder's help is executed", function () {
var r = checkOutput(function () {
yargs(['blerg', '-h'])
Expand Down
33 changes: 18 additions & 15 deletions yargs.js
Expand Up @@ -1036,21 +1036,24 @@ function Yargs (processArgs, cwd, parentRequire) {
}

// Handle 'help' and 'version' options
Object.keys(argv).forEach(function (key) {
if (key === helpOpt && argv[key]) {
if (exitProcess) setBlocking(true)

skipValidation = true
self.showHelp('log')
self.exit(0)
} else if (key === versionOpt && argv[key]) {
if (exitProcess) setBlocking(true)

skipValidation = true
usage.showVersion()
self.exit(0)
}
})
// if we haven't already output help!
if (!hasOutput) {
Object.keys(argv).forEach(function (key) {
if (key === helpOpt && argv[key]) {
if (exitProcess) setBlocking(true)

skipValidation = true
self.showHelp('log')
self.exit(0)
} else if (key === versionOpt && argv[key]) {
if (exitProcess) setBlocking(true)

skipValidation = true
usage.showVersion()
self.exit(0)
}
})
}

// Check if any of the options to skip validation were provided
if (!skipValidation && options.skipValidation.length > 0) {
Expand Down

0 comments on commit 17e89bd

Please sign in to comment.