-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelp.js
84 lines (74 loc) · 2.44 KB
/
help.js
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
'use strict';
const content = require('./content');
let getTextFromArray = function(arr, prefix){
let text = '';
arr.forEach(el => {
text = text + '\n' + prefix + el.toString();
});
return text;
};
let getLongDesc = function (element) {
let longDesc = element.longDesc;
let text ='';
if (!longDesc){
return text;
}
if (longDesc.required && longDesc.required.length > 0){
text = text +'\nRequired Arguments:' + getTextFromArray(longDesc.required, '\t');
}
if (longDesc.optional && longDesc.optional.length > 0){
text = text +'\n\nOptional Arguments:' + getTextFromArray(longDesc.optional, '\t');
}
if (longDesc.samples && longDesc.samples.length > 0){
text = text +'\n\nSamples:' + getTextFromArray(longDesc.samples, `\t gite ${element.cmd} `);
}
text = text +'\n\n';
return text;
};
let showHelpForCommand = function (element, showFull) {
showFull = showFull || false;
let result ='';
if(showFull) {
result = '\n' + 'usage:\t gite ' + element.cmd + (element.args ? ' ' + element.args : '') + '\n\n';
result = result + 'Description:\n' + element.desc + '\n';
result = result + (element.longDesc ? getLongDesc(element): '');
}
else {
result = '\t' + padding(element.cmd.split(' ')[0],30) + '\t' + element.desc;
}
console.log(result);
};
let padding = function (str, pad, char) {
char = char || ' ';
if (str.length >= pad) {
return str;
}
return str + Array(pad-str.length).join(char);
};
let showHelpForCommands = function (elements, showFull) {
showFull = showFull || false;
elements.forEach(element => {
showHelpForCommand(element, showFull);
});
};
let showHelpForOptions = function (elements) {
elements.forEach(element => {
showHelpForOption(element);
});
};
let showHelpForOption = function (element) {
let result ='';
result = '\t' + padding(element.option, 30) + '\t' + element.desc;
console.log(result);
};
let showHelpGlobal = (config) => {
console.log(content.usage);
console.log(content.commandsHeader);
showHelpForCommands(config.commandList);
console.log(content.optionsHeader);
showHelpForOptions(config.globalOptions);
console.log(content.helpFooter);
};
module.exports.showHelpForCommand = showHelpForCommand;
module.exports.showHelpForCommands = showHelpForCommands;
module.exports.showHelpGlobal = showHelpGlobal;