UNPKG

tiny-bin

Version:

A library for building tiny and beautiful command line apps.

40 lines (39 loc) 1.38 kB
/* IMPORT */ import colors from 'tiny-colors'; import Addon from './addon.js'; import { identity } from '../utils.js'; /* MAIN */ class Usage extends Addon { constructor() { /* VARIABLES */ super(...arguments); this.usages = new Set(); } /* API */ print(command) { this.stdout.group('USAGE', () => { if (this.usages.size) { //TODO: Maybe automatically colorize these this.usages.forEach(usage => { this.stdout.print(usage); }); } else { //TODO: List required options too const isCommandDefault = (command === this.bin.command); const binName = this.bin.metadata.name; const commandName = isCommandDefault ? '' : command.name; const name = [binName, commandName].filter(identity).join(' '); const commands = isCommandDefault && !command.handler ? colors.magenta('[command]') : ''; const args = command.arguments.getAll().map(arg => colors.yellow(arg.name)).join(' '); const line = [name, commands, args].filter(identity).join(' '); this.stdout.print(line); } }); } register(usage) { this.usages.add(usage); } } /* EXPORT */ export default Usage;