Skip to content

Commit

Permalink
fix: prefer user supplied script name in usage (#1383)
Browse files Browse the repository at this point in the history
  • Loading branch information
cameronhunter authored and bcoe committed Jul 29, 2019
1 parent e3981fd commit 28c74b9
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 2 deletions.
2 changes: 1 addition & 1 deletion lib/usage.js
Expand Up @@ -151,7 +151,7 @@ module.exports = function usage (yargs, y18n) {
normalizeAliases()

// handle old demanded API
const base$0 = path.basename(yargs.$0)
const base$0 = yargs.customScriptName ? yargs.$0 : path.basename(yargs.$0)
const demandedOptions = yargs.getDemandedOptions()
const demandedCommands = yargs.getDemandedCommands()
const groups = yargs.getGroups()
Expand Down
18 changes: 18 additions & 0 deletions test/usage.js
Expand Up @@ -1250,6 +1250,24 @@ describe('usage tests', () => {
r.errors.should.have.length(0)
r.exit.should.equal(true)
})

it('should not alter the user supplied scriptName', () => {
const r = checkUsage(() => yargs(['--help'])
.scriptName('./custom')
.command('command')
.parse()
)
r.logs.join('\n').split(/\n+/).should.deep.equal([
'./custom [command]',
'Commands:',
' ./custom command',
'Options:',
' --help Show help [boolean]',
' --version Show version number [boolean]'
])
r.errors.should.have.length(0)
r.exit.should.equal(true)
})
})

it('should succeed when rebase', () => {
Expand Down
3 changes: 2 additions & 1 deletion yargs.js
Expand Up @@ -37,7 +37,8 @@ function Yargs (processArgs, cwd, parentRequire) {

if (!cwd) cwd = process.cwd()

self.scriptName = function scriptName (scriptName) {
self.scriptName = function (scriptName) {
self.customScriptName = true
self.$0 = scriptName
return self
}
Expand Down

0 comments on commit 28c74b9

Please sign in to comment.