UNPKG

cpp-merge

Version:

Command line tool to produce single source file from multiple C/C++ files

102 lines 3.43 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const os_1 = require("os"); const stringUtils_1 = require("../common/stringUtils"); class HelpFormatter { constructor() { this.syntaxTitle = "Syntax"; this.argumentsTitle = "Arguments"; this.optionsTitle = "Options"; this.lineMaxLength = 80; this.smallMargin = " ".repeat(2); this.largeMargin = " ".repeat(16); this.descriptionMaxLength = this.lineMaxLength - this.largeMargin.length; } formatHelp(programName, description, args, options) { let help = programName; help += os_1.EOL; help += os_1.EOL; help += (0, stringUtils_1.limitLineLength)(description, this.lineMaxLength).join(os_1.EOL); help += os_1.EOL; help += this.formatSectionTitle(this.syntaxTitle); help += this.formatSyntaxHelp(programName, args, options); help += os_1.EOL; if (args.length > 0) { help += this.formatArgumentsHelp(args); } if (options.length > 0) { help += this.formatOptionsHelp(options); } return help; } formatSectionTitle(title) { let help = os_1.EOL; help += `${title}:`; help += os_1.EOL; return help; } formatSyntaxHelp(programName, args, options) { let help = this.smallMargin; help += programName; if (options.length > 0) { help += ` [${this.optionsTitle.toUpperCase()}]`; } for (const argument of args) { const valueName = argument.valueName || argument.name; help += ` <${valueName}>`; } return help; } formatArgumentsHelp(args) { let help = this.formatSectionTitle(this.argumentsTitle); let separator = ""; for (const argument of args) { help += separator; help += this.formatArgumentHelp(argument); separator = os_1.EOL; } return help; } formatArgumentHelp(argument) { let help = this.smallMargin; help += argument.valueName || argument.name; help += os_1.EOL; help += this.formatDescription(argument.description); return help; } formatDescription(description) { let help = ""; const descriptionLines = (0, stringUtils_1.limitLineLength)(description, this.descriptionMaxLength); descriptionLines.forEach(line => { help += this.largeMargin; help += line; help += os_1.EOL; }); return help; } formatOptionsHelp(options) { let help = this.formatSectionTitle(this.optionsTitle); let separator = ""; for (const option of options) { help += separator; help += this.formatOptionHelp(option); separator = os_1.EOL; } return help; } formatOptionHelp(option) { let help = this.smallMargin; help += option.options.map(opt => { let optionHelp = opt; if (option.value) { optionHelp += ` <${option.value.name}>`; } return optionHelp; }).join(", "); help += os_1.EOL; help += this.formatDescription(option.description); return help; } } exports.default = HelpFormatter; //# sourceMappingURL=HelpFormatter.js.map