Skip to content

Commit df9669d

Browse files
committed
fix: allow to set program name
1 parent ca1b5bb commit df9669d

File tree

4 files changed

+25
-13
lines changed

4 files changed

+25
-13
lines changed

README.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
- [Projects Using CAC](#projects-using-cac)
3131
- [References](#references)
3232
- [CLI Instance](#cli-instance)
33+
- [cac(name?)](#cacname)
3334
- [cli.command(name, description, config?)](#clicommandname-description-config)
3435
- [cli.option(name, description, config?)](#clioptionname-description-config)
3536
- [cli.parse(argv?)](#cliparseargv)
@@ -253,6 +254,10 @@ const cac = require('cac')
253254
const cli = cac()
254255
```
255256

257+
#### cac(name?)
258+
259+
Create a CLI instance, optionally specify the program name which will be used to display in help and version message. When not set we use the basename of `argv[1]`.
260+
256261
#### cli.command(name, description, config?)
257262

258263
- Type: `(name: string, description: string) => Command`
@@ -370,7 +375,7 @@ Allow unknown options in this command, by default CAC will log an error when unk
370375
Add an example which will be displayed at the end of help message.
371376

372377
```ts
373-
type CommandExample = ((bin: string) => string) | string
378+
type CommandExample = ((name: string) => string) | string
374379
```
375380
376381
### Events

examples/command-examples.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ const cli = require('../src/index')()
44
cli
55
.command('build', 'Build project')
66
.example('cli build foo.js')
7-
.example(bin => {
8-
return `${bin} build foo.js`
7+
.example(name => {
8+
return `${name} build foo.js`
99
})
1010
.option('--type [type]', 'Choose a project type')
1111

src/CAC.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ interface MriResult extends ParsedArgv {
2424
}
2525

2626
class CAC extends EventEmitter {
27-
bin: string
27+
/** The program name to display in help and version message */
28+
name: string
2829
commands: Command[]
2930
globalCommand: GlobalCommand
3031
matchedCommand: Command
@@ -45,8 +46,12 @@ class CAC extends EventEmitter {
4546
*/
4647
rawOptions: MriResult['rawOptions']
4748

48-
constructor() {
49+
/**
50+
* @param name The program name to display in help and version message
51+
*/
52+
constructor(name = '') {
4953
super()
54+
this.name = name
5055
this.commands = []
5156
this.globalCommand = new GlobalCommand(this)
5257
this.globalCommand.usage('<command> [options]')
@@ -159,7 +164,9 @@ class CAC extends EventEmitter {
159164
} = {}
160165
): ParsedArgv {
161166
this.rawArgs = argv
162-
this.bin = argv[1] ? path.basename(argv[1]) : 'cli'
167+
if (!this.name) {
168+
this.name = argv[1] ? path.basename(argv[1]) : 'cli'
169+
}
163170

164171
let shouldParse = true
165172

src/Command.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ class Command {
124124
}
125125

126126
outputHelp() {
127-
const { bin, commands } = this.cli
127+
const { name, commands } = this.cli
128128
const {
129129
versionNumber,
130130
options: globalOptions,
@@ -133,13 +133,13 @@ class Command {
133133

134134
const sections: HelpSection[] = [
135135
{
136-
body: `${bin}${versionNumber ? ` v${versionNumber}` : ''}`
136+
body: `${name}${versionNumber ? ` v${versionNumber}` : ''}`
137137
}
138138
]
139139

140140
sections.push({
141141
title: 'Usage',
142-
body: ` $ ${bin} ${this.usageText || this.rawName}`
142+
body: ` $ ${name} ${this.usageText || this.rawName}`
143143
})
144144

145145
const showCommands =
@@ -165,7 +165,7 @@ class Command {
165165
body: commands
166166
.map(
167167
command =>
168-
` $ ${bin}${
168+
` $ ${name}${
169169
command.name === '' ? '' : ` ${command.name}`
170170
} --help`
171171
)
@@ -202,7 +202,7 @@ class Command {
202202
body: this.examples
203203
.map(example => {
204204
if (typeof example === 'function') {
205-
return example(bin)
205+
return example(name)
206206
}
207207
return example
208208
})
@@ -228,11 +228,11 @@ class Command {
228228
}
229229

230230
outputVersion() {
231-
const { bin } = this.cli
231+
const { name } = this.cli
232232
const { versionNumber } = this.cli.globalCommand
233233
if (versionNumber) {
234234
console.log(
235-
`${bin}/${versionNumber} ${process.platform}-${process.arch} node-${
235+
`${name}/${versionNumber} ${process.platform}-${process.arch} node-${
236236
process.version
237237
}`
238238
)

0 commit comments

Comments
 (0)