-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
97 lines (82 loc) · 2.07 KB
/
index.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
function Cliclopts (opts) {
if (!(this instanceof Cliclopts)) {
return new Cliclopts(opts)
}
this.opts = opts || []
this.indent = ' '
this.width = 23
}
Cliclopts.prototype.print = function (stream) {
var output = stream || process.stdout
output.write(this.usage())
}
Cliclopts.prototype.usage = function () {
var output = ''
this.opts
.filter(function (opt) {
return opt.help
})
.forEach(function (option) {
output += this.indent
var helpIndex = option.helpIndex
if (!helpIndex) {
helpIndex = '--' + option.name
if (option.abbr) helpIndex += ', -' + option.abbr
}
output += helpIndex
var offset = 0
if (helpIndex.length > this.width) {
output += '\n' + this.indent
} else {
offset = helpIndex.length
}
output += Array(this.width - offset).join(' ')
output += option.help
if (option.hasOwnProperty('default')) {
output += ' (default: ' + JSON.stringify(option.default) + ')'
}
output += '\n'
}.bind(this))
return output
}
Cliclopts.prototype.booleans = function () {
return this.opts
.filter(function (opt) {
return opt.boolean
})
.map(function (opt) {
return opt.name
})
}
Cliclopts.prototype.boolean = Cliclopts.prototype.booleans
Cliclopts.prototype.default = function () {
var defaults = {}
this.opts.forEach(function (opt) {
if ('default' in opt) {
defaults[opt.name] = opt.default
if ('abbr' in opt) defaults[opt.abbr] = opt.default
}
})
return defaults
}
Cliclopts.prototype.alias = function () {
var alias = {}
this.opts.forEach(function (opt) {
var build = []
if ('alias' in opt) {
if (typeof opt.alias === 'string') build.push(opt.alias)
else build = build.concat(opt.alias)
}
if ('abbr' in opt) build.push(opt.abbr)
alias[opt.name] = build
})
return alias
}
Cliclopts.prototype.options = function () {
return {
alias: this.alias(),
boolean: this.boolean(),
default: this.default()
}
}
module.exports = Cliclopts