1 | module.exports = function showUsage(knownOpts, shortHands /*, description1, description2, ... */ ) {
|
2 | var descriptions = Array.prototype.slice.call(arguments, 2, arguments.length),
|
3 | knownOptsMaxLen = maxLength(Object.keys(knownOpts)),
|
4 | shortHands2 = {},
|
5 | SPACE = 4;
|
6 |
|
7 |
|
8 | Object.keys(shortHands).forEach(function (key) {
|
9 | if (key == "___singles") return;
|
10 | var value = Array.isArray(shortHands[key]) ? shortHands[key][0] : shortHands[key];
|
11 | if (typeof value == "string") {
|
12 | var option = value.replace('--', '');
|
13 | shortHands2[option] = shortHands2[option] || [];
|
14 | shortHands2[option].push(key);
|
15 | }
|
16 | });
|
17 |
|
18 |
|
19 | var shortHandsMaxLength = values(shortHands2).reduce(function (a, b) {
|
20 | a = Array.isArray(a) ? a.join(', -').length : a;
|
21 | b = Array.isArray(b) ? b.join(', -').length : b;
|
22 | return (a > b) ? a : b;
|
23 | });
|
24 |
|
25 |
|
26 | var out = {};
|
27 | Object.keys(knownOpts).forEach(function (key) {
|
28 | var shorts = shortHands2[key] ? ', -' + shortHands2[key].join(', -') : '';
|
29 | var cmd = rpad('', SPACE) + '--' + key + shorts;
|
30 | out[key] = rpad(cmd, knownOptsMaxLen + shortHandsMaxLength + 5 + 2 * SPACE);
|
31 | });
|
32 |
|
33 |
|
34 | descriptions.forEach(function (description) {
|
35 | var descriptionMaxLen = maxLength(values(description));
|
36 | Object.keys(knownOpts).forEach(function (key) {
|
37 | var value = '' + (description[key] !== undefined ? description[key] : '');
|
38 | out[key] += rpad(value, descriptionMaxLen + SPACE);
|
39 | });
|
40 | });
|
41 |
|
42 | return values(out).join('\n');
|
43 | };
|
44 |
|
45 |
|
46 | function rpad(str, length) {
|
47 | while (str.length < length)
|
48 | str = str + ' ';
|
49 | return str;
|
50 | }
|
51 |
|
52 |
|
53 | function values(o) {
|
54 | return Object.keys(o).map(function (k) {
|
55 | return o[k];
|
56 | });
|
57 | }
|
58 |
|
59 |
|
60 | function maxLength(arr) {
|
61 | return arr.map(function (str) {
|
62 | return ('' + str).length;
|
63 | }).reduce(function (a, b) {
|
64 | return (a > b) ? a : b;
|
65 | });
|
66 | } |
\ | No newline at end of file |