Skip to content

Commit

Permalink
fix: less eager help command execution (#972)
Browse files Browse the repository at this point in the history
BREAKING CHANGE: help command now only executes if it's the last positional in argv._
  • Loading branch information
bcoe committed Oct 13, 2017
1 parent db77c53 commit 8c1d7bf
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 36 deletions.
10 changes: 7 additions & 3 deletions test/command.js
Expand Up @@ -682,10 +682,14 @@ describe('Command', () => {
''
]

helpCmd.logs.join('\n').split(/\n+/).should.deep.equal(expectedCmd)
// no help is output if help isn't last
// positional argument.
helpCmd.logs.should.eql([])
helpCmdSub.logs.should.eql([])
cmdHelpSub.logs.should.eql([])

// shows help if it is the last positional argument.
cmdHelp.logs.join('\n').split(/\n+/).should.deep.equal(expectedCmd)
helpCmdSub.logs.join('\n').split(/\n+/).should.deep.equal(expectedSub)
cmdHelpSub.logs.join('\n').split(/\n+/).should.deep.equal(expectedSub)
cmdSubHelp.logs.join('\n').split(/\n+/).should.deep.equal(expectedSub)
})
})
Expand Down
21 changes: 0 additions & 21 deletions test/yargs.js
Expand Up @@ -1728,27 +1728,6 @@ describe('yargs dsl tests', () => {
])
h.result.should.have.property('_').and.deep.equal(['h'])
})

it('uses single-char help alias as command if there are no multi-char aliases', () => {
const h = checkOutput(() => yargs('h')
.help('h').alias('h', '?')
.wrap(null)
.argv
)
const q = checkOutput(() => yargs('?')
.help('h').alias('h', '?')
.wrap(null)
.argv
)
const expected = [
'Options:',
' --version Show version number [boolean]',
' -h, -? Show help [boolean]',
''
]
h.logs[0].split('\n').should.deep.equal(expected)
q.logs[0].split('\n').should.deep.equal(expected)
})
})

describe('.coerce()', () => {
Expand Down
20 changes: 8 additions & 12 deletions yargs.js
Expand Up @@ -693,7 +693,6 @@ function Yargs (processArgs, cwd, parentRequire) {
if (parseOptions[pk][key] && !(pk in opts)) opts[pk] = parseOptions[pk][key]
}
})

self.group(key, usage.getPositionalGroupName())
return self.option(key, opts)
}
Expand Down Expand Up @@ -982,17 +981,14 @@ function Yargs (processArgs, cwd, parentRequire) {
// consider any multi-char helpOpt alias as a valid help command
// unless all helpOpt aliases are single-char
// note that parsed.aliases is a normalized bidirectional map :)
let helpCmds = [helpOpt].concat(aliases[helpOpt] || [])
const multiCharHelpCmds = helpCmds.filter(k => k.length > 1)
if (multiCharHelpCmds.length) helpCmds = multiCharHelpCmds
// look for and strip any helpCmds from argv._
argv._ = argv._.filter((cmd) => {
if (~helpCmds.indexOf(cmd)) {
argv[helpOpt] = true
return false
}
return true
})
const helpCmds = [helpOpt]
.concat(aliases[helpOpt] || [])
.filter(k => k.length > 1)
// check if help should trigger and strip it from _.
if (~helpCmds.indexOf(argv._[argv._.length - 1])) {
argv._.pop()
argv[helpOpt] = true
}
}
// if there's a handler associated with a
// command defer processing to it.
Expand Down

0 comments on commit 8c1d7bf

Please sign in to comment.