Skip to content

Commit a2f2eae

Browse files
laggingreflexbcoe
authored andcommitted
feat: allow setting scriptName $0 (#1143)
1 parent 88b6d23 commit a2f2eae

3 files changed

Lines changed: 39 additions & 0 deletions

File tree

docs/api.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -813,6 +813,20 @@ var yargs = require("yargs")(['--info'])
813813
.argv
814814
```
815815

816+
<a name="scriptName"></a>.scriptName($0)
817+
------------------
818+
819+
Set the name of your script ($0). Default is the base filename executed by node (`process.argv[1]`)
820+
821+
Example:
822+
823+
```js
824+
var yargs = require("yargs")
825+
.scriptName("my-script")
826+
.help()
827+
.argv
828+
```
829+
816830
<a name="showHidden"></a>.showHidden()
817831
-----------------------------------------
818832
.showHidden([option | boolean])

test/usage.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1232,6 +1232,26 @@ describe('usage tests', () => {
12321232
})
12331233
})
12341234

1235+
describe('scriptName', () => {
1236+
it('should display user supplied scriptName', () => {
1237+
const r = checkUsage(() => yargs(['--help'])
1238+
.scriptName('custom')
1239+
.command('command')
1240+
.parse()
1241+
)
1242+
r.logs.join('\n').split(/\n+/).should.deep.equal([
1243+
'custom [command]',
1244+
'Commands:',
1245+
' custom command',
1246+
'Options:',
1247+
' --help Show help [boolean]',
1248+
' --version Show version number [boolean]'
1249+
])
1250+
r.errors.should.have.length(0)
1251+
r.exit.should.equal(true)
1252+
})
1253+
})
1254+
12351255
it('should succeed when rebase', () => {
12361256
rebase(['home', 'chevex'].join(path.sep), ['home', 'chevex', 'foo', 'bar', 'baz'].join(path.sep)).should.equal(['foo', 'bar', 'baz'].join(path.sep))
12371257
rebase(['home', 'chevex', 'foo', 'bar', 'baz'].join(path.sep), ['home', 'chevex'].join(path.sep)).should.equal(['..', '..', '..'].join(path.sep))

yargs.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,11 @@ function Yargs (processArgs, cwd, parentRequire) {
3737

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

40+
self.scriptName = function scriptName (scriptName) {
41+
self.$0 = scriptName
42+
return self
43+
}
44+
4045
self.$0 = process.argv
4146
.slice(0, 2)
4247
.map((x, i) => {

0 commit comments

Comments
 (0)