This repository has been archived by the owner on Nov 13, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 31
/
AbstractHelpGenerator.ts
277 lines (249 loc) · 10.9 KB
/
AbstractHelpGenerator.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
/*
* This program and the accompanying materials are made available under the terms of the
* Eclipse Public License v2.0 which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-v20.html
*
* SPDX-License-Identifier: EPL-2.0
*
* Copyright Contributors to the Zowe Project.
*
*/
import { TextUtils } from "../../../../utilities";
import { format, isNullOrUndefined } from "util";
import { CommandOptionType, ICommandDefinition, ICommandOptionDefinition } from "../../../";
import { ImperativeError } from "../../../../error";
import { Logger } from "../../../../logger";
import { IHelpGeneratorParms } from "../doc/IHelpGeneratorParms";
import { IHelpGeneratorFactoryParms } from "../doc/IHelpGeneratorFactoryParms";
import { IHelpGenerator } from "../doc/IHelpGenerator";
import { Constants } from "../../../../constants";
export abstract class AbstractHelpGenerator implements IHelpGenerator {
public static SHORT_DASH = "-";
public static LONG_DASH = "--";
/**
* Get a heading for the CLI / help
* @param {string} header - The text you want to display in the header
* @param indent - prefix the the heading and dashes with this string (defaults to one space)
* @param color - the color to highlight the header in
* @returns {string} the formatted/colored header
*/
public static formatHelpHeader(header: string, indent: string = " ", color: string): string {
if (isNullOrUndefined(header) || header.trim().length === 0) {
throw new ImperativeError({
msg: "Null or empty header provided; could not be formatted."
});
}
const numDashes = header.length + 1;
const headerText = TextUtils.formatMessage("{{indent}}{{headerText}}\n{{indent}}{{dashes}}",
{ headerText: header.toUpperCase(), dashes: Array(numDashes).join("-"), indent });
return TextUtils.chalk[color](headerText);
}
/**
* The command name being invoked by the user.
* For example, when using "banana --help", banana is the command name.
*/
protected mRootCommandName: string;
/**
* The command definition for which we are building help
*/
protected mCommandDefinition: ICommandDefinition;
/**
* The full command definition - which includes the parents of the command.
*/
protected mDefinitionTree: ICommandDefinition;
/**
* Produce markdown - not help text.
*/
protected mProduceMarkdown: boolean = false;
/**
* Logger
*/
protected mLog: Logger;
protected mPrimaryHighlightColor: string;
/**
* The configured experimental command description.
* Has a default, generic description which can be overridden through parameters
* to the constructor
* @type {string}
*/
protected mExperimentalCommandDescription: string = Constants.DEFAULT_EXPERIMENTAL_COMMAND_EXPLANATION;
/**
* A map of group names to option names and aliases - useful in help text/doc generation
* e.g. this.grouptoOption["job options"] -> [ "--async | -a ", "--activate | -A"]
*/
protected groupToOption: {
[key: string]: string[];
} = {};
/**
* A map of option names and aliases to their descriptions - useful in help text/doc generation
* e.g. this.optionToDescription["--async | a"] -> "Don't wait for this job to complete before returning"
*/
protected optionToDescription: {
[key: string]: string;
} = {};
/**
* Get log instance
*/
protected get log(): Logger {
return this.mLog;
}
// TODO - rework these parameter (and possible the help generator scheme)
constructor(defaultParms: IHelpGeneratorFactoryParms, commandParms: IHelpGeneratorParms) {
if (isNullOrUndefined(commandParms.commandDefinition) || isNullOrUndefined(commandParms.fullCommandTree)) {
throw new ImperativeError({
msg: "Error initializing help generator. The command definition or command definition tree was null or undefined.",
additionalDetails: JSON.stringify(commandParms.commandDefinition) + "\n\n" + JSON.stringify(commandParms.fullCommandTree)
});
}
this.mCommandDefinition = commandParms.commandDefinition;
this.mDefinitionTree = commandParms.fullCommandTree;
this.mProduceMarkdown = defaultParms.produceMarkdown;
this.mRootCommandName = defaultParms.rootCommandName;
this.mPrimaryHighlightColor = defaultParms.primaryHighlightColor;
this.mLog = Logger.getImperativeLogger();
if (commandParms.experimentalCommandsDescription != null) {
// use the configured experimental command description, if any
this.mExperimentalCommandDescription = commandParms.experimentalCommandsDescription;
}
}
public abstract buildHelp(): string;
public getOptionAndAliasesString(option: ICommandOptionDefinition, caseSensitive?: boolean): string {
let aliasString = "";
if (!isNullOrUndefined(option.aliases) && option.aliases.length > 0 &&
(option.aliases.join("").trim().length !== 0)) {
const formattedOptAliases = [];
aliasString += " | ";
for (const alias of option.aliases) {
if (!isNullOrUndefined(alias) && alias.length > 0) {
formattedOptAliases.push("{{codeBegin}}" +
(alias.length === 1 ? "-" : "--") + alias + "{{codeEnd}}");
}
else {
this.log.warn("The aliases for option " + option.name + " contained a null or empty alias." +
"This has been ignored; please take corrective action in your option definition.");
}
}
aliasString += formattedOptAliases.join(" | ");
}
const explainedType: string = this.explainType(option.type);
aliasString += " {{italic}}" + this.dimGrey("(" + explainedType + ")") + "{{italic}}";
// if (!option.required) {
// aliasString += " {{italic}}" + this.dimGrey("(optional)") + "{{italic}}";
// }
if (caseSensitive) {
aliasString += " {{italic}}" + this.dimGrey("(case sensitive)") + "{{italic}}";
}
return this.renderHelp(format("{{codeBegin}}--%s{{codeEnd}} %s", option.name, aliasString));
}
public abstract buildFullCommandHelpText(includeGlobalOptions: boolean): string;
protected buildOptionMaps() {
this.groupToOption = {};
this.optionToDescription = {};
if (isNullOrUndefined(this.mCommandDefinition.options)) {
return;
}
for (const option of this.mCommandDefinition.options) {
const group = option.group;
if (!this.groupToOption[group]) {
this.groupToOption[group] = [];
}
const caseSensitive = this.getCaseSensitiveFlagByOptionName(option.name);
const optionAndAliases = this.getOptionAndAliasesString(option, caseSensitive);
this.groupToOption[group].push(optionAndAliases);
// build the option help text
let optionText = option.description;
const defaultValueText = [undefined, null].includes(option.defaultValue) ? "" : this.grey("\nDefault value: " + option.defaultValue);
const allowableValuesText = option.allowableValues ? this.grey("\nAllowed values: " + option.allowableValues.values.join(", ")) : "";
if (defaultValueText.length > 0 || allowableValuesText.length > 0) {
optionText += "\n";
optionText += defaultValueText + allowableValuesText;
}
// Place the help text in the map
this.optionToDescription[optionAndAliases] = optionText;
}
}
protected getCaseSensitiveFlagByOptionName(optionName: string): boolean {
if (!isNullOrUndefined(this.mCommandDefinition.customize) &&
!isNullOrUndefined(this.mCommandDefinition.customize.commandStatement) &&
!isNullOrUndefined(this.mCommandDefinition.customize.commandStatement.children)) {
for (const child of this.mCommandDefinition.customize.commandStatement.children) {
if (child.name.toUpperCase() === optionName.toUpperCase()) {
return child.caseSensitive;
}
}
}
return undefined;
}
protected renderHelp(help: string): string {
if (isNullOrUndefined(help)) {
throw new ImperativeError({
msg: "Help unable to be rendered - the supplied help text was null or undefined."
});
}
// avoid replacing any literal {{strings like this}} in the help
const validTags = ["indent", "space", "italic", "header", "bullet", "codeBegin", "codeEnd"];
const mustachePattern = /\{\{([a-z0-9\-]*?)\}\}/ig;
help = help.replace(mustachePattern, (fullMatch, variableName) => {
if (validTags.indexOf(variableName) >= 0) {
return fullMatch;
}
else {
// temporarily change the mustache delimiter to avoid
// replacing literal curly braces in the help
return "{{=<% %>=}}" + fullMatch + "<%={{ }}=%>";
}
});
if (this.mProduceMarkdown) {
return TextUtils.renderWithMustache(help, {
indent: "\t",
space: " ",
italic: "*",
header: "#",
bullet: "*",
codeBegin: "`",
codeEnd: "`"
});
} else {
return TextUtils.renderWithMustache(help, {
indent: "",
space: "",
header: "",
italic: "",
bullet: "",
codeBegin: "",
codeEnd: ""
});
}
}
protected explainType(type: CommandOptionType) {
let explainedType = type as string;
if (explainedType === "existingLocalFile") {
explainedType = "local file path";
} else if (explainedType === "stringOrEmpty") {
explainedType = "string";
}
return explainedType;
}
/**
* Highlight text in dim grey (disabled if producing markdown)
* @param {string} text - the text you would like to highlight
* @returns {string} the highlighted text
*/
protected dimGrey(text: string) {
if (this.mProduceMarkdown) {
return text;
}
return TextUtils.chalk.grey.dim(text);
}
/**
* Highlight text in grey (disabled if producing markdown)
* @param {string} text - the text you would like to highlight
* @returns {string} the highlighted text
*/
protected grey(text: string) {
if (this.mProduceMarkdown) {
return text;
}
return TextUtils.chalk.grey(text);
}
}