deno.land / x / sade@v1.8.1 / src / utils.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
85
86
87
88
89
90
91
92
93
94
95
export const ALL = '__all__';export const DEF = '__default__';
export const GAP = 4;export const __ = ' ';export const NL = '\n';
export function format(arr) { if (!arr.length) return ''; let len = maxLen( arr.map(x => x[0]) ) + GAP; let join = a => a[0] + ' '.repeat(len - a[0].length) + a[1] + (a[2] == null ? '' : ` (default ${a[2]})`); return arr.map(join);}
export function maxLen(arr) { let c=0, d=0, l=0, i=arr.length; if (i) while (i--) { d = arr[i].length; if (d > c) { l = i; c = d; } } return arr[l].length;}
export function noop(s) { return s;}
export function section(str, arr, fn) { if (!arr || !arr.length) return ''; let i=0, out=''; out += (NL + __ + str); for (; i < arr.length; i++) { out += (NL + __ + __ + fn(arr[i])); } return out + NL;}
export function help(bin, tree, key, single) { let out='', cmd=tree[key], pfx=`$ ${bin}`, all=tree[ALL]; let prefix = s => `${pfx} ${s}`.replace(/\s+/g, ' ');
// update ALL & CMD options let tail = [['-h, --help', 'Displays this message']]; if (key === DEF) tail.unshift(['-v, --version', 'Displays current version']); cmd.options = (cmd.options || []).concat(all.options, tail);
// write options placeholder if (cmd.options.length > 0) cmd.usage += ' [options]';
// description ~> text only; usage ~> prefixed out += section('Description', cmd.describe, noop); out += section('Usage', [cmd.usage], prefix);
if (!single && key === DEF) { let key, rgx=/^__/, help='', cmds=[]; // General help :: print all non-(alias|internal) commands & their 1st line of helptext for (key in tree) { if (typeof tree[key] == 'string' || rgx.test(key)) continue; if (cmds.push([key, (tree[key].describe || [''])[0]]) < 3) { help += (NL + __ + __ + `${pfx} ${key} --help`); } }
out += section('Available Commands', format(cmds), noop); out += (NL + __ + 'For more info, run any command with the `--help` flag') + help + NL; } else if (!single && key !== DEF) { // Command help :: print its aliases if any out += section('Aliases', cmd.alibi, prefix); }
out += section('Options', format(cmd.options), noop); out += section('Examples', cmd.examples.map(prefix), noop);
return out;}
export function error(bin, str, num=1) { let out = section('ERROR', [str], noop); out += (NL + __ + `Run \`$ ${bin} --help\` for more info.` + NL); console.error(out); process.exit(num);}
// Strips leading `-|--` & extra space(s)export function parse(str) { return (str || '').split(/^-{1,2}|,|\s+-{1,2}|\s+/).filter(Boolean);}
// @see https://stackoverflow.com/a/18914855/3577474export function sentences(str) { return (str || '').replace(/([.?!])\s*(?=[A-Z])/g, '$1|').split('|');}
sade

Version Info

Tagged at
2 years ago