/
program.js
271 lines (238 loc) · 7.24 KB
/
program.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
var cadence = require('cadence')
var createUsage = require('./usage')
var getopt = require('./getopt')
var util = require('util')
var slice = [].slice
var interrupt = require('interrupt').createInterrupter('bigeasy.arguable')
var events = require('events')
function isNumeric (n) { return !isNaN(parseFloat(n)) && isFinite(n) }
// TODO IPv6.
function isListen (value) {
var bind = value.split(':')
if (bind.length == 1) {
bind.unshift('0.0.0.0')
}
if (isNumeric(bind[1])) {
var parts = bind[0].split('.')
if (parts.length == 4) {
return parts.filter(function (part) {
return isNumeric(part) && 0 <= +part && +part <= 255
}).length == 4
}
}
return false
}
function Command (program, name, gotopts) {
this.program = program
this.name = name
this.given = gotopts.given
this.params = gotopts.params
this.ordered = gotopts.ordered
this.terminal = gotopts.terminal
this.param = {}
this.given.forEach(function (key) {
this.param[key] = this.params[key][this.params[key].length - 1]
}, this)
}
Command.prototype.required = function () {
slice.call(arguments).forEach(function (param) {
if (!(param in this.param)) {
this.program.abend(param + ' is required')
}
}, this)
}
Command.prototype.numeric = function () {
this.validate.apply(this, [ '%s is not numeric' ].concat(slice.call(arguments))
.concat(isNumeric))
}
Command.prototype.bind = function (name) {
this.validate('%s is not bindable', name, isListen)
var bind = this.param[name].split(':')
if (bind.length == 1) {
bind.unshift('0.0.0.0')
}
return { address: bind[0], port: +bind[1] }
}
Command.prototype.validate = function () {
var vargs = slice.call(arguments)
var format = vargs.shift()
var test = vargs.pop()
var f = test instanceof RegExp ? function (value) {
return test.test(value)
} : test
vargs.forEach(function (param) {
if ((param in this.param) && !f(this.param[param])) {
this.program.abend(util.format(format, param))
}
}, this)
}
function Program (usage, env, argv, io) {
this._usage = usage
// use environment `LANG` or else language of first usage definition
this.lang = env.LANG ? env.LANG.split('.')[0] : usage.language
this.path = []
var state, root, parent = {}, path = [], command
root = parent
for (;;) {
state = usage.getCommand(argv, state)
if (!state) {
break
}
argv = argv.slice(state.command.length)
while (state.command.length != 0) {
command = state.command.shift()
path.push(command)
parent.command = new Command(this, command, { given: [], params: {} })
parent = parent.command
}
var params = {}
var opt = getopt(usage.getPattern(path), argv)
if (opt.abend) {
this.abend(opt.abend, opt.context)
}
parent.command = new Command(this, command, opt)
parent = parent.command
}
this.command = root.command
if (!this.command) {
this.path = []
this.abend('command required')
}
var path = [], iterator = this.command
while (iterator.command) {
path.push(iterator.command.name)
iterator = iterator.command
}
this.argv = argv = argv.slice()
this.params = {}
this.env = env
this.stdout = io.stdout
this.stderr = io.stderr
this.stdin = io.stdin
this.params = io.params
this.send = io.send
this._require = io.require
this._process = io.events
this._hooked = {}
this.path = path
}
util.inherits(Program, events.EventEmitter)
Program.prototype.on = function (event, listener) {
this._hook(event)
events.EventEmitter.prototype.on.call(this, event, listener)
}
Program.prototype.once = function (event, listener) {
this._hook(event)
events.EventEmitter.prototype.once.call(this, event, listener)
}
Program.prototype.disconnect = function () {
this._process.disconnect()
}
Program.prototype.disconnectIf = function () {
if (this.connected) {
this.disconnect()
}
}
Program.prototype.__defineSetter__('exitCode', function (exitCode) {
this._process.exitCode = exitCode
})
Program.prototype.__defineGetter__('exitCode', function () {
return this._process.exitCode
})
Program.prototype.__defineGetter__('connected', function () {
return this._process.connected
})
Program.prototype._hook = function (event) {
if (!this._hooked[event]) {
this._process.on(event, function () {
this.emit.apply(this, [ event ].concat(slice.call(arguments)))
}.bind(this))
this._hooked[event] = true
}
}
// format messages using strings.
Program.prototype.format = function (key) {
return this._usage.format(this.lang, this.path, key, slice.call(arguments, 1))
}
// abend helper stops execution and prints a message
Program.prototype.abend = function () {
var vargs = slice.call(arguments), key = vargs.shift(), code
if (typeof key == 'number') {
this._code = key
key = vargs.shift()
} else {
this._code = 1
}
var message
if (key) {
message = this._usage.format(this.lang, this.path, key, vargs)
}
this._redirect = 'stderr'
throw interrupt({
name: 'abend',
context: {
key: key,
method: 'abend',
stderr: message,
code: this._code
}
})
}
// help helper prints stops execution and prints the help message
Program.prototype.help = function () {
this._code = 0
throw interrupt({
name: 'help',
context: {
method: 'help',
stdout: this._usage.chooseUsage(this.lang, this.path),
code: this._code
}
})
}
Program.prototype.assert = function (condition, message) {
if (!condition) this.abend(message)
}
Program.prototype.require = function (moduleName) {
return this._require(moduleName)
}
Program.prototype.helpIf = function (help) {
if (help) this.help()
}
Program.prototype.delegate = cadence(function (async, prefix) {
var argv = this.argv.slice()
var sub = argv.shift()
var pkg = [ prefix, sub ].join('.')
var f
try {
f = require(pkg)
} catch (error) {
if (error.code == 'MODULE_NOT_FOUND') {
this.abend('cannot find executable command', this.path.concat(sub).join(' '))
} else {
throw error
}
}
var io = {
stdout: this.stdout,
stdin: this.stdin,
stderr: this.stderr,
events: this._process,
send: this.send
}
f(this.env, argv, io, async())
})
// exit helper stops execution and exits with the given code, hmm...
// TODO This ought to be testable, how do I test this?
Program.prototype.exit = function (code) {
throw interrupt({ name: 'exit', context: { code: code } })
}
module.exports = cadence(function (async, source, env, argv, io, main) {
// parse usage
var usage = createUsage(source)
if (!usage) {
throw new Error('no usage found')
}
// run program
main(new Program(createUsage(source), env, argv, io), async())
})