UNPKG

tiny-bin

Version:

A library for building tiny and beautiful command line apps.

48 lines (47 loc) 1.68 kB
/* IMPORT */ import Addon from './addon.js'; import Argument from './argument.js'; import ChainableAction from './chainable_action.js'; import ChainableCommandLocal from './chainable_command_local.js'; import Command from './command.js'; import Option from './option.js'; import { isObject } from './utils.js'; /* MAIN */ class ChainableCommandGlobal extends Addon { /* API */ config(options) { this.bin.config.update(options); return this; } usage(usage) { this.bin.command.usage.register(usage); return this; } option(name, description, options) { const optionOptions = isObject(name) ? name : { name, description, ...options }; const option = new Option(this.bin, optionOptions); this.bin.command.options.register(option, !!optionOptions.override); return this; } argument(name, description, options) { const argumentOptions = isObject(name) ? name : { name, description, ...options }; const argument = new Argument(this.bin, argumentOptions); this.bin.command.arguments.register(argument); return this; } action(handler) { this.bin.command.handler = handler; return new ChainableAction(this.bin); } command(name, description, options) { const commandOptions = isObject(name) ? name : { name, description, ...options }; const command = new Command(this.bin, commandOptions); this.bin.commands.register(command); return new ChainableCommandLocal(this.bin, command); } run(argv) { return this.bin.run(argv); } } /* EXPORT */ export default ChainableCommandGlobal;