UNPKG

tiny-bin

Version:

A library for building tiny and beautiful command line apps.

41 lines (40 loc) 1.21 kB
/* IMPORT */ import Addon from './addon.js'; import Arguments from './arguments.js'; import Options from './options.js'; import Usage from './usage.js'; /* MAIN */ class Command extends Addon { /* CONSTRUCTOR */ constructor(bin, options) { super(bin); this.arguments = new Arguments(this.bin); this.options = new Options(this.bin); this.usage = new Usage(this.bin); this.ids = [this.parse(options.name)]; this.name = options.name; this.description = options.description; this.section = options.section || ''; this.deprecated = !!options.deprecated; this.hidden = !!options.hidden; } /* PRIVATE API */ parse(name) { const re = /^_?[a-z][a-z-]*$/; const isValid = re.test(name); if (!isValid) this.bin.fail(`Invalid command: "${name}"`); return name; } /* API */ async run(options, argv) { if (!this.handler) { this.bin.fail(`Command handler not defined for command: "${this.name}"`); } else { return this.handler(options, options._, options['--']); } } } /* EXPORT */ export default Command;