diff --git a/lib/usage.js b/lib/usage.js index ded4fc427..98d1508b0 100644 --- a/lib/usage.js +++ b/lib/usage.js @@ -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() diff --git a/test/usage.js b/test/usage.js index 4d211535d..20be98b4d 100644 --- a/test/usage.js +++ b/test/usage.js @@ -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', () => { diff --git a/yargs.js b/yargs.js index e9eae4bac..67f9d1125 100644 --- a/yargs.js +++ b/yargs.js @@ -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 }