From 8c5ec9eda3c260c072c4d934bb2d0b9ec11517f6 Mon Sep 17 00:00:00 2001 From: EGOIST <0x142857@gmail.com> Date: Mon, 26 Nov 2018 00:10:03 +0800 Subject: [PATCH] fix: don't prepend bin to command example --- README.md | 6 +++++- examples/command-examples.js | 14 ++++++++++++++ src/Command.ts | 19 ++++++++++++++----- src/index.ts | 4 ++-- 4 files changed, 35 insertions(+), 8 deletions(-) create mode 100644 examples/command-examples.js diff --git a/README.md b/README.md index 4ab3087..e6ad9a7 100644 --- a/README.md +++ b/README.md @@ -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: diff --git a/examples/command-examples.js b/examples/command-examples.js new file mode 100644 index 0000000..e5700c5 --- /dev/null +++ b/examples/command-examples.js @@ -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)) diff --git a/src/Command.ts b/src/Command.ts index 91d4842..e7eeeee 100644 --- a/src/Command.ts +++ b/src/Command.ts @@ -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[] @@ -34,7 +36,7 @@ export default class Command { commandAction?: (...args: any[]) => any usageText?: string versionNumber?: string - examples: string[] + examples: CommandExample[] config: CommandConfig helpCallback?: HelpCallback @@ -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 } @@ -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') }) } @@ -225,4 +234,4 @@ export default class Command { } } -export { HelpCallback } +export { HelpCallback, CommandExample } diff --git a/src/index.ts b/src/index.ts index 7780c90..586809d 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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' @@ -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 }