deno.land / x / sade@v1.8.1 / deno / 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
96
97
98
99
100
101
102
103
104
105
const GAP = 4;const __ = " ";const ALL = "__all__";const DEF = "__default__";const NL = "\n";
function format(arr) { if (!arr.length) return ""; const len = maxLen(arr.map((x) => x[0])) + GAP; const join = (a) => a[0] + " ".repeat(len - a[0].length) + a[1] + (a[2] == null ? "" : ` (default ${a[2]})`); return arr.map(join);}
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;}
function noop(s) { return s;}
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 const help = function (bin, tree, key, single) { let out = ""; const cmd = tree[key], pfx = `$ ${bin}`, all = tree[ALL]; const prefix = (s) => `${pfx} ${s}`.replace(/\s+/g, " ");
// update ALL & CMD options const 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; const rgx = /^__/; let help = ""; const 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 const error = function (bin, str, num = 1) { let out = section("ERROR", [str], noop); out += (NL + __ + `Run \`$ ${bin} --help\` for more info.` + NL); console.error(out); Deno.exit(num);};
// Strips leading `-|--` & extra space(s)export const parse = function (str) { return (str || "").split(/^-{1,2}|,|\s+-{1,2}|\s+/).filter(Boolean);};
// @see https://stackoverflow.com/a/18914855/3577474export const sentences = function (str) { return (str || "").replace(/([.?!])\s*(?=[A-Z])/g, "$1|").split("|");};
sade

Version Info

Tagged at
2 years ago