UNPKG

ern-api-gen

Version:

Electrode Native API generator

54 lines 1.96 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const camelCase_1 = __importDefault(require("lodash/camelCase")); const padEnd_1 = __importDefault(require("lodash/padEnd")); const PREFIX = /^-+?/; const SORT_OPTS = (b, a) => { if (a.required && !b.required) { return 1; } if (b.required && !a.required) { return -1; } let ret = a.name[0] .replace(PREFIX, '') .localeCompare(b.name[0].replace(PREFIX, '')); if (ret === 0 && a.name.length > 1) { if (b.name.length < 2) { return 1; } ret = a.name[1] .repalce(PREFIX, '') .localeCompare(b.name[1].replace(PREFIX, '')); } return ret; }; class CommandUsage { constructor(values, meta, cmd) { this.values = values; this.cmd = cmd; this.meta = meta; } run() { const Usage = this.cmd.Usage; const name = Usage.name; const options = Usage.options; const description = Usage.description; const { values } = this; let str = `Command ${name} has the following options:\n${description}`; for (const opt of options.sort(SORT_OPTS)) { const property = opt.property || camelCase_1.default(values[opt.title]); const value = values[property] || opt.title; const short = opt.name.length > 1 ? opt.name[0] : ''; const long = opt.name.length > 1 ? opt.name[1] : opt.name[0]; str = `${str} \t${padEnd_1.default(opt.required ? '* ' : '', 2)}${padEnd_1.default(short, 5)} ${padEnd_1.default(long, 25)} ${padEnd_1.default(opt.hasArg ? `[${value}]` : '', 20)}\v${opt.description}`; } return str; } } exports.CommandUsage = CommandUsage; //# sourceMappingURL=CommandUsage.js.map