import {Directive} from "../Directive" import {Injectable, Inject} from "../../di" import {padRight} from "../../util" import {CommandLine} from "../service" /** * Directive that prints the help message and usage information about * directives registered with the command line utility. */ @Injectable() export class UsageDirective extends Directive { @Inject() protected readonly cli!: CommandLine public getKeywords(): string | string[] { return 'help' } public getDescription(): string { return 'print information about available commands' } public handle(argv: string[]): void | Promise { const directiveStrings = this.cli.getDirectives() .map(cls => this.make(cls)) .map<[string, string]>(dir => { return [dir.getMainKeyword(), dir.getDescription()] }) const maxLen = directiveStrings.max(x => x[0].length) const printStrings = directiveStrings.map(grp => { return [padRight(grp[0], maxLen + 1), grp[1]] }) .map(grp => { return ` ${grp[0]}: ${grp[1]}` }) .toArray() console.log(this.cli.getASCIILogo()) console.log([ '', 'Welcome to Extollo! Specify a command to get started.', '', `USAGE: ex [..options]`, '', ...printStrings, '', 'For usage information about a particular command, pass the --help flag.', '-------------------------------------------', `powered by Extollo, © ${(new Date).getFullYear()} Garrett Mills`, ].join('\n')) } }