diff --git a/src/command.ts b/src/command.ts index 43937df7..36b8e71c 100644 --- a/src/command.ts +++ b/src/command.ts @@ -21,11 +21,12 @@ const wrap = require('wrap-ansi') export default class CommandHelp { render: (input: string) => string - constructor(public config: Config.IConfig, public opts: HelpOptions) { + constructor(public command: Config.Command, public config: Config.IConfig, public opts: HelpOptions) { this.render = template(this) } - command(cmd: Config.Command): string { + generate(): string { + const cmd = this.command const flags = sortBy(Object.entries(cmd.flags || {}) .filter(([, v]) => !v.hidden) .map(([k, v]) => { @@ -34,10 +35,10 @@ export default class CommandHelp { }), f => [!f.char, f.char, f.name]) const args = (cmd.args || []).filter(a => !a.hidden) let output = compact([ - this.usage(cmd, flags), + this.usage(flags), this.args(args), this.flags(flags), - this.description(cmd), + this.description(), this.aliases(cmd.aliases), this.examples(cmd.examples || (cmd as any).example), ]).join('\n\n') @@ -45,25 +46,27 @@ export default class CommandHelp { return output } - protected usage(cmd: Config.Command, flags: Config.Command.Flag[]): string { - let body = (cmd.usage ? castArray(cmd.usage) : [this.defaultUsage(cmd, flags)]) + protected usage(flags: Config.Command.Flag[]): string { + const usage = this.command.usage + let body = (usage ? castArray(usage) : [this.defaultUsage(flags)]) .map(u => `$ ${this.config.bin} ${u}`.trim()) .join('\n') return [ bold('USAGE'), - indent(wrap(body, this.opts.maxWidth - 2, {trim: false, hard: true}), 2), + indent(wrap(this.render(body), this.opts.maxWidth - 2, {trim: false, hard: true}), 2), ].join('\n') } - protected defaultUsage(command: Config.Command, _: Config.Command.Flag[]): string { + protected defaultUsage(_: Config.Command.Flag[]): string { return compact([ - command.id, - command.args.filter(a => !a.hidden).map(a => this.arg(a)).join(' '), + this.command.id, + this.command.args.filter(a => !a.hidden).map(a => this.arg(a)).join(' '), // flags.length && '[OPTIONS]', ]).join(' ') } - protected description(cmd: Config.Command): string | undefined { + protected description(): string | undefined { + const cmd = this.command let description = cmd.description && this.render(cmd.description).split('\n').slice(1).join('\n') if (!description) return return [ diff --git a/src/index.ts b/src/index.ts index 886d0610..218eebcd 100644 --- a/src/index.ts +++ b/src/index.ts @@ -113,8 +113,8 @@ export default class Help { } command(command: Config.Command): string { - const help = new CommandHelp(this.config, this.opts) - return help.command(command) + const help = new CommandHelp(command, this.config, this.opts) + return help.generate() } topics(topics: Config.Topic[]): string | undefined { diff --git a/test/command.test.ts b/test/command.test.ts index cb095fd6..87075669 100644 --- a/test/command.test.ts +++ b/test/command.test.ts @@ -200,6 +200,23 @@ ARGUMENTS OPTIONS --[no-]opt`)) + test + .commandHelp(class extends Command { + static id = 'apps:create' + static usage = '<%= config.bin %> <%= command.id %> usage' + }) + .it('outputs usage with templates', ctx => expect(ctx.commandHelp).to.equal(`USAGE + $ oclif oclif apps:create usage`)) + + test + .commandHelp(class extends Command { + static id = 'apps:create' + static usage = ['<%= config.bin %>', '<%= command.id %> usage'] + }) + .it('outputs usage arrays with templates', ctx => expect(ctx.commandHelp).to.equal(`USAGE + $ oclif oclif + $ oclif apps:create usage`)) + // class AppsCreate3 extends Command { // static id = 'apps:create' // static flags = {