Skip to content

Commit

Permalink
feat: allow setting scriptName $0 (#1143)
Browse files Browse the repository at this point in the history
  • Loading branch information
laggingreflex authored and bcoe committed Jun 24, 2018
1 parent 88b6d23 commit a2f2eae
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 0 deletions.
14 changes: 14 additions & 0 deletions docs/api.md
Expand Up @@ -813,6 +813,20 @@ var yargs = require("yargs")(['--info'])
.argv
```

<a name="scriptName"></a>.scriptName($0)
------------------

Set the name of your script ($0). Default is the base filename executed by node (`process.argv[1]`)

Example:

```js
var yargs = require("yargs")
.scriptName("my-script")
.help()
.argv
```

<a name="showHidden"></a>.showHidden()
-----------------------------------------
.showHidden([option | boolean])
Expand Down
20 changes: 20 additions & 0 deletions test/usage.js
Expand Up @@ -1232,6 +1232,26 @@ describe('usage tests', () => {
})
})

describe('scriptName', () => {
it('should display 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', () => {
rebase(['home', 'chevex'].join(path.sep), ['home', 'chevex', 'foo', 'bar', 'baz'].join(path.sep)).should.equal(['foo', 'bar', 'baz'].join(path.sep))
rebase(['home', 'chevex', 'foo', 'bar', 'baz'].join(path.sep), ['home', 'chevex'].join(path.sep)).should.equal(['..', '..', '..'].join(path.sep))
Expand Down
5 changes: 5 additions & 0 deletions yargs.js
Expand Up @@ -37,6 +37,11 @@ function Yargs (processArgs, cwd, parentRequire) {

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

self.scriptName = function scriptName (scriptName) {
self.$0 = scriptName
return self
}

self.$0 = process.argv
.slice(0, 2)
.map((x, i) => {
Expand Down

0 comments on commit a2f2eae

Please sign in to comment.