Skip to content

Commit

Permalink
fix: don't prepend bin to command example
Browse files Browse the repository at this point in the history
  • Loading branch information
egoist committed Nov 25, 2018
1 parent 78ab2fd commit 8c5ec9e
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 8 deletions.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -343,10 +343,14 @@ Allow unknown options in this command, by default CAC will log an error when unk

#### command.example(example)

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

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

```ts
type CommandExample = ((bin: string) => string) | string
```
### Events
Listen to commands:
Expand Down
14 changes: 14 additions & 0 deletions examples/command-examples.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
require('ts-node/register')
const cli = require('../src/index')()

cli
.command('build', 'Build project')
.example('cli build foo.js')
.example(bin => {
return `${bin} build foo.js`
})
.option('--type [type]', 'Choose a project type')

const parsed = cli.parse()

console.log(JSON.stringify(parsed, null, 2))
19 changes: 14 additions & 5 deletions src/Command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ interface CommandConfig {

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

type CommandExample = ((bin: string) => string) | string

export default class Command {
options: Option[]
aliasNames: string[]
Expand All @@ -34,7 +36,7 @@ export default class Command {
commandAction?: (...args: any[]) => any
usageText?: string
versionNumber?: string
examples: string[]
examples: CommandExample[]
config: CommandConfig
helpCallback?: HelpCallback

Expand Down Expand Up @@ -63,8 +65,8 @@ export default class Command {
return this
}

example(text: string) {
this.examples.push(text)
example(example: CommandExample) {
this.examples.push(example)
return this
}

Expand Down Expand Up @@ -173,7 +175,14 @@ export default class Command {
if (this.examples.length > 0) {
sections.push({
title: 'Examples',
body: this.examples.map(v => ` ${config.bin} ${v}`).join('\n')
body: this.examples
.map(example => {
if (typeof example === 'function') {
return example(config.bin)
}
return example
})
.join('\n')
})
}

Expand Down Expand Up @@ -225,4 +234,4 @@ export default class Command {
}
}

export { HelpCallback }
export { HelpCallback, CommandExample }
4 changes: 2 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { EventEmitter } from 'events'
import path from 'path'
import minimost, { Opts as MinimostOpts } from 'minimost'
import Command, { HelpCallback } from './Command'
import Command, { HelpCallback, CommandExample } from './Command'
import { OptionConfig } from './Option'
import { getMinimostOptions } from './utils'

Expand Down Expand Up @@ -73,7 +73,7 @@ class CAC extends EventEmitter {
* Add a global example
* @param example
*/
example(example: string) {
example(example: CommandExample) {
this.globalCommand.example(example)
return this
}
Expand Down

0 comments on commit 8c5ec9e

Please sign in to comment.