Skip to content

Commit

Permalink
feat: allow templates in usage and provide command context (#35)
Browse files Browse the repository at this point in the history
  • Loading branch information
amphro authored and jdx committed Jan 30, 2019
1 parent 0777d50 commit 2dc9533
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 13 deletions.
25 changes: 14 additions & 11 deletions src/command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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]) => {
Expand All @@ -34,36 +35,38 @@ 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')
if (this.opts.stripAnsi) output = stripAnsi(output)
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 [
Expand Down
4 changes: 2 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
17 changes: 17 additions & 0 deletions test/command.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down

0 comments on commit 2dc9533

Please sign in to comment.