Skip to content

Commit 8c5ec9e

Browse files
committed
fix: don't prepend bin to command example
1 parent 78ab2fd commit 8c5ec9e

File tree

4 files changed

+35
-8
lines changed

4 files changed

+35
-8
lines changed

README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -343,10 +343,14 @@ Allow unknown options in this command, by default CAC will log an error when unk
343343

344344
#### command.example(example)
345345

346-
- Type: `(example: string) => Command`
346+
- Type: `(example: CommandExample) => Command`
347347

348348
Add an example which will be displayed at the end of help message.
349349

350+
```ts
351+
type CommandExample = ((bin: string) => string) | string
352+
```
353+
350354
### Events
351355
352356
Listen to commands:

examples/command-examples.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
require('ts-node/register')
2+
const cli = require('../src/index')()
3+
4+
cli
5+
.command('build', 'Build project')
6+
.example('cli build foo.js')
7+
.example(bin => {
8+
return `${bin} build foo.js`
9+
})
10+
.option('--type [type]', 'Choose a project type')
11+
12+
const parsed = cli.parse()
13+
14+
console.log(JSON.stringify(parsed, null, 2))

src/Command.ts

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ interface CommandConfig {
2525

2626
type HelpCallback = (sections: HelpSection[]) => void
2727

28+
type CommandExample = ((bin: string) => string) | string
29+
2830
export default class Command {
2931
options: Option[]
3032
aliasNames: string[]
@@ -34,7 +36,7 @@ export default class Command {
3436
commandAction?: (...args: any[]) => any
3537
usageText?: string
3638
versionNumber?: string
37-
examples: string[]
39+
examples: CommandExample[]
3840
config: CommandConfig
3941
helpCallback?: HelpCallback
4042

@@ -63,8 +65,8 @@ export default class Command {
6365
return this
6466
}
6567

66-
example(text: string) {
67-
this.examples.push(text)
68+
example(example: CommandExample) {
69+
this.examples.push(example)
6870
return this
6971
}
7072

@@ -173,7 +175,14 @@ export default class Command {
173175
if (this.examples.length > 0) {
174176
sections.push({
175177
title: 'Examples',
176-
body: this.examples.map(v => ` ${config.bin} ${v}`).join('\n')
178+
body: this.examples
179+
.map(example => {
180+
if (typeof example === 'function') {
181+
return example(config.bin)
182+
}
183+
return example
184+
})
185+
.join('\n')
177186
})
178187
}
179188

@@ -225,4 +234,4 @@ export default class Command {
225234
}
226235
}
227236

228-
export { HelpCallback }
237+
export { HelpCallback, CommandExample }

src/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { EventEmitter } from 'events'
22
import path from 'path'
33
import minimost, { Opts as MinimostOpts } from 'minimost'
4-
import Command, { HelpCallback } from './Command'
4+
import Command, { HelpCallback, CommandExample } from './Command'
55
import { OptionConfig } from './Option'
66
import { getMinimostOptions } from './utils'
77

@@ -73,7 +73,7 @@ class CAC extends EventEmitter {
7373
* Add a global example
7474
* @param example
7575
*/
76-
example(example: string) {
76+
example(example: CommandExample) {
7777
this.globalCommand.example(example)
7878
return this
7979
}

0 commit comments

Comments
 (0)