Skip to content

Commit

Permalink
feat: generalize help coordination inot showHelp()
Browse files Browse the repository at this point in the history
  • Loading branch information
jdx committed Feb 3, 2018
1 parent be53238 commit fa37288
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 18 deletions.
14 changes: 2 additions & 12 deletions src/commands/help.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import {Command, flags} from '@anycli/command'
import cli from 'cli-ux'

import Help from '..'

Expand All @@ -14,18 +13,9 @@ export default class HelpCommand extends Command {
]

async run() {
const {flags, args} = this.parse(HelpCommand)
const {flags, argv} = this.parse(HelpCommand)
const format = flags.format as any || 'screen'
let id = args.command
let help = new Help(this.config, {format})
if (!id) {
let rootHelp = help.root()
cli.info(rootHelp)
} else {
let command = this.config.findCommand(id, {must: true})
let commandHelp = help.command(command)
cli.info(commandHelp)
}
if (format === 'screen') cli.info()
help.showHelp(argv)
}
}
24 changes: 18 additions & 6 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,24 @@ export interface HelpOptions {
export default class Help {
constructor(public config: Config.IConfig, public opts: HelpOptions = {}) {}

showHelp(command: Config.Command.Class, _: string[]) {
// if (command.type === 'engine') {
// cli.info(this.root())
// } else {
cli.info(this.command(Config.Command.toCached(command)))
// }
showHelp(argv: string[]) {
const getHelpSubject = () => {
for (let arg of argv) {
if (arg === '--') return
if (arg.startsWith('-')) continue
if (arg === 'help') continue
return arg
}
}
const subject = getHelpSubject()
if (!subject) {
cli.info(this.root())
} else {
// TODO: topic help
let command = this.config.findCommand(subject, {must: true})
cli.info(this.command(command))
}
if (this.opts.format === 'screen') cli.info()
}

root(): string {
Expand Down

0 comments on commit fa37288

Please sign in to comment.